簡體   English   中英

如何導航到另一個屏幕

[英]How navigate to another screen

我有一個下一個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'),
              )
            ],
          ),
        )),
      ),
    );
  }

我的詳細信息

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.

使用MaterialPageRoute時,您需要從runApp()發送MaterialApp內部的主類

用代碼解釋

正確

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

你發送了第一個ScreenApps的MaterialApp() ,能夠使用MaterialPageRoute()

不正確

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

如果您只是發送沒有MaterialApp()的第一個屏幕,那么包裝器就不起作用了

在按鈕周圍或列周圍使用Builder,如下面的代碼所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM