import'package:flutter/material.dart';voidmain(){runApp(constMyApp());}classMyAppextendsStatelessWidget{constMyApp({super.key});// This widget is the root of your application.@overrideWidgetbuild(BuildContextcontext){returnMaterialApp(title:'Flutter Demo',theme:ThemeData(colorScheme:ColorScheme.fromSeed(seedColor:Colors.deepPurple),),home:MyHomePage(title:'Flutter Demo Home Page'),);}}classMyHomePageextendsStatefulWidget{MyHomePage({super.key,requiredthis.title});finalStringtitle;intcount=0;@overrideState<MyHomePage>createState()=>_MyHomePageState();}class_MyHomePageStateextendsState<MyHomePage>{void_incrementCounter(){setState((){//#widget.count++;});}@overrideWidgetbuild(BuildContextcontext){returnScaffold(appBar:AppBar(backgroundColor:Theme.of(context).colorScheme.inversePrimary,title:Text(widget.title),),body:Center(child:Column(mainAxisAlignment:MainAxisAlignment.center,children:<Widget>[constText('You have pushed the button this many times:'),Text('${widget.count}',style:Theme.of(context).textTheme.headlineMedium,),],),),floatingActionButton:FloatingActionButton(onPressed:_incrementCounter,tooltip:'Increment',child:constIcon(Icons.add),),// This trailing comma makes auto-formatting nicer for build methods.);}}
import'package:counter_app_sm/providers/count_provider.dart';import'package:flutter/material.dart';import'package:flutter_riverpod/flutter_riverpod.dart';voidmain(){runApp(ProviderScope(child: MyApp()));}classMyAppextendsStatelessWidget{constMyApp({super.key});// This widget is the root of your application.@overrideWidgetbuild(BuildContextcontext){returnMaterialApp(title:'Flutter Demo',theme:ThemeData(colorScheme:ColorScheme.fromSeed(seedColor:Colors.deepPurple),),home:MyHomePage(),);}}classMyHomePageextends ConsumerWidget{constMyHomePage({super.key});@overrideWidgetbuild(BuildContextcontext,WidgetRef ref){int count = ref.watch(countProvider);returnScaffold(appBar:AppBar(backgroundColor:Theme.of(context).colorScheme.inversePrimary,title:Text("Counter App"),),body:Center(child:Column(mainAxisAlignment:MainAxisAlignment.center,children:<Widget>[constText('You have pushed the button this many times:'),Text('$count',style:Theme.of(context).textTheme.headlineMedium),],),),floatingActionButton:FloatingActionButton(onPressed:ref.read(countProvider.notifier).incrementCounter,// (1)tooltip:'Increment',child:constIcon(Icons.add),),// This trailing comma makes auto-formatting nicer for build methods.);}}
This is how functions of a StateNotifier class are accessed.
note how the (ref) {} passed to StateNotifierProvider constructor
returned a StateNotifier extending class, but for a Provider constructor
the (ref) {} returns the value.
The value that is passed to super() constructor becomes the initial value of
the state. The date type of the state can be seen in extends part: ... extends
StateNotifierProvider<int>
classTabsScreenextendsStatefulWidget{constTabsScreen({super.key});@overrideState<TabsScreen>createState(){return_TabsScreenState();}}class_TabsScreenStateextendsState<TabsScreen>{int_selectedPageIndex=0;finalList<Meal>_favoriteMeals=[];Map<Filter,bool>_selectedFilters=kInitialFilters;void_showInfoMessage(Stringmessage){ScaffoldMessenger.of(context).clearSnackBars();ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:Text(message),),);}void_toggleMealFavoriteStatus(Mealmeal){finalisExisting=_favoriteMeals.contains(meal);if(isExisting){setState((){_favoriteMeals.remove(meal);});_showInfoMessage('Meal is no longer a favorite.');}else{setState((){_favoriteMeals.add(meal);_showInfoMessage('Marked as a favorite!');});}}void_selectPage(intindex){setState((){_selectedPageIndex=index;});}void_setScreen(Stringidentifier)async{Navigator.of(context).pop();if(identifier=='filters'){finalresult=awaitNavigator.of(context).push<Map<Filter,bool>>(MaterialPageRoute(builder:(ctx)=>FiltersScreen(currentFilters:_selectedFilters,),),);setState((){_selectedFilters=result??kInitialFilters;});}}@overrideWidgetbuild(BuildContextcontext){finalavailableMeals=dummyMeals.where((meal){if(_selectedFilters[Filter.glutenFree]!&&!meal.isGlutenFree){returnfalse;}if(_selectedFilters[Filter.lactoseFree]!&&!meal.isLactoseFree){returnfalse;}if(_selectedFilters[Filter.vegetarian]!&&!meal.isVegetarian){returnfalse;}if(_selectedFilters[Filter.vegan]!&&!meal.isVegan){returnfalse;}returntrue;}).toList();WidgetactivePage=CategoriesScreen(onToggleFavorite:_toggleMealFavoriteStatus,availableMeals:availableMeals,);varactivePageTitle='Categories';if(_selectedPageIndex==1){activePage=MealsScreen(meals:_favoriteMeals,onToggleFavorite:_toggleMealFavoriteStatus,);activePageTitle='Your Favorites';}returnScaffold(appBar:AppBar(title:Text(activePageTitle),),drawer:MainDrawer(onSelectScreen:_setScreen,),body:activePage,bottomNavigationBar:BottomNavigationBar(onTap:_selectPage,currentIndex:_selectedPageIndex,items:const[BottomNavigationBarItem(icon:Icon(Icons.set_meal),label:'Categories',),BottomNavigationBarItem(icon:Icon(Icons.star),label:'Favorites',),],),);}}
classTabsScreenextendsConsumerStatefulWidget{// This is a comment btwconstTabsScreen({super.key});@overrideConsumerState<TabsScreen>createState(){return_TabsScreenState();}}class_TabsScreenStateextendsConsumerState<TabsScreen>{int_selectedPageIndex=0;finalList<Meal>_favoriteMeals=[];Map<Filter,bool>_selectedFilters=kInitialFilters;void_showInfoMessage(Stringmessage){ScaffoldMessenger.of(context).clearSnackBars();ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:Text(message),),);}void_selectPage(intindex){setState((){_selectedPageIndex=index;});}void_setScreen(Stringidentifier)async{Navigator.of(context).pop();if(identifier=='filters'){finalresult=awaitNavigator.of(context).push<Map<Filter,bool>>(MaterialPageRoute(builder:(ctx)=>FiltersScreen(currentFilters:_selectedFilters,),),);setState((){_selectedFilters=result??kInitialFilters;});}}@overrideWidget build(BuildContext context){// (1)final meals = ref.watch(mealsProvider);
finalavailableMeals=meals.where((meal){if(_selectedFilters[Filter.glutenFree]!&&!meal.isGlutenFree){returnfalse;}if(_selectedFilters[Filter.lactoseFree]!&&!meal.isLactoseFree){returnfalse;}if(_selectedFilters[Filter.vegetarian]!&&!meal.isVegetarian){returnfalse;}if(_selectedFilters[Filter.vegan]!&&!meal.isVegan){returnfalse;}returntrue;}).toList();WidgetactivePage=CategoriesScreen(onToggleFavorite:_toggleMealFavoriteStatus,availableMeals:availableMeals,);varactivePageTitle='Categories';if(_selectedPageIndex==1){activePage=MealsScreen(meals:_favoriteMeals,onToggleFavorite:_toggleMealFavoriteStatus,);activePageTitle='Your Favorites';}returnScaffold(appBar:AppBar(title:Text(activePageTitle),),drawer:MainDrawer(onSelectScreen:_setScreen,),body:activePage,bottomNavigationBar:BottomNavigationBar(onTap:_selectPage,currentIndex:_selectedPageIndex,items:const[BottomNavigationBarItem(icon:Icon(Icons.set_meal),label:'Categories',),BottomNavigationBarItem(icon:Icon(Icons.star),label:'Favorites',),],),);}}