class StateManagerByProviderModel with ChangeNotifier{
int count = 0;
void increment(){
count ++;
notifyListeners();
}
}
var pmodel = StateManagerByProviderModel();
var providers = Providers();
providers..provide(Provider<StateManagerByProviderModel>.value(pmodel));
runApp(ProviderNode(child: MyApp(), providers: providers));
@override
Widget build(BuildContext contuext) {
return Scaffold(
appBar: WQAppBar(
titleName: ""
).appBar(context),
body: Center(
child: GestureDetector(
onTap: (){
},
child: Provide(
builder: (context,child,StateManagerByProviderModel scope){
return Text("${scope.count}");
}),
//child: Provide<StateManagerByProviderModel>(builder: (context,child,scope){
// return Text("${scope.count}");
// }),
),
)
);
}
child: GestureDetector(
onTap: (){
Provide.value<StateManagerByProviderModel>(context).increment();
Navigator.of(context).push(CupertinoPageRoute(builder: (context){
return StateManagerShowPage();
}));
},
child: Text("点击"),
),
封装之后
class ThemeConfigInfo {
Color themeColor = color00CCBB;
}
class ThemeConfigModel extends ThemeConfigInfo with ChangeNotifier{
Future setThemeColor(themeColor) async{
this.themeColor = themeColor;
notifyListeners();
}
}
class ProvierHelper{
static var providers = Providers();
static var themeConfigModel = ThemeConfigModel();
static init({model,child,dispose = true}){
providers = Providers()
..provide(Provider<ThemeConfigModel>.value(themeConfigModel));
return ProviderNode(child: child, providers: providers,dispose: dispose);
}
static connect<T>({builder, child, scope}) {
return Provide<T>(builder: builder, child: child, scope: scope);
}
static T value<T>(context,{scope}){
return Provide.value<T>(context,scope: scope);
}
}
main文件中
runApp(ProvierHelper.init(child: MyApp()));
Widget build(BuildContext context) {
return ProvierHelper.connect<ThemeConfigModel>(
builder: (context,child,ThemeConfigModel model){
return MaterialApp(
title: "app",
localizationsDelegates: [
// 本地化的代理类
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
WQCommonLocalizationsDelegate(),
WQLocalDelegate(),
],
supportedLocales: [
const Locale('en', 'US'), // 美国英语
const Locale('zh', 'CN'), // 中文简体
//其它Locales
],
//这句话是直接设置中文,国际化配置也会失效
locale: const Locale('zh', 'CN'), // 中文简体
theme: ThemeData(
primaryColor:model.themeColor,
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
),
routes: WQRoute.routes,
home: Builder(
builder: (context){
return TabbarController();
},
),
);
}
) ;
改变一个按钮的颜色
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
ProvierHelper.connect<ThemeConfigModel>(
builder: (context, child, ThemeConfigModel model) {
return RaisedButton(
color: model.themeColor,
onPressed: () {
Navigator.push(context, CupertinoPageRoute(builder: (context) {
return SettingPage();
}));
},
child: Text("设置"),
);
})
],
)
);
改变主题颜色
void changeTheme(){
ProvierHelper.value<ThemeConfigModel>(context).setThemeColor(colorList[selectedIndex]);
}