如何从一个屏幕转到另一个屏幕,以从堆栈中清除所有先前的屏幕?

时间:2019-07-18 00:56:28

标签: flutter dart

在Flutter项目中,我有三个屏幕- Screen0,Screen1,Screen2 。现在,从 screen0 ,我可以单击按钮转到screen1,然后从 screen1 可以通过按钮转到 screen2 。在 screen2 中,我有一个按钮曾经用来返回到 screen0 。由于 Screen0 是初始屏幕,因此我想在回到 Screen0 时清除以前的所有屏幕,并且不想在此图中有任何后退选项,

enter image description here

这是我的代码-

main.dart

import 'package:flutter/material.dart';
import 'screen0.dart';
import 'screen1.dart';
import 'screen2.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context)=> Screen0(),
        '/first': (context)=> Screen1(),
        '/second': (context)=> Screen2(),
      },
    );
  }
}

我已经在main.dart文件中设置了所有路由。然后在Screen0中,我有一个按钮可以转到screen1,如下所示-

screen0.dart

导入“ package:flutter / material.dart”;

class Screen0 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        title: Text('Screen 0'),
      ),

      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              color: Colors.red,
              child: Text('Go to screen 1'),
              onPressed: (){
                Navigator.pushNamed(context, '/first');
              },
            ),

          ],
        ),
      ),

    );
  }

}

在screen1中,我有一个按钮可以转到screen2-

class Screen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        title: Text('Screen 1'),
      ),

      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              color: Colors.red,
              child: Text('Go to screen 2'),
              onPressed: (){
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) {
                    return Screen2();
                  })
                );
              },
            ),
          ],
        ),
      ),

    );
  }

}

现在,在screen2中,我具有转到screen0的按钮-

class Screen2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        title: Text('Screen 2'),
      ),

      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              color: Colors.red,
              child: Text('Go to screen 0'),
              onPressed: (){
                Navigator.pop(context, Screen2);
                Navigator.pushNamed(context, '/');
              },
            ),

          ],
        ),
      ),

    );
  }

}

我已经尝试过使用下面链接中给出的Navigator.pushAndRemoveUntil之类的解决方案-

Flutter - Navigate to a new screen, and clear all the previous screens

但是这种解决方案对我不起作用。

因此,如果有人帮助我解决该问题,这将是很好的。

1 个答案:

答案 0 :(得分:1)

使用pushNamedAndRemoveUntil

例如

Navigator.of(context).pushNamedAndRemoveUntil('/screen4', (Route<dynamic> route) => false);