如何在flutter中通过弯曲导航栏导航不同页面?

use*_*587 4 flutter flutter-layout

我无法在颤动中使用弯曲导航栏,当我滑动屏幕时,弯曲导航栏的按钮也会移动,但当我点击弯曲导航栏的按钮时,什么也没有发生。我认为onTap()无法正常工作。点击按钮时如何导航页面?这是我的程序的代码=>

  static final String id = 'profile_page';
  @override
  _PagesState createState() => _PagesState();
}

class _PagesState extends State<Pages> {
  PageController _pageController;

  int _Page=0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override

  Widget build(BuildContext context) {
    return Scaffold(

        body: PageView(

            controller: _pageController,
          children: <Widget>[

            Search(),
            Trending(),
            Friends(),
            Profile(),
          ],
          onPageChanged: (int index) {
            setState(() {

              _pageController.jumpToPage(index);
            });
          }
          ),


          bottomNavigationBar: CurvedNavigationBar(
            animationCurve: Curves.easeInOutBack,

            index:3,

            items: <Widget>[
              Icon(Icons.search, size: 30, color: Colors.white, ),
              Icon(Icons.trending_up, size: 30, color: Colors.white),
              Icon(Icons.group, size: 30, color: Colors.white),
              Icon(Icons.person, size: 30, color: Colors.white)
            ],
            color: Colors.blueAccent,
            backgroundColor: Colors.white,
            height: 60.0,
            onTap: (int index) {
             setState(() {
               _pageController.jumpToPage(index);
             });
            },

          ),
        );

  }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*sal 6

我这样使用它:

  1. 创建一个列表和一个保存当前页号的变量。这里的 3 个类可以是无状态或有状态的 widget。

    final List<Widget> _tabItems = [Class1(), Class2(), Class3()];
    int _activePage = 0;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 像这样定义你的身体:

    body: _tabItems[_activePage], //Customise it as you want.
    
    Run Code Online (Sandbox Code Playgroud)
  3. 然后在你的 onTap 中:

    onTap: (index) {
       setState(() {
         _activePage = index;
       });
     },
    
    Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!快乐编码:)