How to do nested navigation in Flutter - flutter

Does anyone have any recommendations for figuring out nested navigation in Flutter?
What I want is to keep a persistent BottomNavigationBar even when redirecting to new screens. Similar to YouTube, where the bottom bar is always there, even when you browse deeper into the menus.
I'm unable to figure it out from the docs.
The only tutorial I have been able to find so far that goes in-depth into exactly my requirement is https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf (source code). However, It's super confusing.
Right now I'm using
Navigator.push(context,
MaterialPageRoute(builder: (BuildContext context) {
return Container()
However, its just pushing the new widget over the entire stack, covoring the BottomNavigationBar.
Any tips would be greatly appreciated!

Here is a simple example that even supports popping to the first screen with a tab bar.
import 'package:flutter/material.dart';
import '../library/screen.dart';
import '../playlists/screen.dart';
import '../search/screen.dart';
import '../settings/screen.dart';
class TabsScreen extends StatefulWidget {
#override
_TabsScreenState createState() => _TabsScreenState();
}
class _TabsScreenState extends State<TabsScreen> {
int _currentIndex = 0;
final _libraryScreen = GlobalKey<NavigatorState>();
final _playlistScreen = GlobalKey<NavigatorState>();
final _searchScreen = GlobalKey<NavigatorState>();
final _settingsScreen = GlobalKey<NavigatorState>();
#override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: <Widget>[
Navigator(
key: _libraryScreen,
onGenerateRoute: (route) => MaterialPageRoute(
settings: route,
builder: (context) => LibraryScreen(),
),
),
Navigator(
key: _playlistScreen,
onGenerateRoute: (route) => MaterialPageRoute(
settings: route,
builder: (context) => PlaylistsScreen(),
),
),
Navigator(
key: _searchScreen,
onGenerateRoute: (route) => MaterialPageRoute(
settings: route,
builder: (context) => SearchScreen(),
),
),
Navigator(
key: _settingsScreen,
onGenerateRoute: (route) => MaterialPageRoute(
settings: route,
builder: (context) => SettingsScreen(),
),
),
],
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: (val) => _onTap(val, context),
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.library_books),
title: Text('Library'),
),
BottomNavigationBarItem(
icon: Icon(Icons.list),
title: Text('Playlists'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
);
}
void _onTap(int val, BuildContext context) {
if (_currentIndex == val) {
switch (val) {
case 0:
_libraryScreen.currentState.popUntil((route) => route.isFirst);
break;
case 1:
_playlistScreen.currentState.popUntil((route) => route.isFirst);
break;
case 2:
_searchScreen.currentState.popUntil((route) => route.isFirst);
break;
case 3:
_settingsScreen.currentState.popUntil((route) => route.isFirst);
break;
default:
}
} else {
if (mounted) {
setState(() {
_currentIndex = val;
});
}
}
}
}

Here is the example code for persistent BottomNavigationBar as a starter:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MainPage(),
);
}
}
class MainPage extends StatelessWidget {
final navigatorKey = GlobalKey<NavigatorState>();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: Navigator(
key: navigatorKey,
onGenerateRoute: (route) => MaterialPageRoute(
settings: route,
builder: (context) => PageOne(),
),
),
),
BottomNavigationBar(navigatorKey)
],
),
);
}
}
class BottomNavigationBar extends StatelessWidget {
final GlobalKey<NavigatorState> navigatorKey;
BottomNavigationBar(this.navigatorKey) : assert(navigatorKey != null);
Future<void> push(Route route) {
return navigatorKey.currentState.push(route);
}
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: ButtonBar(
alignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
child: Text("PageOne"),
onPressed: () {
push(MaterialPageRoute(builder: (context) => PageOne()));
},
),
RaisedButton(
child: Text("PageTwo"),
onPressed: () {
push(MaterialPageRoute(builder: (context) => PageTwo()));
},
)
],
),
);
}
}
class PageOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Page One"),
RaisedButton(
onPressed: (){
Navigator.of(context).pop();
},
child: Text("Pop"),
),
],
),
);
}
}
class PageTwo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Page Two"),
RaisedButton(
onPressed: (){
Navigator.of(context).pop();
},
child: Text("Pop"),
),
],
),
);
}
}
Here is how it the screen record

Related

The return type '_MainPage' isn't a 'Widget', as required by the closure's context

