我正在try 创建一个可滚动的页面(通过使用ListView)并支持响应(通过使用Flex,当屏幕宽度较小时,它将从行切换到列).它是有效的,但我很好奇为什么它需要设置容器的高度.有谁能帮我解释或给我适当的解决方案吗?

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    bool isScreenWide = MediaQuery.sizeOf(context).width >= 800;

    return Scaffold(
        body: ListView(
      children: [
        Container(
          height: 400, // need this line to make it works
          color: Colors.blue[100],

          child: Flex(
            direction: isScreenWide ? Axis.horizontal : Axis.vertical,
            children: [
              Expanded(
                child: Container(
                    padding: const EdgeInsets.all(20),
                    alignment: Alignment.topLeft,
                    child: const Text("1")),
              ),
              Expanded(
                child: Container(
                    padding: const EdgeInsets.all(20),
                    alignment: Alignment.topLeft,
                    child: const Text("2")),
              ),
            ],
          ),
        ),
      ],
    ));
  }
}

推荐答案

有几个细节,但我会试着向你解释.

默认的ListView有一个垂直的ScrollDirection,这是ListView的一个要求,它的子级在滚动方向上有一个不是无穷大的大小,这就是为什么你必须设置高度.这就是为什么你必须在容器的高度上设定一个尺寸.

ListView垂直滚动方向的高度是无限的,而Flex在列中的高度是无限的,这是冲突的.

我认为这将是最好的方式:


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Material App',
      home: Scaffold(
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    bool isScreenWide = MediaQuery.sizeOf(context).width >= 800;

    return Scaffold(
        body: ListView(
      scrollDirection: isScreenWide ? Axis.horizontal : Axis.vertical,
      children: [
        Flex(
          direction: isScreenWide ? Axis.horizontal : Axis.vertical,
          children: [
            Container(
                color: Colors.blue[100],
                padding: const EdgeInsets.all(20),
                alignment: Alignment.topLeft,
                child: const Padding(
                  padding: EdgeInsets.all(100),
                  child: Text("1"),
                )),
            Container(
                color: Colors.blue[200],
                padding: const EdgeInsets.all(20),
                alignment: Alignment.topLeft,
                child: const Padding(
                  padding: EdgeInsets.all(100),
                  child: Text("2"),
                )),
            Container(
                color: Colors.blue[300],
                padding: const EdgeInsets.all(20),
                alignment: Alignment.topLeft,
                child: const Padding(
                  padding: EdgeInsets.all(100),
                  child: Text("3"),
                )),
            Container(
                color: Colors.blue[400],
                padding: const EdgeInsets.all(20),
                alignment: Alignment.topLeft,
                child: const Padding(
                  padding: EdgeInsets.all(100),
                  child: Text("4"),
                )),
          ],
        ),
      ],
    ));
  }
}

enter image description here

enter image description here

enter image description here

Flutter相关问答推荐

为什么Flutter 翼扩展卡有上下边框?

什么是dart :这只是dart ?

如何让经过Firebase身份验证的用户能够将Zip文件上载到Firebase云存储?

Flutter :执行com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction时出现故障

为什么用户界面不会在Flight中的复选框中更改?

Flutter-Riverpod:如何只从提供者读取一次值

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

没有固定宽度的小部件可以约束文本小部件吗?

Dart/Flutter 泛型:类型 '(SearchFilterItemModel) => String' 不是类型 '(SearchFilterItemModel) => String' 的子类型

Flutter记录返回类型问题

如何将取消图标放置在右上角

#Flutter/Riverpod - 使用 AsyncNotifier 时是否可以仅调用错误?

具有 BLOC 和存储库模式的 Flutter Workmanager

Flutter 动画页面计数器文本

Riverpod 在使用冻结副本更新状态时触发重建,即使没有任何更改

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

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

Flutter - 检测手指何时进入容器

如何从用户 Flutter 导航抽屉中隐藏管理菜单

Flutter 评级栏包选中和未选中的容器