【问题标题】:how to match height of a row widget which is tallest among other widgets如何匹配在其他小部件中最高的行小部件的高度
【发布时间】:2019-09-05 12:31:58
【问题描述】:

我定义了一行列小部件,但无法匹配行中定义的其他小部件的高度

我尝试过使用 Expand 、Intrinsic Height、width、Contrained box 以及在每列中定义容器。

预期结果是与父/其他行小部件匹配高度[在此处输入图像描述][1]

Widget columnOne(String t1, String t2) {
 return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8)),
child: Container(
  color: Colors.white,
  child: Column(
    mainAxisSize: MainAxisSize.max,
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: <Widget>[
      Container(
        color: Colors.white,
        child: Padding(
          padding:
          EdgeInsets.fromLTRB(10, 10, 10, 10),
          child: Align(
            alignment: Alignment.center,
            child: Text(t1,
                style: TextStyle(
                    fontSize: 30,
                    color: Colors.black,
                    backgroundColor: Colors.white,
                    fontFamily: 'FJ',fontWeight: FontWeight.bold
                )),
          )
        ),
      ),
      IntrinsicHeight(
        child: ConstrainedBox(
          constraints: const BoxConstraints(minWidth: double.infinity),
          child: Container(
              color: Colors.red,
              child: Padding(
                  padding: EdgeInsets.all(10),
                  child: Align(
                    alignment: Alignment.center,
                    child: Text(t2 ,
                        textAlign: TextAlign.center,
                        textWidthBasis: TextWidthBasis.parent,
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.black,
                          backgroundColor: Colors.red,
                        )),
                  ))),
        )
      )


    ],
  ),
),
 );

}

https://i.stack.imgur.com/HfYOl.png 当前结果如果我更改文本/溢出文本

https://i.stack.imgur.com/t9MTK.png 预计

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    从设计上看,卡底只能是一两行高。 在这种情况下,不依赖自动高度,而是使用固定值会更容易。

    import 'package:flutter/material.dart';
    import 'package:vector_math/vector_math.dart' as v_math;
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            backgroundColor: Colors.black,
            body: SafeArea(
              child: RowExample(),
            ),
          ),
        );
      }
    }
    
    class RowExample extends StatelessWidget {
      final List<List<String>> entities = [
        ['22', 'fdslfdssdf'],
        ['22', 'sdfdssf fd fasdf '],
        ['3242', 'sdsf'],
      ];
    
      @override
      Widget build(BuildContext context) {
        return Row(
          children: entities.map((item) => columnOne(item[0], item[1])).toList(),
        );
      }
    
      Widget columnOne(String t1, String t2) {
        return ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(8)),
          child: Container(
            height: 130,
            width: 100,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(8)),
              color: Colors.white,
            ),
            child: Column(
              children: <Widget>[
                Container(
                  height: 70,
                  color: Colors.white,
                  child: Padding(
                      padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(t1,
                            style: TextStyle(
                                fontSize: 30,
                                color: Colors.black,
                                backgroundColor: Colors.white,
                                fontFamily: 'FJ',
                                fontWeight: FontWeight.bold)),
                      )),
                ),
                Expanded(
                  child: Container(
                      color: Colors.red,
                      child: Padding(
                          padding: EdgeInsets.all(10),
                          child: Align(
                            alignment: Alignment.center,
                            child: Text(t2,
                                textAlign: TextAlign.center,
                                textWidthBasis: TextWidthBasis.parent,
                                style: TextStyle(
                                  fontSize: 16,
                                  color: Colors.black,
                                  backgroundColor: Colors.red,
                                )),
                          ))),
                )
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 感谢您的解决方案。我认为静态高度和宽度对于不同的屏幕尺寸来说是个坏主意,作为一个原生开发者,我总是使用匹配/换行、最小高度、最小宽度等,无论如何,现在将其作为解决方案。再次感谢您的努力。如果您有任何其他想法与您分享自动高度和宽度,请标记此问题,这也有助于其他人。
    • 添加了 Kherel。我想出了解决方案,请检查并支持我的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 2019-06-03
    • 2019-12-27
    • 2020-10-20
    • 2021-01-14
    • 1970-01-01
    • 2014-09-08
    • 2019-06-23
    相关资源
    最近更新 更多