Title: How to close drawer using change Notifier class with riverpod till I save
Tags: flutter;dart;riverpod
Question: I am trying to set the drawer value to 0 using ChangeNotifier as to close the drawer, but it doesn't work as in the below GIF:
and this is the below ChangeNotifer class:
import 'package:flutter/foundation.dart';
class DrawerProvider extends ChangeNotifier {
double drawerValue;
setDrawerValue() {
drawerValue = 0;
notifyListeners();
After that I take an instance of Drawer Provider as the below code:
final drawerProvider = ChangeNotifierProvider<DrawerProvider&gt;((ref) =&gt; DrawerProvider());
and here I am trying to call the setDrawerValue method as the below code:
ListTile(
onTap: () {
contentWidget = HomeScreen();
Navigator.of(context).maybePop();
watch(drawerProvider).setDrawerValue();
},
leading: Icon(
Icons.home,
color: Colors.white,
),
title: IPetLabel(
ipetText: 'Home',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
it works only without the ChangeNotifierClass as the below code:
ListTile(
onTap: () {
contentWidget = HomeScreen();
Navigator.of(context).maybePop();
watch(value).state = 0;
},
leading: Icon(
Icons.home,
color: Colors.white,
),
title: IPetLabel(
ipetText: 'Home',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
when I set the value:
final value = StateProvider<double&gt;((ref) =&gt; 0);
and here you can find the full code main.dart:
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ipet/provider/drawer_provider.dart';
import 'package:ipet/ui/screens/home_screen.dart';
import 'package:ipet/ui/screens/settings_screen.dart';
import 'package:ipet/ui/widgets/ipet_gesture_detector.dart';
import 'package:ipet/ui/widgets/ipet_icon.dart';
import 'package:ipet/ui/widgets/ipet_list_tile.dart';
import 'package:ipet/ui/widgets/label.dart';
void main() {
runApp(ProviderScope(child: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
final value = StateProvider<double&gt;((ref) =&gt; 0);
final drawerProvider = ChangeNotifierProvider<DrawerProviderNotifier&gt;(
(ref) =&gt; DrawerProviderNotifier());
final GlobalKey<ScaffoldState&gt; scaffoldKey = new GlobalKey<ScaffoldState&gt;();
Widget contentWidget = HomeScreen();
@override
Widget build(BuildContext context) {
print('build');
return Consumer(
builder: (context, watch, child) =&gt; Scaffold(
key: scaffoldKey,
body: Stack(
children: [
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.cyan[400],
Colors.cyan[800],
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
),
SafeArea(
child: Container(
width: 200.0,
padding: EdgeInsets.all(8.0),
child: Column(
children: [
DrawerHeader(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
radius: 50.0,
backgroundImage:
AssetImage(&quot;assets/images/myimage.jpg&quot;),
),
SizedBox(
height: 10.0,
),
FittedBox(
child: IPetLabel(
ipetText: 'Mahmoud Al-Haroon',
ipetTextStyle: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
),
],
),
),
Expanded(
child: ListView(
children: [
ListTile(
onTap: () {
contentWidget = HomeScreen();
// Navigator.pop(context);
Navigator.of(context).maybePop();
watch(value).state = 0;
// watch(drawerProvider).setDrawerValue();
},
leading: Icon(
Icons.home,
color: Colors.white,
),
title: IPetLabel(
ipetText: 'Home',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
ListTile(
onTap: () {},
leading: Icon(
Icons.map,
color: Colors.white,
),
title: IPetLabel(
ipetText: 'Map',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
IPetListTile(
ltOnTab: () {
contentWidget = SettingsScreen();
Navigator.of(context).maybePop();
watch(value).state = 0;
// watch(drawerProvider).setDrawerValue();
},
ltLeading: Icon(
Icons.settings,
color: Colors.white,
),
ltTitle: IPetLabel(
ipetText: 'Settings',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
IPetListTile(
ltOnTab: () {},
ltLeading: Icon(
Icons.star_rate,
color: Colors.white,
),
ltTitle: IPetLabel(
ipetText: 'Rate App',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
IPetListTile(
ltOnTab: () {},
ltLeading: Icon(
Icons.logout,
color: Colors.white,
),
ltTitle: IPetLabel(
ipetText: 'Logout',
ipetTextStyle: TextStyle(
color: Colors.white,
),
),
),
],
),
),
],
),
),
),
TweenAnimationBuilder(
tween: Tween<double&gt;(begin: 0, end: watch(value).state),
duration: Duration(milliseconds: 500),
curve: Curves.easeIn,
builder: (_, double val, __) {
return (Transform(
alignment: Alignment.center,
transform: Matrix4.identity()
..setEntry(3, 2, 0.001)
..setEntry(0, 3, 200 * val)
..rotateY((pi / 6) * val),
child: Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {
watch(value).state == 0
? watch(value).state = 1
: watch(value).state = 0;
},
icon: IPetIcon(
ipetIcon: Icons.list,
ipetIconColor: Colors.white,
),
),
title: IPetLabel(ipetText: 'IPet'),
backgroundColor: Colors.cyan[800],
),
body: Container(
child: contentWidget,
),
),
));
},
),
IPetGestureDetector(
gdOnHorizontalDragUpdate: (e) {
if (e.delta.dx &gt; 0) {
watch(value).state = 1;
} else {
watch(value).state = 0;
}
// print(e.delta.dx);
},
)
],
),
),
);
Comment: Have you tried using a GlobalKey on the Scaffold and closing programmatically? https://stackoverflow.com/a/60652518/3224608

Comment: I know there is code in the other question you asked, but can you please expand the code in this question so we can at least see where you're declaring the providers, and how/where you are reading the value of the provider?

Comment: @daddygames yes, but it doesn't work because I am not using the flutter `drawer`

Comment: @AlexHartford I have edited the question, please have a check :), and thank you for your support :)



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/05/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<flutter><dart><riverpod>', 'Title': 'How to close drawer using change Notifier class with riverpod till I save', 'ViewCount': 706, 'id': 66958694, 'user_AboutMe': '', 'user_AccountId': 10301093.0, 'user_DisplayName': 'Mahmoud Harooney', 'user_Reputation': 3305.0, 'user_Views': 360.0, 'user_id': 12089796.0}}