下面的页面小部件抛出异常 运行'package:flutter/src/rendering/box. dart': box.dart:1 断言失败:第1965行位置12:'hasSize'

相关的导致错误的小部件是 脚手架

断言失败:1965行位置12:‘hasSize’

相关的导致错误的小部件是 FittedBox`

但是,一旦我使小部件变得无状态或删除FittedBox小部件,运行时错误就消失了.


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

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  String _result = '';

  @override
  Widget build(BuildContext context) {
    Widget result = FittedBox(
      child: Text(_result),
    );

    Widget actionButton = ElevatedButton(
      onPressed: () {
        setState(() {
          _result = 'Text generated';
        });
      },
      child: const Text('Press me'),
    );

    Widget bottom = Container(
      height: 80,
      color: Colors.orange,
      alignment: Alignment.center,
      child: const Text("Footer"),
    );

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('My App'),
      ),
      body: Padding(
        padding: const EdgeInsets.only(top: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            const Flexible(
              flex: 15,
              child: TextField(),
            ),
            Expanded(
              flex: 10,
              child: result,
            ),
            Container(
              height: 50,
              child: Text('Somre data'),
            ),
            const Expanded(
              flex: 56,
              child: Text('Some content'),
            ),
            bottom,
          ],
        ),
      ),
    );
  }
}

我遇到了一种情况,输出结果可以长达30个字符.我希望结果内容显示在一行和最大尺寸,同时遵循Flex属性指定的约束.

推荐答案

解决方案是通过容器指定结果小部件的宽度.

Expanded(
    flex: 10,
    child: result,
),

将上述代码更新为

Expanded(
    flex: 10,
    child: Container(
        width: screenWidth,
        child: result,
    ),
),

其中,creenWidth是通过媒体查询获取的.

Flutter相关问答推荐

Flutter ReorderableListView在可滚动父级中无法完美工作

Flutter 对齐:不适用于多行文字

Ffmpeg_kit_fltter:^6.0.3版权问题

在Ffltter中,我已经使用了Ffltter_BLOC Cupit和Pusher,但当获得新数据时,UI没有更新

Flutter如何将2个以上的值传递到ListView上的另一个屏幕?

当 Dart SDK 版本范围不匹配时,为什么依赖解析器不会抛出错误?

「Flutter」错误:没有名为'kind'的命名参数

Flutter蓝牙 - 如何通过蓝牙从设备中获取文件?

在 Flutter 中更改喜欢按钮的 colored颜色

用户文档 ID 与 Firestore 中的用户 ID 相同

围绕父组件旋转位置组件

启动画面中的淡入淡出图像

有没有办法在没有收缩包装的情况下在列中使用 ListView.Builder?

Flutter可滚动定位列表支持rtl方向吗?

Flutter 中的 Firebase 后台消息处理程序产生重复的隔离并运行应用程序两次

如何按键在 map 列表中搜索

构建失败 - 无法解析 io.grpc:grpc-core:[1.28.0]. (是的,我已经升级到 mavenCentral())

type '_InternalLinkedHashMap' 不是使用 http 包的'String' 类型的子类型

Flutter:滚动一个ListWheelScrollView,无法获取选中项的状态

Flutter Bloc Todo 示例 - 收听存储库中的单个 todo