Flutter路由替换以及返回根路由

应用场景

在这里插入图片描述

返回根路由

import 'package:flutter/material.dart';

import 'package:untitled1/pages/Tabs.dart';

class RegisterSecond extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('注册完成'),
      ),
      body: ListView(
        children: <Widget>[
          SizedBox(height: 200),
          RaisedButton(
            child: Text('完成',style: TextStyle(
                color: Colors.white
            ),),
            onPressed: (){
                //返回根路由
              Navigator.of(context).pushAndRemoveUntil(
                new MaterialPageRoute(builder: (context)=>Tabs()),//Tabs为根路由组件
                  (route)=>route==null
              );
            },
            color: Theme.of(context).accentColor,
          )
        ],
      ),
    );
  }
}

返回上级

      onPressed: (){
              //返回上级
              Navigator.of(context).pop();
            },

替换路由

      Navigator.of(context).pushReplacementNamed('/RegisterSecond');

猜你喜欢

转载自blog.csdn.net/qq_42572245/article/details/106689067