如何在Flutter中获取当前路线路径? [英] How to get current route path in Flutter?

查看:3558
本文介绍了如何在Flutter中获取当前路线路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实施永久性底部栏时,以前的单击底部栏中的按钮时,其当前路径路径(/a/b/c )已保存,并通过单击按钮恢复了先前保存的路线.

When a button in the bottom bar is clicked, its current route path (/a/b/c) is saved and previously saved route is restored according to the button click.

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

Conceptually user will think each button as a workspace and its state is never get lost (including back stack). User can safely switch from one workspace to another.

当路由为倒回到根目录时,如何在Flutter中获取当前路由路径?

推荐答案

NavigatorState不公开用于获取当前路线路径的API,并且Route不公开用于确定路线路径的API任何一个.路由可以是(通常是匿名的).您可以使用

NavigatorState doesn't expose an API for getting the path of the current route, and Route doesn't expose an API for determining a route's path either. Routes can be (and often are) anonymous. You can find out if a given Route is on the top of the navigator stack right now using the isCurrent method, but this isn't very convenient for your use case.

我建议您对这个问题采用其他方法,并且不要完全退回到根源.而是,对BottomNavigationBar的每个窗格使用不同的Navigator小部件.这样,您在窗格之间切换时就不必倒回堆栈.您可以将Navigator小部件包装在 Opacity IgnorePointer 小部件可隐藏它们,以防止它们在不可见的情况下不可见破坏他们的堆栈.

I would recommend that you take a different approach to this problem and don't rewind to the root at all. Instead, use a different Navigator widget for each pane of the BottomNavigationBar. That way, you won't have to rewind the stack when switching between panes. You can wrap your Navigator widgets in Opacity and IgnorePointer widgets to hide them when they aren't supposed to be visible without destroying their stack.

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'),
          ),
        ],
      ),
    );
  }
}

这篇关于如何在Flutter中获取当前路线路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