【问题标题】:BoxConstraints forces an infinite height when streching containersBoxConstraints 在拉伸容器时强制使用无限高
【发布时间】:2020-09-15 13:09:45
【问题描述】:

我是 Android 开发的新手。我正在尝试使用行和列小部件垂直拉伸 3 个容器和水平拉伸 3 个容器。每次我在 Row 小部件中添加 crossAxisAlignment: CrossAxisAlignment.stretch, 时,我都会收到 BoxConstraints force an infinite height 错误。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Multichild Container Test'),
        ),
        body: SafeArea(
          child: Container(
            child: Column(
              children: <Widget>[
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.pink.shade900,
                      ),

                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.green.shade900,
                      ),

                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.deepPurple.shade900,
                      ),
                    ],
                  ),
                ),

                Container(
                  child: Row(
                    //crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.pink.shade900,
                      ),

                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.green.shade900,
                      ),

                      Container(
                        width: 60.0,
                        height: 100.0,
                        child: Text('Container 1'),
                        color: Colors.deepPurple.shade900,
                      ),
                    ],
                  ),
                )


              ],
            )
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: android-layout flutter flutter-layout


    【解决方案1】:

    您可以在下面使用复制粘贴运行完整代码
    您可以使用Expanded 和flex 为Container 提供高度
    代码sn-p

    Expanded(
                flex: 1,
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Multichild Container Test'),
            ),
            body: SafeArea(
              child: Container(
                  child: Column(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: <Widget>[
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.pink.shade900,
                          ),
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.green.shade900,
                          ),
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.deepPurple.shade900,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: <Widget>[
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.pink.shade900,
                          ),
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.green.shade900,
                          ),
                          Container(
                            width: 60.0,
                            height: 100.0,
                            child: Text('Container 1'),
                            color: Colors.deepPurple.shade900,
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              )),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2022-01-04
      • 1970-01-01
      • 2021-10-15
      • 2020-12-22
      • 2020-04-27
      • 2019-11-04
      • 2019-02-25
      • 2020-09-21
      相关资源
      最近更新 更多