Flutter:如何在不单击图标的情况下更改导航栏的视图?

发布于 2025-01-23 06:18:15 字数 3291 浏览 2 评论 0原文

我使用经典的 navigationRail ,在 row()两分裂的内部来更改视图。但是我也想通过单击视图中显示的项目来更改视图。 例如,在开始时 showtables()是可见的,当我单击 showtables()构建的按钮时,我想替换 showtables()由其他小部件。这个新的小部件可能有可能返回 showtables(),保持我的地图表的价值(或者至少我的应用程序状态 data 如果不可能)。我该怎么办?

class NavBarTest extends StatefulWidget {
  final Metadata? data;

  const NavBarTest({Key? key, required this.data}) : super(key: key);

  @override
  State<NavBarTest> createState() => _NavBarTestState();
}

class _NavBarTestState extends State<NavBarTest> {
  Map tables = {};
  int _selectedIndex = 0;

  @override
  void initState() {
    /*
      here I init tables...
    */
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child:
            Row(
              children: <Widget>[
                    NavigationRail(
                      groupAlignment: -1.0,
                      selectedIndex: _selectedIndex,
                      leading:
                      Column(children: [
                        SizedBox(
                          height: MediaQuery.of(context).size.height/3,
                        ),
                        SizedBox(
                          width: 50,
                          height: 50,
                          child: Image.asset("assets/logo.png"),
                        ),
                      ],),
                      onDestinationSelected: (int index) {
                        setState(() {
                          _selectedIndex = index;
                        });
                      },
                      labelType: NavigationRailLabelType.none,
                      destinations: const <NavigationRailDestination>[
                        NavigationRailDestination(
                          icon: Icon(Icons.spoke_rounded,color: icon),
                          selectedIcon: Icon(Icons.spoke_rounded,color: Colors.yellow),
                          label: Text('First'),
                        ),
                        NavigationRailDestination(
                          icon: Icon(Icons.show_chart_sharp,color: icon),
                          selectedIcon: Icon(Icons.show_chart_sharp,color: Colors.yellow),
                          label: Text('Second'),
                        ),
                        NavigationRailDestination(
                          icon: Icon(Icons.info_sharp,color: icon),
                          selectedIcon: Icon(Icons.info_outline,color: Colors.yellow),
                          label: Text('Third'),
                        ),
                      ],
                    ),
                    // This is the main content.
                    Expanded(
                        child: buildPage(_selectedIndex)
                    )
                  ],
                )
              )
      );
  }

  Widget buildPage(index){
    switch (index) {
      case 0:
        return ShowTables(tables: tables);
      case 1:
        return const Details();
      case 2:
        return Links(tables: tables);
      default:
        return ShowTables(tables: tables);
    }
  }

}

I use a classic NavigationRail, inside a Row() divided in two, to change the view. But I would also like to change the view by clicking on items displayed in the view.
For example, at the beginning ShowTables() is visible, and when I click on buttons built by ShowTables(), I want to replace ShowTables() by an other widget. This new widget could have the possibility to return to ShowTables(), keeping the value of my Map Tables (or at least my app state data if not possible). How can I do ?

class NavBarTest extends StatefulWidget {
  final Metadata? data;

  const NavBarTest({Key? key, required this.data}) : super(key: key);

  @override
  State<NavBarTest> createState() => _NavBarTestState();
}

class _NavBarTestState extends State<NavBarTest> {
  Map tables = {};
  int _selectedIndex = 0;

  @override
  void initState() {
    /*
      here I init tables...
    */
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child:
            Row(
              children: <Widget>[
                    NavigationRail(
                      groupAlignment: -1.0,
                      selectedIndex: _selectedIndex,
                      leading:
                      Column(children: [
                        SizedBox(
                          height: MediaQuery.of(context).size.height/3,
                        ),
                        SizedBox(
                          width: 50,
                          height: 50,
                          child: Image.asset("assets/logo.png"),
                        ),
                      ],),
                      onDestinationSelected: (int index) {
                        setState(() {
                          _selectedIndex = index;
                        });
                      },
                      labelType: NavigationRailLabelType.none,
                      destinations: const <NavigationRailDestination>[
                        NavigationRailDestination(
                          icon: Icon(Icons.spoke_rounded,color: icon),
                          selectedIcon: Icon(Icons.spoke_rounded,color: Colors.yellow),
                          label: Text('First'),
                        ),
                        NavigationRailDestination(
                          icon: Icon(Icons.show_chart_sharp,color: icon),
                          selectedIcon: Icon(Icons.show_chart_sharp,color: Colors.yellow),
                          label: Text('Second'),
                        ),
                        NavigationRailDestination(
                          icon: Icon(Icons.info_sharp,color: icon),
                          selectedIcon: Icon(Icons.info_outline,color: Colors.yellow),
                          label: Text('Third'),
                        ),
                      ],
                    ),
                    // This is the main content.
                    Expanded(
                        child: buildPage(_selectedIndex)
                    )
                  ],
                )
              )
      );
  }

  Widget buildPage(index){
    switch (index) {
      case 0:
        return ShowTables(tables: tables);
      case 1:
        return const Details();
      case 2:
        return Links(tables: tables);
      default:
        return ShowTables(tables: tables);
    }
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文