【问题标题】:Why is my Navigator not responding anything?为什么我的导航器没有任何响应?
【发布时间】:2020-03-07 07:23:27
【问题描述】:

我不知道为什么我得到这么多反对票...我是新来的。如有任何误解,请见谅。希望有人能最终帮助我解决我的问题。

我的设置屏幕导航器不起作用。我希望你能帮助我。非常感谢!这是代码。

这是主要的飞镖

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

class biminda extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
          primaryColor: Color(0xFFad1457),
          scaffoldBackgroundColor: Colors.white),
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreeen(),
        '/setting': (context) => Settings(),
      },
    );
  }
}

从我的主屏幕

Container(
              padding: EdgeInsets.fromLTRB(20, 0, 0, 30),
              child: SideButton(
                functionality: 'Settings',
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Settings(),
                    ),
                  );
                },
              ),
            ),

我更新了容器,但没有任何改变。

Container(
              padding: EdgeInsets.fromLTRB(20, 0, 0, 30),
              child: SideButton(
                functionality: 'Settings',
                onPressed: () {
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => Settings(),
                    ),
                  );
                },
              ),
            ),

非常感谢!我希望这将得到解决。 :)

【问题讨论】:

    标签: flutter navigator


    【解决方案1】:

    你没有在主页面中声明你的设置()页面, 在 MaterialApp 的主目录中,你删除 home 用 initialRoute 替换它,如下面的代码,

    MaterialApp(
    
    //......
        initialRoute: '/',
              routes: {
                '/': (context) => HomePage(),
                '/setting': (context) => SettingsPage(), );
    

    【讨论】:

    • 好的,我实现了代码并且它运行了,但是按钮没有反应。我好难过
    • 尝试用 pushNamed 代替 push
    • 现在我收到一个错误:不能将参数类型 MaterialPageRoute 分配给参数类型 String。我会尝试修复它。非常感谢!:)
    【解决方案2】:

    请添加有关您的问题的更多详细信息,这是我根据您的代码提出的建议:

    在设置界面你不需要做

    Navigator.pop(context, MaterialPageRoute....);
    

    做一个

    Navigator.pop(context);
    

    【讨论】:

    • 感谢您的评论。我实现了@GirlWhoCode 的代码,它运行了,但按钮不会做任何事情。我只想切换到设置屏幕,但没有任何反应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多