在列中颤动不同的对齐方式

Igo*_*ryl 4 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))
              ],
            )
           ],
         )
        ]),
        ),
       ),
     );
   }
Run Code Online (Sandbox Code Playgroud)

结果,我有三行对齐开始。

我怎样才能Text()在中心对齐第一个?让步性的文字和包装ExpandedContainerStack没有帮助,或者我这样做是错误的方式。提前致谢!

编辑

build()附上全方法

ibh*_*ana 9

问题:

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

解决方案:

你需要ColumnExpanded或包裹你的Flexible

像下面。

Expanded(
  child: Column(
    children: <Widget>[
      ...
    ],
  ),
)
Run Code Online (Sandbox Code Playgroud)

或者

Flexible(
  child: Column(
    children: <Widget>[
      ...
    ],
  ),
)
Run Code Online (Sandbox Code Playgroud)

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

像下面这样:

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

或者

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


Pra*_*ndo 3

更新 您还没有为该列指定宽度,因此它不知道它可以占用的剩余宽度。因此,请将您的列包裹在柔性材料中,这将使其具有剩余的宽度以扩展。

  Flexible(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(height: 16),
                  Row(
Run Code Online (Sandbox Code Playgroud)

  • 但我有一个不同的父母。即使它有效,如何使用我的布局? (2认同)