NavigatorState
は、現在のルートのパスを取得するためのAPIを公開していません。Route
は、ルートのパスを決定するAPIも公開していません。ルートは匿名(しばしば)であることがあります。与えられたRoute
が今すぐisCurrent
メソッドを使ってナビゲータスタックの一番上にあるかどうかを調べることができますが、これはあなたのユースケースではあまり便利ではありません。
この問題に対して別のアプローチをとって、ルートにまったく巻き戻さないことをお勧めします。代わりに、BottomNavigationBar
の各ペインに異なるNavigator
ウィジェットを使用してください。こうすることで、ペインの切り替え時にスタックを巻き戻す必要がなくなります。 Navigator
ウィジェットをOpacity
とIgnorePointer
ウィジェットにラップして、スタックを破壊せずに表示されないようにする必要がない場合はウィジェットを非表示にすることができます。
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'),
),
],
),
);
}
}
うわー!美化する。この解決策は、私が持っている他の状態の問題も解決します。 –
実際には、私は 'Stack'と' Opacity'を回っていましたが、動作させることはできませんでした。私は「ナビゲータ」がローカライズできることを知らなかった。 'IgnorePointer'は私が把握できなかった別のトリックです。この2つを組み合わせることで、ビルドイン[Hide-Show Widget](https://stackoverflow.com/questions/44489804/show-hide-widgets-on-flutter-programmatically)は、それらを発見可能にするのに良いでしょう。 –
この素晴らしいソリューションには[その他の問題](https://stackoverflow.com/questions/46500186/flutter-keyboard-issue-when-wrap-under-stack-opacity-scrollable)があります。 –