1.引用 google 状态管理库
provider: ^3.0.0+1
2.自定义model 继承 ChangeNotifier
class SkinModel extends ChangeNotifier {
MaterialColor color =Colors.blue;
}
3.使用 ChangeNotifierProvider
void main() => runApp(ChangeNotifierProvider(
builder: (context) =>SkinModel(),
child:MyApp(),
));
class MyApp extends StatelessWidget {
// This widget is the root of your application.
SkinModel skinModel;
@override
Widget build(BuildContext context) {
return MaterialApp(
title:'Flutter Demo',
theme:ThemeData(
primarySwatch:Provider.of(context).color,
),
home:MyHomePage(),
);
}
}
4. 修改样式
void _changeState() {
SkinModel skinModel =Provider.of(context)..color =Colors.red;
skinModel.notifyListeners();
}
网友评论