我正试着用Ffltter为我的网站创建一个主页, 我已经将内部具有灵活小部件的列放入了堆栈小部件. 在全屏模式下,它看起来很好,位置正确,但当我改变屏幕大小时,列不会粘在顶部,而是在中间显示一个小边距.

以下是代码

<sub>
class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  bool _isLogin = false;
  @override
  Widget build(BuildContext context) {
    // check login
    Future.delayed(Duration.zero, () {
      if (isLogin()) {
        setState(() {
          _isLogin = true;
        });
      } else {
        MovePage(
            context: context,
            routeName: Routes.loginPage,
            generation: Generation());
      }
    });
    if (_isLogin) {
      return Scaffold(
        body: Stack(children: [
            const _OrangeVertical(),
            const _GreenVertical(),
            const _PurpleVertical(),
            const _LogoVertical(),
        ]),
      );
    } else {
      return const Scaffold(
        body: Center(
          child: CircularProgressIndicator(),
        ),
      );
    }
  }
}

class _Logo1920 extends StatelessWidget {
  const _Logo1920();

  @override
  Widget build(BuildContext context) {
    return const FlexibleImage(
      imgPath: 'assets/images/1920_logo.png',
      xFlexesOne: 3,
      xFlexesTwo: 4,
      xFlexesThree: 3,
      yFlexesOne: 325,
      yFlexesTwo: 625,
      yFlexesThree: 735,
    );
  }
}
class _Orange1920 extends StatelessWidget {
  const _Orange1920();

  @override
  Widget build(BuildContext context) {
    return const FlexibleImage(
      imgPath: 'assets/images/1920_orange.png',
      xFlexesOne: 0,
      xFlexesTwo: 1,
      xFlexesThree: 2,
      yFlexesOne: 0,
      yFlexesTwo: 7,
      yFlexesThree: 10,
    );
  }
}

class _Green1920 extends StatelessWidget {
  const _Green1920();

  @override
  Widget build(BuildContext context) {
    return const FlexibleImage(
      imgPath: 'assets/images/1920_green_new.png',
      xFlexesOne: 0,
      xFlexesTwo: 11,
      xFlexesThree: 20,
      yFlexesOne: 11,
      yFlexesTwo: 6,
      yFlexesThree: 0,
    );
  }
}

class _Purple1920 extends StatelessWidget {
  const _Purple1920();

  @override
  Widget build(BuildContext context) {
    return const FlexibleImage(
        imgPath: 'assets/images/1920_purple.png',
        xFlexesOne: 20,
        xFlexesTwo: 11,
        xFlexesThree: 0,
        yFlexesOne: 0,
        yFlexesTwo: 0,
        yFlexesThree: 0);
  }
}

class FlexibleImage extends StatelessWidget {
  final String imgPath;
  final int xFlexesOne;
  final int xFlexesTwo;
  final int xFlexesThree;
  final int yFlexesOne;
  final int yFlexesTwo;
  final int yFlexesThree;

  const FlexibleImage({
    super.key,
    required this.imgPath,
    required this.xFlexesOne,
    required this.xFlexesTwo,
    required this.xFlexesThree,
    required this.yFlexesOne,
    required this.yFlexesTwo,
    required this.yFlexesThree,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (yFlexesOne > 0)
          Flexible(
            flex: yFlexesOne,
            fit: FlexFit.tight,
            child: const SizedBox(),
          ),
        Flexible(
          flex: yFlexesTwo,
          fit: FlexFit.tight,
          child: Row(
            children: [
              if (xFlexesOne > 0)
                Flexible(
                  flex: xFlexesOne,
                  fit: FlexFit.tight,
                  child: const SizedBox(),
                ),
              Flexible(
                flex: xFlexesTwo,
                fit: FlexFit.tight,
                child: Image.asset(
                  imgPath,
                  fit: BoxFit.fill,
                ),
              ),
              if (xFlexesThree > 0)
                Flexible(
                  flex: xFlexesThree,
                  fit: FlexFit.tight,
                  child: const SizedBox(),
                ),
            ],
          ),
        ),
        if (yFlexesThree > 0)
          Flexible(
            flex: yFlexesThree,
            fit: FlexFit.tight,
            child: const SizedBox(),
          ),
      ],
    );
  }
}
</sub>

以下是屏幕截图

normally it should look like this. but this happens when I change the size of the screen.

最初,这种情况发生在行小部件上,所以我try 将行中的列更改为列中的行,但现在发生了这种情况

推荐答案

我相信这个解决方案是有效的.我认为这样做有点难看,但我不确定有没有更好的方法.替换

              Flexible(
                flex: xFlexesTwo,
                fit: FlexFit.tight,
                child: Image.asset(
                  imgPath,
                  fit: BoxFit.fill,
                ),
              ),

使用

              Flexible(
                flex: xFlexesTwo,
                fit: FlexFit.tight,
                child: LayoutBuilder(
                  builder: (context, constraints) {
                    return ConstrainedBox(
                      constraints: constraints.copyWith(minHeight: constraints.maxHeight != double.infinity ? constraints.maxHeight : constraints.minHeight),
                      child: Image.asset(
                        imgPath,
                        fit: BoxFit.fill,
                      ),
                    );
                  }
                ),
              ),

The reason why it doesn't work 使用out is when having rows in columns the children in the row will have their constrains as a set width but their height as a range between 0 and the available height. This solution makes it that it sees it as a fixed height.

我还认为紫色的需要yFlexesTwo: 1分.

Flutter相关问答推荐

壁炉有序在Flutter

在Flutter 中将参数从一个类传递到另一个类

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

如何在Ffltter Chrome应用程序中使用webview_fltter_web显示自定义html而不是外部uri?

点击表情符号按钮后,Flutter 文本域文本编辑控制器将文本恢复为原始值

我怎样才能在Ffltter中显示html布局链接?

DART:如何判断有时可以为空的泛型类型?

如何将小部件代码移动到另一个文件以获得更好的可读性

使用 forLoops 在 Dart 中进行单元测试会返回 stackoverflow

了解多Provider 和流

Flutter:如何设置 PersistentFooterButtons 背景?

如何根据应用程序主题(深色和浅色)更改谷歌 map 的主题?

如何在图像上点或(图像的一部分)在 flutter 中点击?

String类型不是Widget类型的子类型?在dart 中

如何从 Firebase `get()` 获取 `DocumentReference`

如何在 Flutter 执行通知时运行后台代码?

如何让多个图标转到不同的网址

如何在 flutter 中更改日期 Select 器 colored颜色 ?

在 FAB 上添加表格小部件单击 FLUTTER

在右侧从 a 到 z Flutter 字母列表