Install the Provider Package from Dart Pub the package manager for the Dart programming language, containing reusable libraries & packages
The Provider Package
A dependency injection system built with widgets for widgets. provider is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward.
this class contains state information and the appropriate methods to modify the state. When state updates happen, the call to notifyListeners is call to let the application know the the appropriate widgets need to be redrawn
Adding State Management to the Widget tree; we are using the MultiProvider here for demonstration purposes of how you could potentially have multiple classes managing different aspects of the application state. Each one would be added to the providers array property.
classMyAppextendsStatelessWidget{// This widget is the root of your application.@overrideWidgetbuild(BuildContextcontext){returnMultiProvider(child:MaterialApp(title:'Flutter Demo',theme:ThemeData(primarySwatch:Colors.teal,),home:MyHomePage(title:'Flutter Demo Home Page'),),providers:<SingleChildCloneableWidget>[ChangeNotifierProvider<DrawerStateInfo>(builder:(_)=>DrawerStateInfo()),],);}}
Creating The Drawer Widget for reuse in application. We are tracking the state to specify which item is the active item in the menu and then based on that we are updating the styling of the menu item's title
Use of Drawer in one of your pages; when using the Drawer, we specify the name of the Drawer when creating the instance. This should probably be a constant or maybe even an object containing addition information but this works for the example.
example flutter app showing a drawer and the master-detail page structure with state management
Flutter Drawer with State Management
A new Flutter project demonstrating a pattern for managing state between the drawers in the application and using one widget to represent the drawer across multiple application pages
A dependency injection system built with widgets for widgets. provider is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward.
For help getting started with Flutter, view the online documentation, which offers tutorials,
samples, guidance on mobile development, and a full API reference.