I create page with bottom_nav_bar, but now i cant open it in emulator, bcs error Error: A value of type '_MainPage' can't be assigned to a variable of type 'Widget'.
Problem here: '/Main': (BuildContext context) => _MainPage(),
Here code:
import 'package:flutter/material.dart';
class LogInPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Log In")),
body: Center(
child: Row(
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Log In")),
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/LogIn/Registr');
},
child: Text("Registration")),
],
)),
);
}
}
class QRCodePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("QR Scan")),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Scan")),
));
}
}
class RegistrPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Registration")),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Log In")),
));
}
}
class MyApp extends StatefulWidget {
#override
_MainPage createState() => _MainPage();
}
class _MainPage extends State<MyApp> {
int _currentIndex = 0;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Main")),
body: Container(),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.qr_code), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.restaurant_menu), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_basket), backgroundColor: Colors.blue),
],
onTap: (index){
setState(() {
_currentIndex = index;
});
},
),
);
}
}
void main()
{
runApp(MaterialApp(
initialRoute: '/LogIn',
routes: {
'/LogIn': (BuildContext context) => LogInPage(),
'/QRScan': (BuildContext context) => QRCodePage(),
'/LogIn/Registr': (BuildContext context) => RegistrPage(),
'/Main': (BuildContext context) => _MainPage(),
},
));
}
Shouldn't it be MyApp instead of _MainPage? _MainPage is not the Widget but the State.
void main() {
runApp(MaterialApp(
initialRoute: '/Main',
routes: {
'/LogIn': (BuildContext context) => LogInPage(),
'/QRScan': (BuildContext context) => QRCodePage(),
'/LogIn/Registr': (BuildContext context) => RegistrPage(),
'/Main': (BuildContext context) => MyApp(),
},
));
}
You should probably rename MyApp into MainPage for more clarity.
Also, you have to define the label or title of each BottomNavigationBarItem:
items: [
BottomNavigationBarItem(
label: 'Home',
icon: Icon(Icons.home),
backgroundColor: Colors.blue
),
...
],

How to navigate to a different page on a different tab using CupertinoTabBar?

