【问题标题】:How do i open other .dart activity from bottom navigation bar item?如何从底部导航栏项打开其他 .dart 活动?
【发布时间】:2018-10-31 01:53:45
【问题描述】:

我已经用颤振实现了下面的底部导航栏

import 'package:flutter/material.dart';

class Test extends StatelessWidget  {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
    bottomNavigationBar: new BottomNavigationBar(
        items: [
          new BottomNavigationBarItem(
              icon: new Icon(Icons.add),
              title: new Text("trends")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.location_on),
              title: new Text("feed")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.people),
              title: new Text("community")
          )
        ]
    )
);}}

我该如何制作,onTap 在每个 BottomNavigationBar 项目上打开新的 .dart 类? 我已经尝试使用TabBarView 作为标签,但不知道如何处理底部导航栏。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    你可以这样做

    import 'package:flutter/material.dart';
    
    class Test extends StatefulWidget {
      @override
      TestState createState() {
        return new TestState();
      }
    }
    
    class TestState extends State<Test> {
      int _currentIndex = 0;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          bottomNavigationBar: new BottomNavigationBar(
            currentIndex: _currentIndex,
            onTap: (newIndex) => setState((){_currentIndex = newIndex;}),
            items: [
              new BottomNavigationBarItem(
                  icon: new Icon(Icons.add),
                  title: new Text("trends")
              ),
              new BottomNavigationBarItem(
                  icon: new Icon(Icons.location_on),
                  title: new Text("feed")
              ),
              new BottomNavigationBarItem(
                  icon: new Icon(Icons.people),
                  title: new Text("community")
              ),
            ],
          ),
          body: new IndexedStack(
            index: _currentIndex,
            children: <Widget>[
              new YourCustomTrendsWidget(),
              new YourCustomFeedWidget(),
              new YourCustomCommunityWidget(),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢。像魅力一样工作。 :)
    • 这个 StackedIndex 小部件代替 PageStorageKey 最适合在使用 BottomNavBar 时保持 bottomNavItem 的状态。干得好!
    • 您好,假设我有一个登录页面,在通过身份验证后,它会显示一个底部导航栏,可以加载几个页面,与它提出的解决方案相同。我的问题是,在底部导航栏显示的页面中,我有一个用于注销的按钮,而我尝试过的所有操作都会导致登录页面被加载,但底部导航栏却在一起。有什么建议吗?
    • 他们是 github 的例子吗?
    • @RussellHarrower 也许这有帮助:github.com/nisrulz/flutter-examples/blob/master/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2021-11-27
    • 2019-07-18
    相关资源
    最近更新 更多