Flutter设备框架展示插件device_frame_community的使用

Flutter设备框架展示插件device_frame_community的使用

device_frame_community 是一个用于在 Flutter 应用中展示设备框架的插件。它可以帮助开发者更好地模拟不同设备上的应用效果。以下是如何使用 device_frame_community 的详细说明。

快速开始

要使用 device_frame_community,只需将任何小部件包裹在 DeviceFrame 小部件中,并指定设备类型。

DeviceFrame(
    device: Devices.ios.iPhone11,
    isFrameVisible: true,
    orientation: Orientation.portrait,
    screen: Container(
        color: Colors.blue,
        child: Text('Hello'),
    ),
)

使用方法

显示虚拟键盘

若要在设备上显示虚拟键盘,可以将任何小部件包裹在 VirtualKeyboard 中。

DeviceFrame(
    device: Devices.ios.iPhone11,
    orientation: orientation,
    screen: VirtualKeyboard(
        isEnabled: true,
        child: // ...
    ),
)

维护设备媒体查询和主题

确保 WidgetsApp 使用从模拟设备获取的模拟 MediaQuery,可以通过设置 useInheritedMediaQuery 属性为 true 来实现。

DeviceFrame(
    device: Devices.ios.iPhone11,
    orientation: orientation,
    screen: Builder(
        builder: (deviceContext) => MaterialApp(
            useInheritedMediaQuery: true,
            theme: Theme.of(context),
        ),
    ),
),

创建自定义通用设备

device_frame_community 提供了多种通用设备类型的工厂方法,方便创建自定义设备实例。

手机

DeviceInfo.genericPhone(
    platform: TargetPlatform.android,
    name: 'Medium',
    id: 'medium',
    screenSize: const Size(412, 732),
    safeAreas: const EdgeInsets.only(
      left: 0.0,
      top: 24.0,
      right: 0.0,
      bottom: 0.0,
    ),
    rotatedSafeAreas: const EdgeInsets.only(
      left: 0.0,
      top: 24.0,
      right: 0.0,
      bottom: 0.0,
    ),
)

平板

DeviceInfo.genericTablet(
    platform: TargetPlatform.android,
    name: 'Medium',
    id: 'medium',
    screenSize: const Size(1024, 1350),
    safeAreas: const EdgeInsets.only(
      left: 0.0,
      top: 24.0,
      right: 0.0,
      bottom: 0.0,
    ),
    rotatedSafeAreas: const EdgeInsets.only(
      left: 0.0,
      top: 24.0,
      right: 0.0,
      bottom: 0.0,
    ),
)

桌面显示器

DeviceInfo.genericDesktopMonitor(
    platform: TargetPlatform.windows,
    name: 'Wide',
    id: 'wide',
    screenSize: const Size(1920, 1080),
    windowPosition: Rect.fromCenter(
      center: const Offset(
        1920 * 0.5,
        1080 * 0.5,
      ),
      width: 1620,
      height: 780,
    ),
)

笔记本电脑

DeviceInfo.genericLaptop(
    platform: TargetPlatform.windows,
    name: 'Laptop',
    id: 'laptop',
    screenSize: const Size(1920, 1080),
    windowPosition: Rect.fromCenter(
      center: const Offset(
        1920 * 0.5,
        1080 * 0.5,
      ),
      width: 1620,
      height: 780,
    ),
)

可用设备

所有可用设备的截图可以在 这里 查看。

贡献

编辑设备框架

所有设备框架都是在 Figma 文件 中设计的。

示例代码

以下是完整的示例代码:

import 'package:device_frame_community/device_frame.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  runApp(const ExampleApp());
}

class ExampleApp extends StatefulWidget {
  const ExampleApp({Key? key}) : super(key: key);

  [@override](/user/override)
  _ExampleAppState createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  bool isDark = true;
  bool isFrameVisible = true;
  bool isKeyboard = false;
  bool isEnabled = true;

  final GlobalKey screenKey = GlobalKey();

  Orientation orientation = Orientation.portrait;
  Widget _frame(DeviceInfo device) => Center(
        child: DeviceFrame(
          device: device,
          isFrameVisible: isFrameVisible,
          orientation: orientation,
          screen: Container(
            color: Colors.blue,
            child: VirtualKeyboard(
              isEnabled: isKeyboard,
              child: FakeScreen(key: screenKey),
            ),
          ),
        ),
      );

