0

In a Scaffold page with something like the following structure

@override
Widget build(BuildContext context){
  body: PageView(
          controller: _controller;
          children: <Widget>[Page1(), Page2(), Page3()];
        );
  bottomNavigationBar: BottomNavBar(
                         onItemSelected: (index) => _controller.animateToPage()
                       )
}

there are two ways to go from Page2() to Page1():

  • Swipe the screen from left to right
  • Tap the Page1() icon on the bottomNavigationBar, and thus calling _controller.animateToPage(0)

The problem is, how can I tell if the page is changed through swiping gesture or animateToPage() function?

Thanks.

Steven Luo
  • 2,350
  • 3
  • 18
  • 35
  • `PageView` has a property `onPageChanged`. Will this suffice? – rickimaru Jan 13 '21 at 02:51
  • Both dragging behavior and `animateToPage()` will trig the `onPageChanged(index)` function, hard to distinguish between them. – Steven Luo Jan 13 '21 at 03:00
  • `animateToPage()` is invoked by you in the code other one is not, did you get my point, below answer explains it – Yadu Jan 13 '21 at 04:05

1 Answers1

2

Maybe you can add a flag to set if animateToPage is ongoing or not.

Sample:

import 'package:flutter/material.dart';

void main() => runApp(Root());

class Root extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final PageController controller = PageController();
    bool isAnimateToPage = false;

    controller.addListener(() {
      print('LISTENER isAnimateToPage = $isAnimateToPage');
    });

    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: <Widget>[
            Expanded(
              child: PageView(
                controller: controller,
                onPageChanged: (int page) {
                  print(
                      'ONPAGECHANGED isAnimateToPage = $isAnimateToPage ; page = $page');
                },
                children: <Widget>[
                  const Center(child: Text('page 1')),
                  const Center(child: Text('page 2')),
                  const Center(child: Text('page 3')),
                ],
              ),
            ),
            FlatButton(
              onPressed: () async {
                isAnimateToPage = true;
                await controller.animateToPage(
                  controller.page.toInt() + 1,
                  duration: const Duration(seconds: 1),
                  curve: Curves.easeIn,
                );
                isAnimateToPage = false;
              },
              child: const Text('Next'),
            ),
          ],
        ),
      ),
    );
  }
}
rickimaru
  • 2,275
  • 9
  • 13