小编典典

如何在Flutter中获取当前路线路径?

flutter

在实现持久性底部栏时,单击底部栏中的按钮时,需要恢复以前的路线

单击底部栏中的按钮时,将保存其当前路径路径(/ a / b /
c
),并根据该按钮单击恢复先前保存的路径。

从概念上讲,用户会将每个按钮视为一个工作区,并且其状态永远不会丢失(包括后退堆栈)。用户可以安全地从一个工作区切换到另一个工作区。

当路由重绕到根时,如何在Flutter中获取当前路由路径?


阅读 1407

收藏
2020-08-13

共1个答案

小编典典

NavigatorState不会公开用于获取当前路线路径的API,Route也不会公开用于确定路线路径的API。路由可以是(通常是匿名的)。Route使用该isCurrent方法,您现在可以确定给定对象是否位于导航器堆栈的顶部,但这对于您的用例而言并不十分方便。

我建议您对这个问题采取不同的方法,并且根本不要倒回根本。相反,请Navigator对的每个窗格使用不同的小部件BottomNavigationBar。这样,您在窗格之间切换时就不必倒回堆栈。你可以用你的Navigator小部件OpacityIgnorePointer部件来隐藏他们时,他们不应该是不破坏他们的筹码可见。

应用程式影片

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class SecurePage extends StatelessWidget {
  final int index;

  SecurePage(this.index);

  Widget build(BuildContext context) {
    return new Material(
      color: Colors.amber,
      child: new InkWell(
        child: new Center(
          child: new Icon(
            Icons.security,
            color: Colors.white,
            size: index * 100.0 + 20.0,
          ),
        ),
        onTap: () {
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (BuildContext context) {
                return new SecurePage(index + 1);
              },
            ),
          );
        },
      ),
    );
  }
}

class VerifiedPage extends StatelessWidget {
  final int index;

  VerifiedPage(this.index);

  Widget build(BuildContext context) {
    return new Material(
      color: Colors.green,
      child: new InkWell(
        child: new Center(
          child: new Icon(
            Icons.verified_user,
            color: Colors.white,
            size: index * 100.0 + 20.0,
          ),
        ),
        onTap: () {
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (BuildContext context) {
                return new VerifiedPage(index + 1);
              },
            ),
          );
        },
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  int _page = 0;
  List<Widget> initialWidgets = <Widget>[
    new SecurePage(0),
    new VerifiedPage(0),
  ];

  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: new List<Widget>.generate(initialWidgets.length, (int index) {
          return new IgnorePointer(
            ignoring: index != _page,
            child: new Opacity(
              opacity: _page == index ? 1.0 : 0.0,
              child: new Navigator(
                onGenerateRoute: (RouteSettings settings) {
                  return new MaterialPageRoute(
                    builder: (_) => initialWidgets[index],
                  );
                },
              ),
            ),
          );
        }),
      ),
      bottomNavigationBar: new BottomNavigationBar(
        currentIndex: _page,
        onTap: (int index) {
          setState(() {
            _page = index;
          });
        },
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
            icon: new Icon(Icons.security),
            title: new Text('Secure'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.verified_user),
            title: new Text('Verified'),
          ),
        ],
      ),
    );
  }
}
2020-08-13