【问题标题】:Flutter layout: render two widgets side by sideFlutter 布局:并排渲染两个小部件
【发布时间】:2021-02-20 21:31:31
【问题描述】:

我正在尝试创建一个简单的布局,在同一行中插入 2 个小部件,我为此使用了 Row,但出现以下错误:

在 performLayout() 期间抛出了以下断言: BoxConstraints 强制无限宽度。这些无效的约束 由 RenderSemanticsAnnotations 的 layout() 函数提供 下面的函数,它可能计算了无效的约束 问题:RenderConstrainedBox.performLayout 有问题的约束 分别是:BoxConstraints(w=Infinity, 50.0

但是如果我只渲染小部件而不使用 Row 小部件,那么一切都会得到应有的渲染。

class CardWithTitle extends StatelessWidget {
  const CardWithTitle({Key key, this.title, this.child}) : super(key: key);

  final String title;
  final child;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Text(title),
            ],
          ),
          Container(
            width: double.infinity,
            constraints: BoxConstraints(minHeight: 50),
            child: Card(
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: child,
              ),
            ),
          )
        ],
      ),
    );
  }
}

class SellsCard extends StatelessWidget {
  SellsCard({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: CardWithTitle(
        title: 'Sells',
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Total',
            ),
            Text(
              '\$ 96.500,54'
            ),
            Text(
              'Lorem ipsum dolor'
            )
          ],
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<HomePage> {
  String _username;
  String _title;
  String _usernameAbbr;

  @override
  Widget build(BuildContext context) {
    _username = 'James';
    _title = 'Some title';
    _usernameAbbr = _username[0].toUpperCase();

    return Scaffold(
      appBar: AppBar(
        title: Text(_title),
        elevation: 0,
        actions: [
          Padding(
              padding: EdgeInsets.all(20.0),
              child: GestureDetector(
                onTap: () {},
                child: CircleAvatar(
                  backgroundColor: Theme.of(context).backgroundColor,
                  radius: 10.0,
                  child: FittedBox(
                    fit: BoxFit.fitWidth,
                    child: Text(_usernameAbbr),
                  ),
                ),
              ))
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SellsCard(), // <-- this is working
            SellsCard(), // <-- this is working
            Row(children: [SellsCard(), SellsCard()]) // <-- This is throwing the errors
          ],
        ),
      ),
    );
  }
}

这是我正在尝试创建的布局结构:

任何关于我做错了什么或如何将这两个项目放在同一行中的见解都会很棒。

【问题讨论】:

  • 我相信这里已经回答了这个问题:link

标签: flutter layout


【解决方案1】:

Expanded() 包裹你的 Row 的孩子:

Row(
  children: [
    Expanded(
      child: SellsCard(),
    ),
    Expanded(
      child: SellsCard(),
    ),
  ]
)

【讨论】:

  • 谢谢你,成功了。请您详细说明为什么需要用 Expanded 小部件包装小部件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多