Flutter:状态更新时BottomNavigationBar中currentIndex属性的值不更新

问题描述 投票:0回答:3

我有一个BottomNavigationBar,当按下一个图标时,它可以导航到其他页面。除状态更新时BottomNavigationBar中的currentIndex属性的值不更新外,这正常工作,这意味着静态BottomNavigationBar上没有变化。 enter image description here

我正在使用可变的(_selectedPage)跟踪BottomNavigationBar中的选定索引,并且在点击一个项目时值会更改,但是当状态更新时它并没有更新currentIndex属性。为什么?

import 'package:flutter/material.dart';
import 'package:independentproject/pages/home_page.dart';
import 'package:independentproject/pages/cook_recipe.dart';

class PageNavigationBar extends StatefulWidget {
  @override
  _PageNavigationBarState createState() => _PageNavigationBarState();
}

class _PageNavigationBarState extends State<PageNavigationBar> {
  //default page showing in bottom navigation bar will be CookRecipe()
  int _selectedPage = 1;

//all pages optional in bottom navigation bar
  final List<Widget> _pageOptions = [
    HomePage(),
    CookRecipe(),
  ];

  void onTapped(int pageTapped) {
    setState(() {
      //print(pageTapped);
      _selectedPage = pageTapped;
      Navigator.push(context, MaterialPageRoute(builder: (context) => _pageOptions[pageTapped]));
      //print(_selectedPage);
    });
  }

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      //TODO: currentIndex: doesn't update when the state updates, why?
      currentIndex: _selectedPage,
        //items showing in bottom navigation bar
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Homepage'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.search),
          title: Text('Search recipe'),
        ),
      ],
      onTap: (int pageTapped) {
        onTapped(pageTapped);
      },
    );
  }
}

import 'package:flutter/material.dart';
import 'package:independentproject/page_navigation_bar.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Home page'),
        ),
        body: Center(
          child: Text('Home page'),
        ),
        bottomNavigationBar: PageNavigationBar(),
      ),
    );
  }
}

import 'package:flutter/material.dart';
import 'package:independentproject/page_navigation_bar.dart';

class CookRecipe extends StatefulWidget {
  @override
  _CookRecipeState createState() => _CookRecipeState();
}

class _CookRecipeState extends State<CookRecipe> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Search recipe'),
        ),
        body: Center(
          child: Text('Search recipes'),
        ),
        bottomNavigationBar: PageNavigationBar(),
      ),
    );
  }
}

flutter flutter-layout flutter-navigation flutter-bottomnavigation
3个回答
0
投票
看看Provider一个非常有用的状态管理包

或者您也可以处理您的页面,当NavBar是您的Main Page并且您只有一页时

return MaterialApp( home: Scaffold( appBar: ownAppBar(_selectedPage), body: _bodyOptions[_selectedPage], bottomNavigationBar: BottomNavigationBar( currentIndex: _selectedPage, items: [ BottomNavigationBarItem( icon: Icon(Icons.home), title: Text('Homepage'), ), BottomNavigationBarItem( icon: Icon(Icons.search), title: Text('Search recipe'), ), ], onTap: (int pageTapped) { onTapped(pageTapped); }, ) ), ); final List<Widget> _bodyOptions = [ HomePage(), CookRecipe(), ];


0
投票
import 'package:flutter/material.dart'; main() { runApp(MaterialApp(home: PageNavigationBar())); } class PageNavigationBar extends StatefulWidget { @override _PageNavigationBarState createState() => _PageNavigationBarState(); } class _PageNavigationBarState extends State<PageNavigationBar> { //default page showing in bottom navigation bar will be CookRecipe() int _selectedPage = 1; //all pages optional in bottom navigation bar final List<Widget> _pageOptions = [ HomePage(), CookRecipe(), ]; void onTapped(int pageTapped) { setState(() { //print(pageTapped); _selectedPage = pageTapped; // Navigator.push(context, MaterialPageRoute(builder: (context) => _pageOptions[pageTapped])); //print(_selectedPage); }); } @override Widget build(BuildContext context) { return Scaffold( body: _pageOptions[_selectedPage], bottomNavigationBar: BottomNavigationBar( //TODO: currentIndex: doesn't update when the state updates, why? currentIndex: _selectedPage, //items showing in bottom navigation bar items: [ BottomNavigationBarItem( icon: Icon(Icons.home), title: Text('Homepage'), ), BottomNavigationBarItem( icon: Icon(Icons.search), title: Text('Search recipe'), ), ], onTap: (int pageTapped) { onTapped(pageTapped); }, ), ); } } class CookRecipe extends StatefulWidget { @override _CookRecipeState createState() => _CookRecipeState(); } class _CookRecipeState extends State<CookRecipe> { @override Widget build(BuildContext context) { return Center( child: Text('Search recipes'), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return Center( child: Text('Home page'), ); } }
© www.soinside.com 2019 - 2024. All rights reserved.