flutter 清空路由 路由替换 返回到根路由

Flutter 中返回到上一级页面

// 使用Navigator的pop方法可以使Flutter应用返回到上一个页面
Navigator.of(context).pop();

Flutter 中替换路由

// 使用pushReplacementNamed可以替换当前的路由。比如从"用户中心"页面跳转到"registerFirst"页面,
// 然后从"registerFirst"页面跳转到"registerSecond"页面,这时点击"registerSecond"的返回按钮
// 会直接返回到"用户中心"页面。
Navigator.of(context).pushReplacementNamed('/registerSecond');

Flutter 返回到根路由

// 当我们需要从多级页面返回到根路由时(例如从registerThird页面返回到用户中心页面),
// 可以使用pushAndRemoveUntil方法。
import '../../home.dart';

Navigator.of(context).pushAndRemoveUntil(
    new MaterialPageRoute(
        builder: (context) => Home(index: 2)
    ), (route) => route == null);

Navigator.of(context).pushNamedAndRemoveUntil('/LoginScreen', (route) => false);

// Home.dart中的代码展示了如何构建Home类,这是一个带有index属性的StatefulWidget
Home.dart
class Home extends StatefulWidget {
    final index;
    Home({Key key, this.index = 0});
    _HomeState createState() => _HomeState(this.index);
}

class _HomeState extends State {
    var _currentIndex;
    var tabs = [Index(), ClassIf(), My()];
    _HomeState(index) {
        _currentIndex = index;
    }
}

相关参考链接

// 以下是关于Flutter导航和路由的详细博客链接,你可以点击查看更多详细内容。
https://blog.csdn.net/weixin_34999505/article/details/86760606

posted on 2020-02-21 12:02  完美前端  阅读(1442)  评论(0)    收藏  举报

导航