  [@override](/user/override)
  Widget build(BuildContext context) {
    return DeviceFrameTheme(
      style: DeviceFrameStyle.dark(),
      child: MaterialApp(
        title: 'Device Frames',
        theme: ThemeData(
          primarySwatch: Colors.purple,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: DefaultTabController(
          length: Devices.all.length,
          child: Scaffold(
            backgroundColor: isDark ? Colors.white : Colors.black,
            appBar: AppBar(
              title: const Text('Device Frames'),
              actions: <Widget>[
                IconButton(
                  onPressed: () {
                    setState(() {
                      isFrameVisible = !isFrameVisible;
                    });
                  },
                  icon: const Icon(Icons.settings_brightness),
                ),
                IconButton(
                  onPressed: () {
                    setState(() {
                      isDark = !isDark;
                    });
                  },
                  icon: const Icon(Icons.brightness_medium),
                ),
                IconButton(
                  onPressed: () {
                    setState(
                      () {
                        orientation = orientation == Orientation.landscape
                            ? Orientation.portrait
                            : Orientation.landscape;
                      },
                    );
                  },
                  icon: const Icon(Icons.rotate_90_degrees_ccw),
                ),
                IconButton(
                  onPressed: () {
                    setState(
                      () {
                        isKeyboard = !isKeyboard;
                      },
                    );
                  },
                  icon: const Icon(Icons.keyboard),
                ),
                /*IconButton(
                  onPressed: () {
                    setState(() {
                      isEnabled = !isEnabled;
                    });
                  },
                  icon: Icon(Icons.check),
                ),*/
              ],
              bottom: TabBar(
                isScrollable: true,
                tabs: [
                  ...Devices.all.map(
                    (x) => Tab(
                      text: '${x.identifier.type} ${x.name}',
                    ),
                  ),
                ],
              ),
            ),
            body: SafeArea(
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: Builder(
                  builder: (context) => !isEnabled
                      ? FakeScreen(key: screenKey)
                      : AnimatedBuilder(
                          animation: DefaultTabController.of(context)!,
                          builder: (context, _) => _frame(
                            Devices
                                .all[DefaultTabController.of(context)!.index],
                          ),
                        ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class FakeScreen extends StatefulWidget {
  const FakeScreen({
    Key? key,
  }) : super(key: key);
  [@override](/user/override)
  _FakeScreenState createState() => _FakeScreenState();
}

class _FakeScreenState extends State<FakeScreen> {
  bool isDelayEnded = false;

  [@override](/user/override)
  void initState() {
    Future.delayed(const Duration(seconds: 2)).then(
      (value) => setState(
        () => isDelayEnded = true,
      ),
    );
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final mediaQuery = MediaQuery.of(context);
    final color =
        theme.platform == TargetPlatform.iOS ? Colors.cyan : Colors.green;
    return Container(
      color: color.shade300,
      padding: mediaQuery.padding,
      child: Container(
        color: color,
        child: AnimatedPadding(
          duration: const Duration(milliseconds: 500),
          padding: EdgeInsets.only(bottom: mediaQuery.viewInsets.bottom),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("${theme.platform}"),
              Text("Size: ${mediaQuery.size.width}x${mediaQuery.size.height}"),
              Text("PixelRatio: ${mediaQuery.devicePixelRatio}"),
              Text("Padding: ${mediaQuery.padding}"),
              Text("Insets: ${mediaQuery.viewInsets}"),
              Text("ViewPadding: ${mediaQuery.viewPadding}"),
              if (isDelayEnded) const Text("---"),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter设备框架展示插件device_frame_community的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter设备框架展示插件device_frame_community的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


device_frame_community 是一个 Flutter 插件,用于在应用程序中展示设备框架,以便在开发过程中模拟不同设备的外观。这个插件可以帮助开发者在设计和测试 UI 时更好地了解 UI 在不同设备上的表现。

安装

首先,你需要在 pubspec.yaml 文件中添加 device_frame_community 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  device_frame_community: ^2.0.0

然后运行 flutter pub get 来安装依赖。

基本使用

1. 导入插件

import 'package:device_frame_community/device_frame_community.dart';

2. 使用 DeviceFrame 组件

DeviceFrame 组件允许你将内容包裹在设备框架中。你可以指定设备类型、方向等信息。

DeviceFrame(
  device: Devices.ios.iPhone13,
  orientation: Orientation.portrait,
  screen: Container(
    color: Colors.white,
    child: Center(
      child: Text('Hello, Device Frame!'),
    ),
  ),
);

3. 选择设备

device_frame_community 提供了多种设备选项,你可以通过 Devices 类来访问这些设备。例如:

  • Devices.ios.iPhone13
  • Devices.android.samsungGalaxyS21
  • Devices.ios.iPadPro12Inch

4. 设置设备方向

你可以通过 orientation 参数来设置设备的方向,支持 Orientation.portraitOrientation.landscape

示例代码

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 device_frame_community 插件:

import 'package:flutter/material.dart';
import 'package:device_frame_community/device_frame_community.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Device Frame Example'),
        ),
        body: Center(
          child: DeviceFrame(
            device: Devices.ios.iPhone13,
            orientation: Orientation.portrait,
            screen: Container(
              color: Colors.white,
              child: Center(
                child: Text('Hello, Device Frame!'),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

自定义设备框架

device_frame_community 还允许你自定义设备框架的颜色、样式等。你可以通过 DeviceFramestyle 参数来进行自定义。

DeviceFrame(
  device: Devices.ios.iPhone13,
  orientation: Orientation.portrait,
  style: DeviceFrameStyle(
    frameColor: Colors.black,
    screenBackgroundColor: Colors.grey,
  ),
  screen: Container(
    color: Colors.white,
    child: Center(
      child: Text('Hello, Device Frame!'),
    ),
  ),
);
回到顶部