I have two tabs - Tab1 and Tab2. Tab1 has 3 pages - Tab1 Page1, Tab1 Page2, Tab1 Page3.
I want to be able to navigate from Tab2 to Tab1 Page2. I can switch the index using controller.index = 0 but am not sure how to navigate to to Page2 of this tab. What is a clean solution for this?
// main.dart
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cupertino Tab Bar Demo',
theme: ThemeData(primarySwatch: Colors.blue, textTheme: TextTheme()),
home: SafeArea(child: Scaffold(body: MyHomePage('Cupertino Tab Bar Demo Home Page'))),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage(this.title);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var navigatorKeyList = [GlobalKey<NavigatorState>(), GlobalKey<NavigatorState>()];
var currentIndex = 0;
var controller = CupertinoTabController(initialIndex: 0);
#override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
controller: controller,
tabBar: CupertinoTabBar(
onTap: (index) {
if (currentIndex == index) {
// Navigate to the tab's root route
navigatorKeyList[index].currentState.popUntil((route) {
return route.isFirst;
});
}
currentIndex = index;
},
items: [
BottomNavigationBarItem(title: Text('Tab 1'), icon: Icon(Icons.ac_unit)),
BottomNavigationBarItem(title: Text('Tab 2'), icon: Icon(Icons.ac_unit)),
],
),
tabBuilder: (BuildContext _, int index) {
switch (index) {
case 0:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page1(),
onWillPop: () => Future<bool>.value(true),
),
'page1b': (context) => Page1b(),
'page1c': (context) => Page1c(),
},
);
case 1:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page2(controller),
onWillPop: () => Future<bool>.value(true),
)
},
);
default:
return Text('Index must be less than 2');
}
},
);
}
}
// Tab1 Page1
class Page1 extends StatelessWidget {
Page1();
#override
Widget build(BuildContext context) {
return BaseContainer(
Column(
children: <Widget>[
Container(
child: Text(
'Tab1',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2'),
onPressed: () {
Navigator.pushNamed(context, 'page1b');
},
)
],
),
);
}
}
// Tab1 Page2
class Page1b extends StatelessWidget {
Page1b();
#override
Widget build(BuildContext context) {
return BaseContainer(
Column(
children: <Widget>[
Container(
child:
Text('Tab1 Page2', style: Theme.of(context).textTheme.headline4),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page3'),
onPressed: () {
Navigator.pushNamed(context, 'page1c');
},
)
],
),
);
}
}
// Tab2
class Page2 extends StatelessWidget {
final CupertinoTabController controller;
const Page2(this.controller);
#override
Widget build(BuildContext context) {
return BaseContainer(
Column(
children: <Widget>[
Container(
child: Text(
'Tab2',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2 (TODO)'),
onPressed: () {
// TODO I want this to go to Tab1 Page2
this.controller.index = 0;
Navigator.of(context).pushNamed('page1b');
},
)
],
),
);
}
}
You can copy paste run full code below
Step 1: You can pass navigatorKeyList[0]) to Page2
Step 2: call navigatorKey.currentState.pushNamed("page1b"); and change index
code snippet
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page2(controller, navigatorKeyList[0]),
onWillPop: () => Future<bool>.value(true),
)
},
);
...
class Page2 extends StatelessWidget {
final CupertinoTabController controller;
final GlobalKey<NavigatorState> navigatorKey;
const Page2(this.controller, this.navigatorKey);
...
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2 (TODO)'),
onPressed: () {
navigatorKey.currentState.pushNamed("page1b");
this.controller.index = 0;
},
)
working demo
full code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cupertino Tab Bar Demo',
theme: ThemeData(primarySwatch: Colors.blue, textTheme: TextTheme()),
home: SafeArea(
child:
Scaffold(body: MyHomePage('Cupertino Tab Bar Demo Home Page'))),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage(this.title);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var navigatorKeyList = [
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>()
];
var currentIndex = 0;
var controller = CupertinoTabController(initialIndex: 0);
#override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
controller: controller,
tabBar: CupertinoTabBar(
onTap: (index) {
if (currentIndex == index) {
// Navigate to the tab's root route
navigatorKeyList[index].currentState.popUntil((route) {
return route.isFirst;
});
}
currentIndex = index;
},
items: [
BottomNavigationBarItem(
title: Text('Tab 1'), icon: Icon(Icons.ac_unit)),
BottomNavigationBarItem(
title: Text('Tab 2'), icon: Icon(Icons.ac_unit)),
],
),
tabBuilder: (BuildContext _, int index) {
switch (index) {
case 0:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page1(),
onWillPop: () => Future<bool>.value(true),
),
'page1b': (context) => Page1b(),
'page1c': (context) => Page1c(),
},
);
case 1:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page2(controller, navigatorKeyList[0]),
onWillPop: () => Future<bool>.value(true),
)
},
);
default:
return Text('Index must be less than 2');
}
},
);
}
}
// Tab1 Page1
class Page1 extends StatelessWidget {
Page1();
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text(
'Tab1',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2'),
onPressed: () {
Navigator.pushNamed(context, 'page1b');
},
)
],
),
);
}
}
// Tab1 Page2
class Page1b extends StatelessWidget {
Page1b();
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text('Tab1 Page2',
style: Theme.of(context).textTheme.headline4),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page3'),
onPressed: () {
Navigator.pushNamed(context, 'page1c');
},
)
],
),
);
}
}
class Page1c extends StatelessWidget {
Page1c();
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text('Tab1 Page2',
style: Theme.of(context).textTheme.headline4),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page3'),
onPressed: () {
Navigator.pushNamed(context, 'page1c');
},
)
],
),
);
}
}
// Tab2
class Page2 extends StatelessWidget {
final CupertinoTabController controller;
final GlobalKey<NavigatorState> navigatorKey;
const Page2(this.controller, this.navigatorKey);
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text(
'Tab2',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2 (TODO)'),
onPressed: () {
navigatorKey.currentState.pushNamed("page1b");
this.controller.index = 0;
},
)
],
),
);
}
}
full code 2
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cupertino Tab Bar Demo',
theme: ThemeData(primarySwatch: Colors.blue, textTheme: TextTheme()),
home: SafeArea(
child:
Scaffold(body: MyHomePage('Cupertino Tab Bar Demo Home Page'))),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage(this.title);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var navigatorKeyList = [
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>()
];
int currentIndex = 0;
var controller = CupertinoTabController(initialIndex: 0);
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
controller: controller,
tabBar: CupertinoTabBar(
onTap: (index) {
if (currentIndex == index) {
// Navigate to the tab's root route
navigatorKeyList[index].currentState.popUntil((route) {
return route.isFirst;
});
}
currentIndex = index;
},
items: [
BottomNavigationBarItem(
title: Text('Tab 1'), icon: Icon(Icons.ac_unit)),
BottomNavigationBarItem(
title: Text('Tab 2'), icon: Icon(Icons.ac_unit)),
],
),
tabBuilder: (BuildContext _, int index) {
switch (index) {
case 0:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page1(controller, navigatorKeyList[1]),
onWillPop: () => Future<bool>.value(true),
),
'page1b': (context) => Page1b(),
'page1c': (context) => Page1c(),
},
);
case 1:
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page2(controller, navigatorKeyList[0]),
onWillPop: () => Future<bool>.value(true),
)
},
);
default:
return Text('Index must be less than 2');
}
},
);
}
}
// Tab1 Page1
class Page1 extends StatelessWidget {
final CupertinoTabController controller;
final GlobalKey<NavigatorState> navigatorKey;
const Page1(this.controller, this.navigatorKey);
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text(
'Tab1',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab2 Page2'),
onPressed: () async {
controller.index = 1;
await Future.delayed(Duration(seconds: 1), () {});
navigatorKey.currentState.pushNamed("/");
},
)
],
),
);
}
}
// Tab1 Page2
class Page1b extends StatelessWidget {
Page1b();
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text('Tab1 Page2',
style: Theme.of(context).textTheme.headline4),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page3'),
onPressed: () {
Navigator.pushNamed(context, 'page1c');
},
)
],
),
);
}
}
class Page1c extends StatelessWidget {
Page1c();
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text('Tab1 Page2',
style: Theme.of(context).textTheme.headline4),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page3'),
onPressed: () {
Navigator.pushNamed(context, 'page1c');
},
)
],
),
);
}
}
// Tab2
class Page2 extends StatelessWidget {
final CupertinoTabController controller;
final GlobalKey<NavigatorState> navigatorKey;
const Page2(this.controller, this.navigatorKey);
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Text(
'Tab2',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab1 Page2 (TODO)'),
onPressed: () async {
navigatorKey.currentState.pushNamed("page1b");
await Future.delayed(Duration(seconds: 1), () {});
this.controller.index = 0;
},
)
],
),
);
}
}
full code 3
return CupertinoTabView(
navigatorKey: navigatorKeyList[index],
routes: {
'/': (context) => WillPopScope(
child: Page1(controller, navigatorKeyList),
onWillPop: () => Future<bool>.value(true),
),
'page2b': (context) => Page2b(),
'page3b': (context) => Page3b(),
},
);
...
import 'package:cupertino_tab_bar/base_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Page1 extends StatelessWidget {
final CupertinoTabController controller;
final List<GlobalKey<NavigatorState>> navigatorKeyList;
const Page1(this.controller, this.navigatorKeyList);
#override
Widget build(BuildContext context) {
return BaseContainer(
Column(
children: <Widget>[
Container(
child: Text(
'Tab1',
style: Theme.of(context).textTheme.headline4,
),
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab2 Page2'),
onPressed: () async{
controller.index = 1;
await Future.delayed(Duration(seconds: 1), () {});
navigatorKeyList[1].currentState.pushNamed("page2b");
},
),
FlatButton(
color: Colors.lightGreen,
child: Text('Go to Tab3 Page2'),
onPressed: () async{
controller.index = 2;
await Future.delayed(Duration(seconds: 1), () {});
navigatorKeyList[2].currentState.pushNamed("page3b");Navigator.pushNamed(context, 'page3b');
},
)
],
),
);
}
}

