Let's change the PageOne widget above by wrapping it as the child of a ScaffoldMessenger and leave the PageTwo widget as it is now. But got Duplicate GlobalKey detected in widget tree. If you want that behavior, you can instantiate your own ScaffoldMessenger instead of using the MaterialApp's root ScaffoldMessenger. Flutter Scaffold is used to display a basic material design layout that contains application bar, body, bottom navigation bar, bottom sheet, drawer. For example, you want to show the SnackBar from PageOne on that page only and not displayed when the user is on PageTwo, even if the display duration has not over yet. What if you want to display a SnackBar on particular Scaffolds only. ScaffoldMessenger.of(context).removeCurrentSnackBar( To try the above method, hust add the button below as another child of the Column widget on the PageTwo. Retrieve a ScaffoldState from the current BuildContext using Scaffold.of. As soon as the current SnackBar has been removed, the next one will be shown if the queue is not empty. description ScaffoldState class State for a Scaffold. It also supports the optional reason argument whose default value is remove. import 'package:flutter/material.dart' Ĭlass MyApp extends StatelessWidget build(BuildContext context) ) As the result, a SnackBar triggered from PageOne is also displayed on PageTwo and vice versa. Both pages use the ScaffoldMessenger from the MaterialApp (instead of creating a new one). In this example, display the SnackBar at the bottom of the screen, without overlapping other important widgets, such as the FloatingActionButton. ScaffoldMessenger.of(context).showSnackBar(_snackBar) īelow is a simple application consisting of two pages: PageOne and PageTwo. Create a Scaffold When creating apps that follow the Material Design guidelines, give your apps a consistent visual structure. After that, you can call the showSnackBar method. ![]() ![]() That means the passed BuildContext must be below a Scaffold widget in the tree. It returns the state of the nearest instance that encloses the given context. To get the instance of ScaffoldMessengerState, you can use the method below static ScaffoldMessengerState of(BuildContext context) ScaffoldFeatureController showSnackBar(SnackBar snackBar) Using Scaffold.Content: const Text('Message from page one'),ĭisplaying a Snackbar can be done by calling the showSnackBar method of ScaffoldMessengerState. this.scaffoldKey, but this isn't recommended. If you absolutely have to use GlobalKeys for some reason, you would be better off passing the instance created in your outer widget into your Menu class as a member i.e. It is quick enough to create a general-purpose. And the state should really be failing analysis since you're passing the wrong type into your Drawer's key field. The Scaffold is a widget in Flutter used to implements the basic material design visual layout structure. This means that adding a Scaffold to each route on a Material app will provide the app with Materials basic visual layout structure. Think of them as global pointers - since you're creating two different ones, they point to different things. The Scaffold is designed to be a top level container for a MaterialApp. The reason what you're attempting to do doesn't work is that you are creating two seperate GlobalKeys - each of which is its own object. When building a Material app in Flutter, you can use the Scaffold widget to provide a default banner, background color, and API for adding drawers. ![]() Since your menu is below the scaffold in the widget tree, Scaffold.of(context) will do what you want. You should try to avoid using GlobalKey as much as possible you're almost always better off using Scaffold.of to get the ScaffoldState.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |