Flutter 適配移動端和web不一樣尺寸

用到的庫

這款開源的庫,能夠實現不一樣屏幕尺寸的適配.responsive_builderweb

使用方法

1. pubspec.yaml引入庫 responsive_builder: ^0.3.0

2. 代碼引用 import 'package:responsive_builder/responsive_builder.dart';

3. 像使用普通控件同樣,使用以下代碼:

ResponsiveBuilder(
              builder: (context, sizingInformation) {
                if (sizingInformation.deviceScreenType ==
                    DeviceScreenType.desktop) {
                  return Container(
                    color: Colors.blue,
                    child: Text("desktop"),
                  );
                }

                if (sizingInformation.deviceScreenType ==
                    DeviceScreenType.tablet) {
                  return Container(
                    color: Colors.red,
                    child: Text("tablet"),
                  );
                }

                if (sizingInformation.deviceScreenType ==
                    DeviceScreenType.watch) {
                  return Container(
                    color: Colors.yellow,
                    child: Text("watch"),
                  );
                }
                return Container(
                  color: Colors.green,
                  child: Text("mobile"),
                );
              },
            ),
以上代碼,能夠在不一樣尺寸的狀況下,返回不一樣的Container.

其餘參考文章ui

相關文章
相關標籤/搜索