I have a simple app to practice provider package to change state, when the user logs in at initState
I change the visibility of the splash screen to false, Thus loading my main app
@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,
),
)
Now, I want to do the same thing with provider package Here is the class for it
class AppData extends ChangeNotifier {
bool splashMainScreen;
bool floatActBtnMainScreen;
void closeSplashScreen(bool splash, bool floatActBtn) {
splashMainScreen = splash;
floatActBtnMainScreen = floatActBtn;
notifyListeners();
}
}
From hereon I don't know how to pass the state to the AppData
class and back to the widget
The least I could do was something like this
@override
void initState() {
super.initState();
Provider.of<AppData>(context, listen: false)
.closeSplashScreen(false, false);
I want to change visible: splash
to false with the provider package instead of setState, Any help ? as I keep getting the error
'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),
]),
),
I don't know if I understand question well, but try this:
first create provider file :
class AppData extends ChangeNotifier {
bool splash = false;
bool floatingActionButton = true;
void closeSplashScreen() {
splash = true;
floatingActionButton = false;
notifyListeners();
}
}
Now use the initial values using consumer :
@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),
]),
),
),
));
})
now you can change the values of splash and floatingActionButton using closeSplashScreen() in provider such us this :
Consumer<AppData>(builder:(context,appData,child){
return ElevatedButton(
onPresse : appData.closeSplashScreen();
child:Text('change')
);
}
then don't use statefull widget
Hope this works