How to get current route path in Flutter?

后端 未结 4 1954
长情又很酷
长情又很酷 2020-12-02 10:13

While implementing persistent bottom bar, previous route need to be restored when a button in the bottom bar was clicked.

When a button in the bottom bar is clicked

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 10:48

    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.

    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 {
      int _page = 0;
      List initialWidgets = [
        new SecurePage(0),
        new VerifiedPage(0),
      ];
    
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Stack(
            children: new List.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: [
              new BottomNavigationBarItem(
                icon: new Icon(Icons.security),
                title: new Text('Secure'),
              ),
              new BottomNavigationBarItem(
                icon: new Icon(Icons.verified_user),
                title: new Text('Verified'),
              ),
            ],
          ),
        );
      }
    }
    

提交回复
热议问题