What is the use of `rootNavigator` in Navigator.of(context, rootNavigator: true).push();

What's the difference between
Navigator.of(context).pushNamed("/route");
and
Navigator.of(context, rootNavigator: true).pushNamed("/route");
More importantly, what's the use of setting rootNavigator: true on Navigator class, I read docs but they aren't quite clear. Can anyone explain the difference properly?
You can copy paste run full code below
There is a root Navigator above tab navigation
This demo shows open(Navigator.push) a full screen dialog (fullscreenDialog: true) with rootNavigator true/false
picture
rootNavigator = true , fullscreenDialog take all screen and above tab
rootNavigator = false, fullscreenDialog take tab size and inside tab, you can switch between Home and Support tab and see fullscreenDialog is still there
working demo
code snippet
Center(
child: CupertinoButton(
child: const Text(
'Push rootNavigator true',
),
onPressed: () {
Navigator.of(context, rootNavigator: true).push(
CupertinoPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => Tab3Dialog(),
),
);
},
),
),
Center(
child: CupertinoButton(
child: const Text(
'Push rootNavigator false',
),
onPressed: () {
Navigator.of(context, rootNavigator: false).push(
CupertinoPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => Tab3Dialog(),
),
);
},
),
),
full code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PawzHome(),
);
}
}
class PawzHome extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.conversation_bubble),
title: Text('Support'),
),
],
),
tabBuilder: (BuildContext context, int index) {
switch (index) {
case 0:
return CupertinoTabView(
builder: (BuildContext context) {
return CupertinoDemoTab1();
},
defaultTitle: 'Colors',
);
break;
case 1:
return CupertinoTabView(
builder: (BuildContext context) => CupertinoDemoTab2(),
defaultTitle: 'Support Chat',
);
break;
}
return null;
},
);
}
}
class CupertinoDemoTab1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: CustomScrollView(
slivers: <Widget>[
CupertinoSliverNavigationBar(),
SliverList(
delegate: SliverChildListDelegate([Tab1RowItem()]),
),
],
),
);
}
}
class Tab1RowItem extends StatelessWidget {
#override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).push(CupertinoPageRoute<void>(
title: "Click me",
builder: (BuildContext context) => Tab1ItemPage(),
));
},
child: Padding(padding: EdgeInsets.all(10.0), child: Text("Click me")),
);
}
}
class Tab1ItemPage extends StatelessWidget {
#override
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(),
child: Container(
child: Column(
children: <Widget>[
SizedBox(height: 100,),
Center(
child: CupertinoButton(
child: const Text(
'Push rootNavigator true',
),
onPressed: () {
Navigator.of(context, rootNavigator: true).push(
CupertinoPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => Tab3Dialog(),
),
);
},
),
),
Center(
child: CupertinoButton(
child: const Text(
'Push rootNavigator false',
),
onPressed: () {
Navigator.of(context, rootNavigator: false).push(
CupertinoPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => Tab3Dialog(),
),
);
},
),
),
],
),
));
}
}
class CupertinoDemoTab2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(),
child: Container(
child: Center(
child: Text("Tab 2"),
),
));
}
}
class Tab3Dialog extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
leading: CupertinoButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: Text("Ok"),
),
),
child: Center(
child: CupertinoButton(
color: CupertinoColors.activeBlue,
child: const Text('Sign in'),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
}
If your app has nested navigators this parameter comes in handy when you want to call the root navigator not it's nested navigators ... please consider the image bellow ... in this example we are inside the page 2 and we want the page 3 to be the rootNavigator's child (because for example we want to ignore the bottomNavigationBar that is in MainPage)... in this example if you don't set the rootNavigator = true and you push the page 3 it will be the nestedNavigator's child (So the BottomNavigationBar of the MainPage's will be still visible).

Flutter Navigator.popUntil failed assertion _debugLocked. What's wrong?

^ ^ ^^ ^ NO! THIS QUESTION DOES NOT HAVE AN ANSWER THERE! ^ ^ ^ ^ ^ ^
I'm having problems with Navigator.popUntil. I wrote a small demo app to show what's happening. Before I post this as a bug, am I using popUntil wrong??
call to popUntil displays
It looks like something is locking up the Navigator (setting _debugLocked) and not releasing it.
main.dart below : (can just be pasted into the Flutter demo app)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Routing Test Page'),
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Home Page',),
settings: settings,
);
case '/home':
return MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Home Page',),
settings: settings,
);
case '/middlepage':
return MaterialPageRoute(
builder: (_) => MiddlePage(),
settings: settings,
);
case '/bottompage':
return MaterialPageRoute(
builder: (_) => BottomBage(),
settings: settings,
);
default:
return null;
}
},
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Home Page',
),
OutlineButton(
child: Text('push MiddlePage()'),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => (MiddlePage())),
),
),
OutlineButton(
child: Text('pushNamed ''/middlepage'''),
onPressed: () => Navigator.pushNamed(context, '/middlepage'),
),
],
),
),
);
}
}
class MiddlePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Middle Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Middle Page',style: TextStyle(fontWeight: FontWeight.bold)
),
OutlineButton(
child: Text('push BottomPage()'),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => (BottomBage())),
),
),
OutlineButton(
child: Text('pushNamed ''/bottompage'''),
onPressed: () => Navigator.pushNamed(context, '/bottompage'),
),
OutlineButton(
child: Text('pop'), onPressed: () => Navigator.pop(context)),
OutlineButton(
child: Text('popUntil ''/home'''),
onPressed: () =>
Navigator.popUntil(context, ModalRoute.withName('/home'))),
],
),
),
);
}
}
class BottomBage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bottom page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Bottom Page', style: TextStyle(fontWeight: FontWeight.bold),
),
OutlineButton(
child: Text('push ''home'''),
onPressed: () =>
Navigator.pushNamed(context, '/home'),
),
OutlineButton(
child: Text('pop'),
onPressed: () =>
Navigator.pop(context)),
OutlineButton(
child: Text('popUntil ''/home'''),
onPressed: () =>
Navigator.popUntil(context, ModalRoute.withName('/home')),
),
OutlineButton(
child: Text('popUntil ''/home'' (with Are You Sure box)) '),
onPressed: () async {
try {
if (await _areYouSureDialog(context)) {
Navigator.popUntil(context, ModalRoute.withName('/home'));
}
} catch (e) {
debugPrint("exception: $e");
}
},
),
],
),
),
);
}
}
Future<bool> _areYouSureDialog(BuildContext context) async {
return await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Pop back?'),
content: const Text('Are you sure?'),
actions: <Widget>[
FlatButton(
child: const Text('YES'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
FlatButton(
child: const Text('NO'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
],
);
},
) ??
false;
}
I recreated your case. Whenever we want to pop navigation to the home screen (root) from anywhere, there are couple of ways to do it as below:
1.Using .isFirst method:
Navigator.of(context).popUntil((route) => route.isFirst);
Using defaultRouteName:
Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName));
By providing the context first, the route will ensure that the navigation will pop to the default always.
You can try with either approach. I tested at my end and it works well.
Hope this answers your question.

