Reputation: 47
My ChangeNotifierProvider is not being called....
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyGlobals>(
builder: (context) => MyGlobals(),
child: MaterialApp(
theme: MyGlobals().GetTheme() == "dark" ? ThemeData(fontFamily: 'ERAS', primaryColor: Colors.black, accentColor: Colors.amberAccent, secondaryHeaderColor: Colors.black54):
ThemeData(fontFamily: 'ERAS', primaryColor: Colors.white, accentColor: Colors.black, secondaryHeaderColor: Colors.white70),
home: MyHomePage(),
));
}
}
And this is MyGlobals:
String _theme = "dark";
class MyGlobals with ChangeNotifier {
getTheme() => _theme;
void setTheme(String _themecolor) {
_theme = _themecolor;
notifyListeners();
}
}
Im not getting any errors its just not being called. I am calling setTheme from another .dart file and its not being changenotifierprovider is not being called. MyGlobals is a seperate .dart file from the ChangeNotifierProvider code above.
Upvotes: 1
Views: 3147
Reputation: 103401
You need a Consumer
widget to listen the changes you made in your Model
, like this :
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyGlobals>(
builder: (context) => MyGlobals(),
child: Consumer<MyGlobals>(
builder: (_, model, __) {
return MaterialApp(
theme: model.getTheme() == "dark"
? ThemeData(
fontFamily: 'ERAS',
primaryColor: Colors.black,
accentColor: Colors.amberAccent,
secondaryHeaderColor: Colors.black54)
: ThemeData(
fontFamily: 'ERAS',
primaryColor: Colors.white,
accentColor: Colors.black,
secondaryHeaderColor: Colors.white70),
home: MyHomePage(),
);
},
),
);
}
Then you can update your theme in this way:
Provider.of<MyGlobals>(context).setTheme("your_theme")
Upvotes: 3