2017-09-29 6 views
3

persistent bottom barを実装している間に、前のroute need to be restoredbottom barのボタンをクリックしたときにクリックしました。Flutterで現在のルートパスを取得するには?

ボトムバーのボタンをクリックすると、現在のルートパス(/a/b/c)が保存され、ボタンクリックに応じて以前に保存されたルートが復元されます。

概念的には、各ボタンはワークスペースとみなされ、その状態は決して失われません(バックスタックを含む)。ユーザーはあるワークスペースから別のワークスペースに安全に切り替えることができます。

ルーティングがrewinding to rootの場合、Flutterで現在のルートパスを取得するにはどうすればよいですか?

答えて

2

NavigatorStateは、現在のルートのパスを取得するためのAPIを公開していません。Routeは、ルートのパスを決定するAPIも公開していません。ルートは匿名(しばしば)であることがあります。与えられたRouteが今すぐisCurrentメソッドを使ってナビゲータスタックの一番上にあるかどうかを調べることができますが、これはあなたのユースケースではあまり便利ではありません。

この問題に対して別のアプローチをとって、ルートにまったく巻き戻さないことをお勧めします。代わりに、BottomNavigationBarの各ペインに異なるNavigatorウィジェットを使用してください。こうすることで、ペインの切り替え時にスタックを巻き戻す必要がなくなります。 NavigatorウィジェットをOpacityIgnorePointerウィジェットにラップして、スタックを破壊せずに表示されないようにする必要がない場合はウィジェットを非表示にすることができます。

app video

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

うわー!美化する。この解決策は、私が持っている他の状態の問題も解決します。 –

+0

実際には、私は 'Stack'と' Opacity'を回っていましたが、動作させることはできませんでした。私は「ナビゲータ」がローカライズできることを知らなかった。 'IgnorePointer'は私が把握できなかった別のトリックです。この2つを組み合わせることで、ビルドイン[Hide-Show Widget](https://stackoverflow.com/questions/44489804/show-hide-widgets-on-flutter-programmatically)は、それらを発見可能にするのに良いでしょう。 –

+0

この素晴らしいソリューションには[その他の問題](https://stackoverflow.com/questions/46500186/flutter-keyboard-issue-when-wrap-under-stack-opacity-scrollable)があります。 –

関連する問題