やりたいこと
以下のように、Rowの中のText「World」を右端に配置したい。

修正前

// 修正前コード
Container(
color: Colors.grey.withAlpha(50),
child: Row(
children: <Widget>[
Text(
"Hello",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
Text( // これを右端に寄せたい
"World",
style: TextStyle(fontSize: 16),
),
],
),
)
方法1 Expanded
Expanded
を使うと実現できるが、階層が深くなるのでベストな方法ではない。
Container(
color: Colors.grey.withAlpha(50),
child: Row(
children: <Widget>[
Expanded( // <= 追加
child: Text(
"Hello",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
Text(
"World",
style: TextStyle(fontSize: 16),
),
],
),
)
方法2 MainAxisAlignment.spaceBetween
Rowに MainAxisAlignment.spaceBetween
を指定する。
これが一番ベストかな
Container(
color: Colors.grey.withAlpha(50),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, // <= 追加
children: <Widget>[
Text(
"Hello",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
Text(
"World",
style: TextStyle(fontSize: 16),
),
],
),
)
方法3 Spacer
TextとTextの間にSpacer
を插入する。
これもシンプルな方法で良い
Container(
color: Colors.grey.withAlpha(50),
child: Row(
children: <Widget>[
Text(
"Hello",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
Spacer(), // <= 追加
Text(
"World",
style: TextStyle(fontSize: 16),
),
],
),
)
使用コード