Flutter - 在 Provider 值更改时触发 PageView 更改

标签 flutter flutter-provider flutter-pageview

我有一个带有 PageView 的自定义 BottomAppBar 用于页面。当用户单击 BottomAppBar 中的项目时,我正在尝试更改页面。我正在使用 Provider 来管理全局状态。单击项目会触发提供程序中的值更改,但 PageView 中没有方法可以在值更改时触发页面更改。我怎样才能做到这一点?下面是我的代码。

我的供应商:

class CurrentPage extends ChangeNotifier {
  int _currentPage = 0;
  int get currentPage => _currentPage;

  setCurrentPage(int val) {
    _currentPage = val;
    notifyListeners();
  }
}

一个 BottomAppBar 项目:

 InkWell(
                onTap: () {
                  context.read<CurrentPage>().setCurrentPage(0);
                },
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 26.5, vertical: 17.0),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        CustomIcons.home,
                        size: 18.0,
                        color: context.watch<CurrentPage>().currentPage == 0
                            ? Color(0xFF2C1DEB)
                            : Color(0xFFCCCCD5),
                      ),
                      SizedBox(height: 9.0),
                      Text(
                        'HOME',
                        style: TextStyle(
                          color: context.watch<CurrentPage>().currentPage == 0
                              ? Color(0xFF2C1DEB)
                              : Color(0xFFCCCCD5),
                          fontWeight: FontWeight.w800,
                          fontSize: 10.0,
                          letterSpacing: 1.0,
                        ),
                      ),
                    ],
                  ),
                ),
              ),

页面浏览量:

class Pages extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final PageController _pageController =
        PageController(initialPage: context.watch<CurrentPage>().currentPage);

    return PageView(
      onPageChanged: (int) {},
      controller: _pageController,
      scrollDirection: Axis.horizontal,
      pageSnapping: true,
      children: [
        Container(
          color: Colors.green,
          child: Text('Home Page'),
        ),
        Container(
          child: Text('Profile Page'),
        ),
        Container(
          child: Text('Search Page'),
        ),
        Container(
          child: Text('Jobs Page'),
        ),
      ],
    );
  }
}

最佳答案

这就是你如何做到的。一旦您从您的提供商调用 notifyListener,如果 currentPage 已更改,这将转到新选择的页面

class Pages extends StatelessWidget {
      final PageController _pageController =
            PageController(initialPage:0);
      @override
      Widget build(BuildContext context) {


    return Consumer<CurrentPage>(builder:(ctx, currentPage,  widget){

     final pageView = PageView(
          onPageChanged: (int) {},
          controller: _pageController,
          scrollDirection: Axis.horizontal,
          pageSnapping: true,
          children: [
            Container(
              color: Colors.green,
              child: Text('Home Page'),
            ),
            Container(
              child: Text('Profile Page'),
            ),
            Container(
              child: Text('Search Page'),
            ),
            Container(
              child: Text('Jobs Page'),
            ),
          ],);
          if(currentPage.currentPage != _pageController.page.floor()){
            _pageController.jumpToPage(currentPage.currentPage);
          }
          return pageView;
          }
        );
      }
    }

关于Flutter - 在 Provider 值更改时触发 PageView 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61934899/

相关文章:

Flutter showDialog 无法进行简单测试

Flutter 如何在单击 TextField 时始终隐藏键盘但保持焦点(保持显示光标)

使用分隔符按百分比进行 flutter 行布局

flutter - 在 flutter 中使用提供程序时,getter 被调用为 null

arrays - 需要将 JSON 数据提取字段配置到 flutter 中的小部件元素

flutter - 如何在 Flutter 中创建无限 PageView

Flutter - 在导航期间创建固定 AppBar 的最佳实践是什么

Flutter 提供程序将列表中的单个对象传递给 subview

flutter - 未处理的异常 : type 'Null' is not a subtype of type 'List<dynamic>' in type cast

flutter - flutter中同步PageView和Slider的换页