在列中 flutter 不同的对齐方式

标签 flutter flutter-layout

我有一个包含三行的列:

@override
Widget build(BuildContext context) {
return GestureDetector(
  onTap: () {
    print('card of course clicked');
  },
  child: Card(
    shape: RoundedRectangleBorder(borderRadius: 
    BorderRadius.circular(8.0)),
    child: Container(
      height: 144,
      child: Row(children: [
        Padding(
            padding: EdgeInsets.all(16.0),
            child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(8.0)),
                child: Image.network(_model._schoolThumb))),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 16),
            Align(
              alignment: Alignment.center,
              child: Text(_model._schoolName,
                  style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w600,
                      fontSize: 18)),
            ),
            SizedBox(height: 12),
            Row(
              children: <Widget>[
                Icon(Icons.location_on, color: Colors.grey[700]),
                Container(width: 8),
                Text(_model._guides,
                    style: TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                        fontWeight: FontWeight.w400)),
              ],
            ),
            SizedBox(height: 12),
            Row(
              children: <Widget>[
                Icon(Icons.attach_money, color: Colors.grey[700]),
                Container(width: 8),
                Text(_model._priceFrom,
                    style: TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                        fontWeight: FontWeight.w400))
              ],
            )
           ],
         )
        ]),
        ),
       ),
     );
   }

结果,我开始对齐三行。

如何将第一个 Text() 居中对齐?为文本赋予属性并在 ExpandedContainerStack 中包装没有帮助,或者我做错了。提前致谢!

编辑

附上整个build()方法

最佳答案

问题:

默认情况下,Columnwidth 设置为最宽的child 小部件的width

解决方案:

您需要使用 ExpandedFlexible 包装您的 Column

如下所示。

Expanded(
  child: Column(
    children: <Widget>[
      ...
    ],
  ),
)

Flexible(
  child: Column(
    children: <Widget>[
      ...
    ],
  ),
)

然后需要使用 AlignCenter 小部件将对齐方式设置为您的子小部件,在您的例子中是 Text 小部件。

如下所示:

Align(
  alignment: Alignment.center,
  child: Text(
    "Test",
    style: TextStyle(
      color: Colors.black,
      fontWeight: FontWeight.w600,
      fontSize: 18,
    ),
  ),
),

Center(
  child: Text(
    "Test",
    style: TextStyle(
      color: Colors.black,
      fontWeight: FontWeight.w600,
      fontSize: 18,
    ),
  ),
),

关于在列中 flutter 不同的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56475512/

相关文章:

android - 如何在MyApp()中调用包含的另一个文件或main.dart文件中的include函数

容器内的 Flutter 文本

android - flutter : screen not scrolling up when keyboard appears in android

grid - 如何在 Flutter 中居中列和行项目?

list - 如何在 Flutter 中将 map 列表显示到 DropdownMenuItem

flutter - 为什么我收到 Equatable 错误 : The element type can't be assigned to the list type 'Object'

flutter - 无法在Android Studio中运行Flutter应用

flutter - 在 AlertDialog 之外构建小部件

flutter - 卡 borderOnForeground 未按预期工作

image - 在 Flutter 中将 Blob 显示为图像