[英]Flutter global Providers
我是 flutter 的初中生
我有一个项目,我使用Provider
进行 state 管理,
在我的应用程序中,我有App Provider
,我在所有其他提供类中使用它,它保存数据以在所有项目中使用它们。
我的应用程序main class
在这里,它太脏了:)
我像这样实现了这个 class:
final ThemeData androidTheme =
ThemeData(fontFamily: 'ChiscoText', dividerColor: Colors.transparent);
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
AppController appController = AppController();
@override
Widget build(BuildContext context) {
const defaultTextStyle =
TextStyle(fontFamily: 'ChiscoText', color: Styles.primaryTextColor);
return Sizer(
builder: (BuildContext context, Orientation orientation,
DeviceType deviceType) {
return ChangeNotifierProvider.value(
value: appController,
child: Consumer<AppController>(
builder: (context, AppController controller, child) =>
GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: MaterialApp(
theme: androidTheme,
title: 'Chisco',
routes: {
loginPage: (context) => ChangeNotifierProvider(
child: const AuthScreen(),
create: (ctx) => AuthController(context),
),
splashPage: (context) => ChangeNotifierProvider(
create: (context) => SplashController(context),
child: const SplashScreen(),
),
homePage: (context) => ChangeNotifierProvider(
create: (context) => HomeController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: HomeScreen()),
),
accountPage: (context) => ChangeNotifierProvider(
create: (context) => AccountController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: AccountScreen()),
),
profilePage: (context) => ChangeNotifierProvider(
create: (context) => ProfileController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: ProfileScreen()),
),
coolerDevicePage: (context) => ChangeNotifierProvider(
create: (BuildContext context) =>
CoolerController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: CoolerScreen()),
),
schedulePage: (context) => ChangeNotifierProvider(
create: (BuildContext context) =>
ScheduleController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: ScheduleScreen()),
),
powerDevicePage: (context) => const Directionality(
textDirection: TextDirection.rtl,
child: PowerScreen()),
testPage: (context) => ChangeNotifierProvider(
create: (context) => HomeController(context),
child: const Directionality(
textDirection: TextDirection.rtl,
child: TestScreen()),
),
},
initialRoute: splashPage,
),
)));
},
);
}
}
这里我使用ChangeNotifierProvider.value
在所有项目中创建AppController
实例。
现在我想再使用一个提供程序 class ,例如我在所有其他提供程序(通用提供程序类)中使用的AppController
。
它用于连接到 mqtt,
我有使用插入ChangeNotifierProvider.value
的MultiProvider
来创建AppController
和mqttController
吗?
你有什么建议让我的代码更简单、更干净?
首先创建route.dart
文件并将所有路线放入其中,如下所示:
var routes = <String, WidgetBuilder>{
'/loginPage': (context) => AuthScreen(),
...
};
然后:
Sizer(
builder: (BuildContext context, Orientation orientation,
DeviceType deviceType) {
return MultiProvider(
providers: [
ChangeNotifierProvider<AppController>(create: (_) => AppController()),
ChangeNotifierProvider<AppControllerTwo>(create: (_) => AppControllerTwo()),
],
child: Consumer2(
builder: (context,AppControllerTwo controllerTwo, AppController controller, child) =>
GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: MaterialApp(
theme: androidTheme,
title: 'Chisco',
routes: routes,
initialRoute: splashPage,
),
)));
},
)
您可以调用Consumer2
、 Consumer3
、 Consumer4
、 Consumer5
和Consumer6
。
当您需要您的 controller 值时,请使用此值:
AppController controller = Provider.of<AppController>(context, listen: false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.