颤振的导航消除了路线之间的

时间:2020-01-08 16:52:07

标签: flutter flutter-navigation

在我的Flutter应用中,我要在路线之间删除,因此当用户按回去键时,它将跳转到上一个屏幕。以下是我的屏幕流程。

  1. 仪表板登录屏幕。
  2. 用户单击列出主题按钮,然后进入主题列表页面。
  3. 用户单击创建新主题,并使用表单进入新页面以填写该主题的基本信息。
  4. 填写表单后,用户单击保存按钮,并使用刚创建的主题详细信息进入新页面
  5. 现在,如果用户单击返回,则他/她应该进入主题列表页面,而不是创建新主题页面。
  6. 如果他再次从主题列表页面上按回去,他应该看到仪表板,而不是退出应用程序。

先谢谢您。

1 个答案:

答案 0 :(得分:3)

如果您想在转到pushReplacement时跳过页面,只需使用push而不是back

enter image description here

import 'dart:math';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter naviagtion',
      theme: ThemeData(
        backgroundColor: Colors.white,
        primarySwatch: Colors.green,
      ),
      home: DashboardLandingScreen(),
    );
  }
}

class DashboardLandingScreen extends StatelessWidget {
  static Route route() => MaterialPageRoute(
        builder: (context) => DashboardLandingScreen(),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Dashboard landing screen.'),
            RaisedButton(
              child: Text('List of Topics'),
              onPressed: () => Navigator.of(context).push(
                TopicsListScreen.route(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class TopicsListScreen extends StatelessWidget {
  static Route route() => MaterialPageRoute(
        builder: (context) => TopicsListScreen(),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('List of Topics'),
            RaisedButton(
              child: Text('Create New Topic'),
              onPressed: () => Navigator.of(context).push(
                NewTopic.route(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class NewTopic extends StatelessWidget {
  static Route route() => MaterialPageRoute(
        builder: (context) => NewTopic(),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('page: new topic'),
            RaisedButton(
              child: Text('Save'),
              onPressed: () => Navigator.of(context).pushReplacement(
                CreatedTopicDetails.route(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class CreatedTopicDetails extends StatelessWidget {
  static Route route() => MaterialPageRoute(
        builder: (context) => CreatedTopicDetails(),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: Text('page: created topic details'),
      ),
    );
  }
}