0

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:

Example

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>((ref) => 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>((ref) => 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>((ref) => 0);
  final drawerProvider = ChangeNotifierProvider<DrawerProviderNotifier>(
      (ref) => DrawerProviderNotifier());
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
  Widget contentWidget = HomeScreen();

  @override
  Widget build(BuildContext context) {
    print('build');
    return Consumer(
      builder: (context, watch, child) => 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("assets/images/myimage.jpg"),
                          ),
                          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>(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 > 0) {
                  watch(value).state = 1;
                } else {
                  watch(value).state = 0;
                }
                // print(e.delta.dx);
              },
            )
          ],
        ),
      ),
    );
  }
}
Mahmoud Al-Haroon
  • 2,239
  • 7
  • 36
  • 72
  • Have you tried using a GlobalKey on the Scaffold and closing programmatically? https://stackoverflow.com/a/60652518/3224608 – daddygames Apr 05 '21 at 20:38
  • @daddygames yes, but it doesn't work because I am not using the flutter `drawer` – Mahmoud Al-Haroon Apr 05 '21 at 20:55
  • 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? – Alex Hartford Apr 05 '21 at 22:33
  • @AlexHartford I have edited the question, please have a check :), and thank you for your support :) – Mahmoud Al-Haroon Apr 05 '21 at 22:54

0 Answers0