Flutter屏幕适配插件responsive_framework_v2的使用

Flutter屏幕适配插件responsive_framework_v2的使用

响应式缩放在Flutter中的应用

此Flutter小部件设置基于预定义的断点启用UI元素的响应式缩放,使用ResponsiveScaleRoot小部件和ScaleInBetween类。

使用示例:

import 'package:flutter/material.dart';
import 'package:responsive_framework_v2/src/responsive_framework_v2.dart';

void main() {
  // 设置默认的断点配置
  ResponsiveConfig(
    xs: 360,
    sm: 640,
    md: 768,
    lg: 1024,
    xl: 1280,
  );

  runApp(
    const MainApp(),
  );
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    // 定义一个覆盖配置
    final overrideConfig = ResponsiveConfig(
      xs: 320,
      sm: 480,
      md: 640,
      lg: 800,
      xl: 960,
    );
    return MaterialApp(
      home: Scaffold(
        body: ResponsiveScaleRoot(
          breakpoints: [
            ScaleInBetween(start: 500, end: 600, scale: 600),
            ScaleInBetween(start: 400, end: 500, scale: 600),
            ScaleInBetween(start: 300, end: 400, scale: 600),
            ScaleInBetween(
                start: 200,
                end: 300,
                scale: 600), // 在600到500之间缩放UI到300
          ],
          child: LayoutBuilder(builder: (context, constraints) {
            return SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          height: 200,
                          color: Colors.blue,
                          child: const Center(
                            child: Text(
                              'Scaled UI (600-700)',
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                        Container(
                          height: 200,
                          color: Colors.blue,
                          child: const Center(
                            child: Text(
                              'Scaled UI (600-700)',
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                        Container(
                          height: 200,
                          color: Colors.blue,
                          child: const Center(
                            child: Text(
                              'Scaled UI (600-700)',
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                        Container(
                          height: 200,
                          color: Colors.blue,
                          child: const Center(
                            child: Text(
                              'Scaled UI (600-700)',
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Container(
                      height: 100,
                      width: 100,
                      decoration: const BoxDecoration(
                          shape: BoxShape.circle, color: Colors.red),
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    // 使用响应式方法显示不同内容,并使用全局实例的断点配置
                    context.responsive(
                      breakpoints: {
                        300: const Text("Extra Small"),
                        600: const Text("Small"),
                        900: Text("Medium"),
                        912: Text('912'),
                        1200: Text("Large"),
                        1400: Text("Extra Large"),
                      },
                      sm: Text('sm'),
                      md: Text('md'),
                      lg: Text('lg'),
                      xl: Text('xl'),
                    ),
                    // 使用覆盖配置来覆盖默认的断点
                    context.responsive(
                      sm: Text('sm over'),
                      overrideConfig: overrideConfig,
                      // lg: Text('lg over'),
                      // xl: Text('xl over'),
                    ),
                    context.showOnScreenBreakPoint(
                        breakpoint: 600, widget: Text('showOnScreenWidth 600')),
                  ],
                ),
              ),
            );
          }),
        ),
      ),
    );
  }
}

更多关于Flutter屏幕适配插件responsive_framework_v2的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter屏幕适配插件responsive_framework_v2的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


responsive_framework_v2 是一个用于 Flutter 的屏幕适配插件,旨在帮助开发者更容易地创建响应式布局。它允许你根据不同的屏幕尺寸和方向来调整 UI 布局。以下是如何使用 responsive_framework_v2 的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  responsive_framework_v2: ^latest_version

然后运行 flutter pub get 来获取依赖。

2. 导入包

在你的 Dart 文件中导入 responsive_framework_v2

import 'package:responsive_framework_v2/responsive_framework_v2.dart';

3. 使用 ResponsiveWrapper

ResponsiveWrapperresponsive_framework_v2 的核心组件。你可以将它包裹在你的应用程序的根部件上,以便自动处理屏幕适配。

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, child) {
        return ResponsiveWrapper.builder(
          child,
          maxWidth: 1200,
          minWidth: 480,
          defaultScale: true,
          breakpoints: [
            ResponsiveBreakpoint.autoScale(480, name: MOBILE),
            ResponsiveBreakpoint.autoScale(800, name: TABLET),
            ResponsiveBreakpoint.autoScale(1000, name: DESKTOP),
          ],
        );
      },
      home: HomeScreen(),
    );
  }
}

4. 定义断点

ResponsiveBreakpoint 用于定义不同的屏幕尺寸断点。你可以根据需要自定义这些断点。例如:

breakpoints: [
  ResponsiveBreakpoint.autoScale(480, name: MOBILE),
  ResponsiveBreakpoint.autoScale(800, name: TABLET),
  ResponsiveBreakpoint.autoScale(1000, name: DESKTOP),
],

5. 使用 ResponsiveValue

ResponsiveValue 允许你根据屏幕尺寸返回不同的值。例如,你可以根据屏幕宽度返回不同的字体大小:

Text(
  'Hello, World!',
  style: TextStyle(
    fontSize: ResponsiveValue<double>(
      context,
      defaultValue: 16.0,
      valueWhen: [
        ResponsiveBreakpoint.mobile(context, value: 14.0),
        ResponsiveBreakpoint.tablet(context, value: 18.0),
        ResponsiveBreakpoint.desktop(context, value: 20.0),
      ],
    ).value,
  ),
);

6. 使用 ResponsiveRow 和 ResponsiveColumn

ResponsiveRowResponsiveColumn 是用于创建响应式网格布局的组件。你可以根据屏幕尺寸调整列数和间距:

ResponsiveRow(
  children: [
    ResponsiveCol(
      child: Container(color: Colors.red, height: 100),
      span: 6,
      offset: 3,
    ),
    ResponsiveCol(
      child: Container(color: Colors.blue, height: 100),
      span: 4,
      offset: 4,
    ),
  ],
);

7. 使用 ResponsiveVisibility

ResponsiveVisibility 允许你根据屏幕尺寸显示或隐藏某些部件:

ResponsiveVisibility(
  visible: false,
  hiddenWhen: [ResponsiveBreakpoint.mobile(context)],
  child: Container(color: Colors.green, height: 100),
);

8. 使用 ResponsiveScaledBox

ResponsiveScaledBox 允许你根据屏幕尺寸缩放部件:

ResponsiveScaledBox(
  scale: ResponsiveValue<double>(
    context,
    defaultValue: 1.0,
    valueWhen: [
      ResponsiveBreakpoint.mobile(context, value: 0.8),
      ResponsiveBreakpoint.tablet(context, value: 1.0),
      ResponsiveBreakpoint.desktop(context, value: 1.2),
    ],
  ),
  child: Container(color: Colors.yellow, height: 100),
);
回到顶部