如何使用Navigator.pop直到Flutter

时间:2019-02-04 22:11:44

标签: flutter

我正在开发Flutter应用程序,我想从第4页回到第1页。 我有一个非常奇怪的错误:

  

状态不好:未来已经完成

我创建了一个简单的项目来重现此错误:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      onGenerateRoute: routes,
    );
  }
}

Route routes(RouteSettings settings) {
  if (settings.name == '/page1') {
    return MaterialPageRoute(
      builder: (context) {
        return Page1();
      },
    );
  } else if (settings.name == '/page2') {
    return MaterialPageRoute(
      builder: (context) {
        return Page2();
      },
    );
  } else if (settings.name == '/page3') {
    return MaterialPageRoute(
      builder: (context) {
        return Page3();
      },
    );
  } else if (settings.name == '/page4') {
    return MaterialPageRoute(
      builder: (context) {
        return Page4();
      },
    );
  } else {
    return MaterialPageRoute(
      builder: (context) {
        return Page1();
      },
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Page 1'),
            RaisedButton(
              child: Text('Go Page 2'),
              onPressed: () {
                Navigator.of(context).pushNamed('/page2');
              },
            )
          ],
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Page 2'),
            RaisedButton(
              child: Text('Go Page 3'),
              onPressed: () {
                Navigator.of(context).pushNamed('/page3');
              },
            )
          ],
        ),
      ),
    );
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Page 3'),
            RaisedButton(
              child: Text('Go Page 4'),
              onPressed: () {
                Navigator.of(context).pushNamed('/page4');
              },
            )
          ],
        ),
      ),
    );
  }
}

class Page4 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Page 4'),
            RaisedButton(
              child: Text('Go Back Page 1'),
              onPressed: () {
                Navigator.of(context).popUntil(ModalRoute.withName('/page1'));
              },
            )
          ],
        ),
      ),
    );
  }
}

我该如何解决?

2 个答案:

答案 0 :(得分:1)

代替:

          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => Page1(),
            ),
          );

使用:

          Navigator.of(context).push(
            MaterialPageRoute(
              settings: RouteSettings(name: "/Page1"),
              builder: (context) => Page1(),
            ),
          );

然后您可以使用:

Navigator.of(context)
              .popUntil(ModalRoute.withName("/Page1"));

答案 1 :(得分:0)

是重复的问题。请参阅thisthis

基本上是发生了什么-启动应用程序时,page1打开,因为它进入了最后一个,并且没有分配任何名称,因此当您弹出该页面名称时,根本找不到它