How to use Navigator.popUntil Flutter

I m doing a Flutter app and I would like to go back from page 4 to page 1.
I have an error really strange :
Bad state : Future already completed
I created a simple project to reproduce this bug :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
onGenerateRoute: routes,
);
}
}
Route routes(RouteSettings settings) {
if (settings.name == '/page1') {
return MaterialPageRoute(
builder: (context) {
return Page1();
},
);
} else if (settings.name == '/page2') {
return MaterialPageRoute(
builder: (context) {
return Page2();
},
);
} else if (settings.name == '/page3') {
return MaterialPageRoute(
builder: (context) {
return Page3();
},
);
} else if (settings.name == '/page4') {
return MaterialPageRoute(
builder: (context) {
return Page4();
},
);
} else {
return MaterialPageRoute(
builder: (context) {
return Page1();
},
);
}
}
class Page1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Page 1'),
RaisedButton(
child: Text('Go Page 2'),
onPressed: () {
Navigator.of(context).pushNamed('/page2');
},
)
],
),
),
);
}
}
class Page2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Page 2'),
RaisedButton(
child: Text('Go Page 3'),
onPressed: () {
Navigator.of(context).pushNamed('/page3');
},
)
],
),
),
);
}
}
class Page3 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Page 3'),
RaisedButton(
child: Text('Go Page 4'),
onPressed: () {
Navigator.of(context).pushNamed('/page4');
},
)
],
),
),
);
}
}
class Page4 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Page 4'),
RaisedButton(
child: Text('Go Back Page 1'),
onPressed: () {
Navigator.of(context).popUntil(ModalRoute.withName('/page1'));
},
)
],
),
),
);
}
}
How can I solve that ?
Instead of:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Page1(),
),
);
use:
Navigator.of(context).push(
MaterialPageRoute(
settings: RouteSettings(name: "/Page1"),
builder: (context) => Page1(),
),
);
and then you can use :
Navigator.of(context)
.popUntil(ModalRoute.withName("/Page1"));
It is duplicate question. Refer this and this.
Basically what is happening is - when you start your app, page1 opens because it goes into the last else and there is no name assigned to it, so when you do popuntil that page name, it doesn't find it at all.
For page_transition plugin
In case you are using named Routes and this Package for transitions between pages (and arguments):
MaterialApp(
onGenerateRoute: (settings) => {
switch (settings.name) {
case "/yourRoute":
final value = settings.arguments as String?; // only if you pass arguments
return PageTransition(
settings: RouteSettings(
name: "/yourRoute", //HERE is where you name your route for using popUntil
),
child: YourPage(
parameters: value ?? "null",
),
type: PageTransitionType.fade,
);
case "/yourNextRoute":
...
}
}
),
Edit your main like this to enable calling and using pop with named routes. This would look like this:
Navigator.popUntil(context, ModalRoute.withName("/yourRoute")) or Navigator.pushNamed(context, "/yourRoute",arguments: "12345")