首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤动自定义导航栏

颤动自定义导航栏
EN

Stack Overflow用户
提问于 2019-03-13 11:58:31
回答 1查看 1.9K关注 0票数 0

我有一个自定义导航栏的问题。我无法更改正文中的页面。我放了4个不同的文本进行测试。稍后,我想拥有带有不同页面的自组织元数据覆盖图部件。_buildItem在不同的小部件中,但在同一个页面中。

代码语言:javascript
复制
class InicioPage extends StatefulWidget {
final int selectedIndex = 0;
  @override
  State<StatefulWidget> createState() {
    return InicioPageState(selectedIndex);
  }
}

class InicioPageState extends State<InicioPage> {
  int selectedIndex;
  InicioPageState(this.selectedIndex);

  final pages = [Text('page 0'),Text('page 1'),   ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        title: Text('Title of App'),     ),
      bottomNavigationBar: BottomNavBar(selectedIndex),
      body: pages[selectedIndex],      );     }    }

这是导航栏。我删掉了一些不重要的部分,因为这些字符。

代码语言:javascript
复制
class BottomNavBar extends StatefulWidget {
  final int selectedIndex;
  BottomNavBar(this.selectedIndex);
  @override
  State<StatefulWidget> createState() {
    return BottomNavBarState(selectedIndex);
  },}

class BottomNavBarState extends State<BottomNavBar> {
  int selectedIndex;
   BottomNavBarState(this.selectedIndex);
    List<NavigationItem> items = [
    NavigationItem(Icon(Icons.home), Text('Home')),
    NavigationItem(Icon(Icons.location_city), Text('Props')),
      ];
 @override
  Widget build(BuildContext context) {
    return Container(
    child: Row(
    children: items.map((item) { var itemIndex = items.indexOf(item);
    return GestureDetector(  onTap: () {   setState(() {
                  selectedIndex = itemIndex;
                });           },
              child: _buildItem(item, selectedIndex == itemIndex),
            );
          }).toList(),
        ));
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-13 15:59:42

不要通过构造函数在BottomNavBarState中传递参数,只需使用

代码语言:javascript
复制
widget.selectedIndex
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55134166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档