【问题标题】:Flutter: cant get navigation to work in projectFlutter:无法让导航在项目中工作
【发布时间】:2021-07-11 12:42:02
【问题描述】:

作为菜鸟,我正在尝试一些项目作为样板。 我有一个麻烦。 我可以让整个项目工作,除了导航。 我尝试了不同的解决方案和文档,但我想我的菜鸟让我看不到正确的问题以及如何解决它。

这是我再次使用的示例,除了底部导航到另一个“页面”:https://github.com/Sithira/FlutterRadioPlayer/tree/master/example/lib。 我需要为它们添加 1 或 2 个页面和相应的按钮。

是否有人愿意将我推向正确的方向,甚至提供可行的解决方案?

【问题讨论】:

    标签: flutter dart navigation


    【解决方案1】:

    您没有做任何事情来实际更改 setState() 上显示的页面

    尝试将第二页放在儿童列表中:

    _children = [
        new FirstPage(),
        new SecondPage(),
      ];
    

    另外,您忘记返回实际选择的页面。尝试类似:

    MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Flutter Radio Player Example'),
            ),
            body: _children[_currentIndex]
    

    这样,每次您 setState() 使用新索引时,页面都会根据您更改的 _currentIndex 重建显示新选择的页面。

    在 Flutter 中还有很多其他处理导航的方法。有些是: 带有Navigator.of(context).push) / pop() 等的命令式方法。 Navigator 2.0Router Delegate 的新声明式方法

    【讨论】:

    • 我已经添加了第二页。但我没有得到你建议的身体部位。 _children 和 _currentIndex 都保持未定义。
    • 您必须根据索引动态返回正文中的页面。 _children 必须是您的第一页和第二页的小部件列表。并且索引是您使用导航栏选择的当前索引
    • 尝试在一个新的小部件中提取脚手架主体内的所有内容。将它与 SecondPage 一起添加到子列表中。现在从正文中返回 _children[_currentIndex]。
    • 使用 Navigator.of(context) 方式让它工作。不得不改变 void main() => runApp(MyApp());进入 void main() => runApp(MaterialApp(title: "App", home: MyApp(), ));
    【解决方案2】:

    “a”解决方案是使用Navigator.of(context) 方法。

    为此,您必须进行更改:

    void main() => runApp(MyApp()); 
    

    到:

    void main() => runApp(MaterialApp(
          title: "App",
          home: MyApp(),
        ));
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2011-07-02
      • 2020-11-23
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多