我想在我的应用程序中同时使用device_preview和ChangeNotifierProvider。为此,我需要将相应的代码添加到MaterialApp的builder属性中。 但是,由于MaterialApp小部件中只有一个构建器属性, 如何也实现设备预览?
void main() {
// DevicePreview(builder: (context)=> MyApp);
runApp(
// ignore: missing_required_param
ChangeNotifierProvider<ThemeChanger>(
create: (_) => ThemeChanger(lightTheme),
child: MyApp(),
),
);
}
答案 0 :(得分:0)
void main() {
runApp(
DevicePreview(
enabled: !kReleaseMode,
builder: (context) => MyApp(), // Wrap your app
),
);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: DevicePreview.locale(context),
builder: (context, child) {
child = ResponsiveWrapper.builder(
BouncingScrollWrapper.builder(context, child!),
maxWidth: 1200,
minWidth: 450,
defaultScale: true,
breakpoints: [
ResponsiveBreakpoint.resize(450, name: MOBILE),
ResponsiveBreakpoint.autoScale(600, name: TABLET),
ResponsiveBreakpoint.autoScale(1000, name: TABLET),
ResponsiveBreakpoint.resize(1200, name: DESKTOP),
ResponsiveBreakpoint.resize(2460, name: "4K"),
],
landscapeBreakpoints: [
ResponsiveBreakpoint.autoScaleDown(800, name: TABLET),
],
background: Container(
color: Color(0xFFF5F5F5),
),
);
child = DevicePreview.appBuilder(context, child);
return child;
},
home: HomePage(),
theme: Theme.of(context).copyWith(platform: TargetPlatform.android),
debugShowCheckedModeBanner: false,
);
}
}