如何在Flutter中同时实现device_preview和响应_框架软件包

时间:2020-08-06 15:39:09

标签: flutter dart flutter-dependencies

我想在我的应用程序中同时使用device_preview和ChangeNotifierProvider。为此,我需要将相应的代码添加到MaterialApp的builder属性中。 但是,由于MaterialApp小部件中只有一个构建器属性, 如何也实现设备预览?

void main() {
  // DevicePreview(builder: (context)=> MyApp);
  runApp(
    // ignore: missing_required_param
    ChangeNotifierProvider<ThemeChanger>(
      create: (_) => ThemeChanger(lightTheme),
      child: MyApp(),
    ),
  );
}

供参考: https://pub.dev/packages/device_preview/example

1 个答案:

答案 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,
);
}
}