Flutter布局:并排渲染两个小部件

dar*_*ong 2 layout flutter

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

在 PerformLayout() 期间抛出以下断言:BoxConstraints 强制无限宽度。这些无效约束由以下函数提供给 RenderSemanticsAnnotations 的 layout() 函数,该函数可能计算有问题的无效约束: RenderConstrainedBox.performLayout 违规约束为: BoxConstraints(w=Infinity, 50.0<=h<=Infinity)

但是,如果我只渲染小部件而不使用“行”小部件,则所有内容都会按应有的方式呈现。

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
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图创建的布局结构:

在此输入图像描述

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

Wil*_*las 6

将 Row 的孩子包裹起来Expanded()

Row(
  children: [
    Expanded(
      child: SellsCard(),
    ),
    Expanded(
      child: SellsCard(),
    ),
  ]
)
Run Code Online (Sandbox Code Playgroud)