Flutter Navigation2.0 with named route loading bar issue - flutter

ElevatedButton(
onPressed: () => Navigator.pushNamed(
context, '/addFood'),
child: Text('I Have Food'))
The above navigation push causing the loading bar on the top. This caused after 2.5 flutter update!. Can't figure out how to resolve the issue.
PS : The loading bar appears to be new and whenever chrome loads the web page.

Looks like found out the issue!.
When I introduce scaffold, the loading bar gone away, anyway flutter team should provide this new web page loading bar.
MaterialApp(
debugShowCheckedModeBanner: true,
theme: appTheme,
home: SafeArea(
child: Scaffold(
body: BlocListener<AddfoodCubit, AddfoodState>(
listener: (context, state) {
// TODO: implement listener
},
child: CustomScrollView(
slivers: [**strong text**

Related

Two scaffolds into one page because of navigation in flutter

I have an issue with the navigation process in my Flutter app.
First, I have a home page that has a button. When I press search, I go to the search page, obviously, and the results will appear on the home page again.
If no results were found, a dialog would appear that says "Try again" and has an OK button.
I want to press the OK button to navigate to the search page again. But there is a problem with the scaffolds like the image below. How can I fix this?
And my home page code, for alert dialog is like this:
Widget build(BuildContext context) {
print(widget.selectedURL);
return MaterialApp(
home: Center(
child: FutureBuilder<List<Pet>>(
future: API.get_pets(widget.selectedURL),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Pet>? pet_data = snapshot.data;
print(pet_data?.length);
return Padding(
padding: const EdgeInsets.all(8.0),
child:(pet_data?.length == 0)
? AlertDialog(
title: const Text("Failed"),
alignment: Alignment.topCenter,
content: const Text(
'No Pets Found. Please try different search options.'),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const Search())),
)
],
)
: ListView.builder(
//showing the list of results
)
The problem is you are wrapping everything inside a MaterialApp.
MaterialApp defines a Navigator property, which means it should be at the top-level of your application and it should be unique. If you have two different MaterialApp they will use their own Navigator, hence they will not share routes. See more informations here MaterialApp.
The usual design is:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: YourWidgets(
[...]
)//YourWidgets
)//Scaffold
);//MaterialApp
}
Try removing MaterialApp, or changing your MaterialApp to something else, like Material if you really need additional customization.

Flutter navigator doesn't show back button on App bar

I'm new to flutter and I'm Having a problem with Navigator , I don't know why it doesn't show back button on the next page (i want to show a back button on the next page app bar) when I use Navigator.push even though I have seen a lot of videos that show the opposite, maybe someone will have the answer here
here's the code I'm Using:
In the appbar there is leading property...
In that leading property you can use any Widget. As your requirement you can use icon Widget to navigate
You have to code the back button in "HomePage" class. In Flutter, when you navigate from page A to page B, the page B will be above page B, something like a stack, but page A and B have to be build on their owns.
A solution that might resolve your problem is to use this code in "HomePage"
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: ElevatedButton(
child: Text('Back Button'),
onPressed: (){
Navigator.pop();
},
),
),
);
Navigator.push(context, MaterialPageRoute( builder: (context) => CardScreen() ));
Make sure you are using Navigator.push instead of Navigator.pushReplacement

Flutter - keeping Drawer after page navigation

I have a Drawer widget on main.dart. The Drawer works great on this page, but when I navigate to a different page, using code like below, I lose the the drawer on the new page. Is there any recommended way to keep the drawer after navigating to a separate page?
Navigator.push(context, new MaterialPageRoute(builder: (context) => new MessageEntryPage()));
I saw similar questions in stackoverflow but couldn't get an answer.
not use Navigator.push
instance of body change
for example
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: your drawer ,
body: // your Widget
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
When we change the drawer, we use setState to change the widget on the body

How to intercept back button in AppBar in flutter

I am using interceptor https://pub.dartlang.org/packages/back_button_interceptor to execute a method when the page 1 is back from page 2.
If I come back from page 2 to page 1 using device back button, the method is executed.
But if I come back from page 2 to page 1 using the arrow button at appBar I am not able to execute the method.
How can the back arrow button functionality default to the device back button?
You can surround your scaffold on Page 2 with WillPopScope, set onWillPop to false to prevent the page from being popped by the system and then add your own back button into the app bar's leading widget and perform your pop in there.
#override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: new Scaffold(
appBar: new AppBar(
title: new Text("data"),
leading: new IconButton(
icon: new Icon(Icons.ac_unit),
onPressed: () => Navigator.of(context).pop(),
),
),
),
);
}
code for the answer from this post
Edit: Addition to Page 2 to control navigation
In addition to the above code you'll add the below code to page 2. Change
Navigator.of(context).pop()
to
Navigator.of(context).pop('upload_files')
Then in your page 1 where you navigate you'll await the navigation and use the result returned from the pop on page 2 and run your logic
var navigationResult = await Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Page2()));
if(navigationResult == 'upload_files') {
uploadFiles(); // Perform your custom functionality here.
}
The default back button in AppBar is BackButton widget from material.dart. You may create it manually and pass your own onPressed to do what you want:
return Scaffold(
appBar: AppBar(
leading: BackButton(onPressed: _onBackPressed),
title: Text('Title'),
),
body: Container(),
);
If you do not specify leading in AppBar, then it creates a BackButton with the handler that does Navigator.maybePop(context).

FullscreenDialog screen not covering bottomnavigationbar

How do I create a fullscreenDialog that covers my bottomnavigationbar?
My mainscreen looks like this, where I have a bottomnavigationbar which navigates to three different screens.
#override
Widget build(BuildContext context) {
return new Scaffold(
body: PageView(
children: [new HomeTab(), new PresentationsTab(), new TestTab()],
controller: _pageController,
onPageChanged: pageChanged,
),
bottomNavigationBar: new BottomNavigationBar(
currentIndex: _page,
onTap: tapBottomNav,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text('Home'),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.pregnant_woman),
title: new Text('Presentation'),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.pregnant_woman),
title: new Text('Presentation'),
)
],
),
);
}
And somewhere I have a screen which navigates to another screen with the fullscreenDialog flag set to true like this.
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new AddAudio(),
fullscreenDialog: true,
),
);
On my appbar I can see that the flag actually works because my backbutton arrow will become an x, but my bottomnavigationbar will still be visible, how do I resolve this?
I have an answer for you on my post: Flutter MaterialPageRoute as fullscreenDialog appears underneath BottomNavigationBar
But, to recap for you:
I assume you are presenting your modal from a page which is itself a Scaffold? In which case you have nested Scaffold objects, and this is where the problem lies. To ensure your modal appears above the BottomNavigationBar you need to include that widget in the child Scaffold and not in the root one.
This seems like an utter pain in the arse, and not a great solution with loads of boilerplate and code duplication; but it isn't. You just need to build a custom composition widget for BottomNavigationBar and then use ChangeNotifier with a state class to manage state. I've explained it in the post I reference above, and it is probably better to read the answer there so you understand the context.
That is probably the expected behavior, you should push a PageRoute (with a PageRouteBuilder) and use a custom transition to have the bottom to top slide animation.