【问题标题】:How navigate to another screen如何导航到另一个屏幕
【发布时间】:2019-10-20 14:07:30
【问题描述】:

我有一个下一个 RaisedButton 可以进入名为 DetailOracion 的下一个屏幕。

基于 Flutter 的例子来推新屏但不起作用。

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Oraciones Cristianas'),
        ),
        body: SafeArea(
            child: Padding(
          padding: EdgeInsets.all(10.0),
          child: Column(
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => DetailOracion()));
                },
                child: Text('Hello Wolrd'),
              )
            ],
          ),
        )),
      ),
    );
  }

我的详细信息Oracion

class DetailOracion extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Hola'),
    ),
    body: Text('Segunda Pantalla'),
  );
 }
}

错误消息是下一个

I/flutter ( 3441): The following assertion was thrown while handling a gesture:
I/flutter ( 3441): Navigator operation requested with a context that does not include a Navigator.
I/flutter ( 3441): The context used to push or pop routes from the Navigator must be that of a widget that is a
I/flutter ( 3441): descendant of a Navigator widget.

【问题讨论】:

  • 你能发布第一个屏幕的代码吗?
  • @KumarBibek 我添加了我的 main.dart 的完整代码
  • 您的MaterialApp 必须是您的自定义Widget 的父级 - 现在您拥有MaterialApp 作为它的子级(它是在您的Widgetbuild() 方法中创建的)

标签: android flutter dart flutter-navigation


【解决方案1】:

使用 MaterialPageRoute 时,您需要从 runApp()

发送 MaterialApp 内的主类

用代码解释

正确

void main() => runApp(MaterialApp(
  title: 'Oraciones Cristianas',
  home: MyApp(),
));

您向您发送了 MaterialApp() 的第一个 Screen insde,可以使用 MaterialPageRoute()

不正确

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

如果您只是在没有 MaterialApp() 的情况下发送您的第一个屏幕,则包装器不起作用

【讨论】:

    【解决方案2】:

    在您的按钮或列周围使用 Builder,如下代码所示:

    Builder(
        builder: (context) => RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(
                        builder: (context) => SelectUserType()));
              },
              child: Text('Registrese'),
            ),
      ),
    

    【讨论】:

      猜你喜欢
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多