返回StateWidget的确切状态

问题描述 投票:0回答:1

Flutter中的路线正在创建他们正在调用的“StateWidget”的“新”瞬间(除非我误解了它)

我有两个问题(可能有一个答案):

我怎么能回到我来自的确切时刻,而且,如果我不想再回到它,我怎么能关闭StateWidget的瞬间,我尝试了exit(0)但它完全关闭了应用程序,然后崩溃了当我尝试super.dispose()

我使用以下代码:

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.deepOrange,
      ),
      home: new HomePage(title: 'Flutter Demo Home Page'),
      routes: <String, WidgetBuilder> {
        '/home': (BuildContext context) => new HomePage(title: 'Flutter Demo Home Page'),
        '/content': (BuildContext context) => new ContentPage(title: 'Content')
      },
    );
  }

并在以下路线之间切换:

new FlatButton(
          child: new Text(
            "Back to home",
            style: Theme.of(context).textTheme.display1,
          ),
          textColor: new Color(0xFF66BB6A),               
          onPressed: () {
            Navigator.of(context).pushNamed('/home');
          //  exit(0);  // this closed the app completly
          // super.dispose();  // the app crashed
          },
        ),

UPDATE

当我尝试Navigator.of(context).pop和其他人时,该应用程序被挂起如图所示:

enter image description here

UPDATE

一旦我尝试使用pushReplacementNamed,上面的行为得到修复,但在两个状态之间切换,但没有保持状态状态,而是创建了新状态!

dart flutter
1个回答
0
投票

您正在寻找的是Navigator.of(context).pop,它的变化如pushReplacementpushReplacementNamed。结合了push / pushNamedpop

© www.soinside.com 2019 - 2024. All rights reserved.