如何使用提供程序包更改状态

How can I change state using provider package

我有一个简单的应用程序来练习提供程序包更改状态,当用户登录时 initState 我将启动画面的可见性更改为 false,从而加载我的主应用程序

@override
  void initState() {
    super.initState();
     setState(() {
      splash = false;
      floatingActionButton = true;
    });
}

//this is the position widget that becomes invisible. I.e the splash screen is offloaded

 @override
  Widget build(BuildContext context) {
  Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Visibility(
                visible: splash,
                maintainState: false,
                maintainAnimation: false,
                maintainSize: false,
                maintainInteractivity: false,
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topRight,
                        end: Alignment.bottomLeft,
                        colors: [
                          Color(0xFFFF800B),
                          Color(0xFFCE1010),
                        ]),
                  ),
                  // color: Colors.white,
                 
              ),
            )

现在,我想对 provider 包做同样的事情 这是它的 class

class AppData extends ChangeNotifier {
 bool splashMainScreen;
  bool floatActBtnMainScreen;
 void closeSplashScreen(bool splash, bool floatActBtn) {
    splashMainScreen = splash;
    floatActBtnMainScreen = floatActBtn;
    notifyListeners();
  }
}

从现在开始,我不知道如何将状态传递给 AppData class 并返回给小部件 至少我能做的就是这样

@override
  void initState() {
    super.initState();
     
 Provider.of<AppData>(context, listen: false)
        .closeSplashScreen(false, false);

我想使用提供程序包而不是 setState 将 visible: splash 更改为 false,有什么帮助吗?因为我不断收到错误 'package:flutter/src/widgets/visibility.dart': Failed assertion: line 67 pos 15: 'visible != null': is not true. package:flutter/…/widgets/visibility.dart:1

 Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Consumer<AppData>(
                builder: (ctx, dsc, child) => Visibility(
                  visible: dsc.splashMainScreen,
                  maintainState: false,
                  maintainAnimation: false,
                  maintainSize: false,
                  maintainInteractivity: false,
                  child: Container(
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                          begin: Alignment.topRight,
                          end: Alignment.bottomLeft,
                          colors: [
                            Color(0xFFFF800B),
                            Color(0xFFCE1010),
                          ]),
                    ),

我不知道我是否理解问题,但是试试这个:

首先创建提供程序文件:

class AppData extends ChangeNotifier {
  bool splash = false;
  bool floatingActionButton = true;
  void closeSplashScreen() {
    splash = true;
    floatingActionButton = false;
    notifyListeners();
  }
}

现在使用消费者使用初始值:

@override
  Widget build(BuildContext context) {
  Positioned(
              top: 0,
              left: 0,
              right: 0,
              child:Consumer<AppData>(builder:(context,appData,child){
              return  Visibility(
                visible: appData.splash,
                maintainState: false,
                maintainAnimation: false,
                maintainSize: false,
                maintainInteractivity: false,
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topRight,
                        end: Alignment.bottomLeft,
                        colors: [
                          Color(0xFFFF800B),
                          Color(0xFFCE1010),
                        ]),
                  ), 
              ),
            ));
})

现在您可以在提供程序中使用 closeSplashScreen() 更改 splash 和 floatingActionButton 的值,例如我们这样:

Consumer<AppData>(builder:(context,appData,child){
    return ElevatedButton(
       onPresse : appData.closeSplashScreen();
       child:Text('change')
    );
}

那就不要使用有状态小部件

希望这有效