質問内容
FlutterのConstraintsについてご質問をさせていただきます。
以下のソースコード①は画面中央に赤色四角形のウィジェットを表示しているものになります。
Centerウィジェットは子であるContainerウィジェットに制約として
高さは画面の高さ以下、幅は画面の幅以下になるように与えています。
続いてソースコード②ではソースコード①のCenterウィジェットの部分のみをConstrainedBoxに書き換えたもので、制約はCenterと同じになるようにしたつもりだったのですが、ConstrainedBoxの制約はContainerウィジェットには効いておらず、Containerウィジェットは画面いっぱいに表示されてしまいます。
なぜソースコード②の方ではConstrainedBoxがContainerウィジェットに対して渡している制約は効かないことになるのでしょうか?
ご教授よろしくお願いいたします。
ソースコード①
Dart
1 Widget build(BuildContext context) { 2 return Scaffold( 3 body: Container( 4 color: Colors.blue[100], 5 constraints: const BoxConstraints.tightFor( 6 width: double.infinity, 7 height: double.infinity, 8 ), 9 child: Center( 10 child: Container( 11 width: 100, 12 height: 100, 13 color: Colors.red, 14 ), 15 ), 16 ), 17 ); 18 }
ソースコード②
Dart
1 Widget build(BuildContext context) { 2 return Scaffold( 3 body: Container( 4 color: Colors.blue[100], 5 constraints: const BoxConstraints.tightFor( 6 width: double.infinity, 7 height: double.infinity, 8 ), 9 child: ConstrainedBox( 10 constraints: const BoxConstraints( 11 minHeight: 0, 12 minWidth: 0, 13 maxHeight: double.infinity, 14 maxWidth: double.infinity, 15 ), 16 child: Container( 17 width: 100, 18 height: 100, 19 color: Colors.red, 20 ), 21 ), 22 ), 23 ); 24 }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/05/17 11:20 編集
2024/05/17 12:08
2024/05/17 12:48