如何使用带有 riverpod 的更改通知程序类关闭抽屉,直到我保存

时间:2021-04-05 19:28:59

标签: flutter dart riverpod

我正在尝试使用 ChangeNotifier 将抽屉值设置为 0 以关闭抽屉,但它在下面的 GIF 中不起作用:

Example

这是下面的 ChangeNotifer 类:

import 'package:flutter/foundation.dart';

class DrawerProvider extends ChangeNotifier {
  double drawerValue;

  setDrawerValue() {
    drawerValue = 0;
    notifyListeners();
  }
}

之后,我将 Drawer Provider 的一个实例作为以下代码:

final drawerProvider = ChangeNotifierProvider<DrawerProvider>((ref) => DrawerProvider());

在这里,我尝试将 setDrawerValue 方法调用为以下代码:

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,
                              ),
                            ),
                          ),

它只能在没有 ChangeNotifierClass 的情况下工作,如下代码:

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,
                              ),
                            ),
                          ),

当我设置值时:

final value = StateProvider<double>((ref) => 0);

在这里你可以找到完整的代码 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);
              },
            )
          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案