Flutterを学んでいる人が、width:100のウィジェットの幅が100ピクセルではない理由を尋ねた場合、デフォルトの答えは、「そのウィジェットをCenter内に配置するように指示すること」です。
Don’t do that.
そうした場合、彼らは何度も何度も戻ってきて、FittedBoxが機能していない理由、その列がオーバーフローしている理由、またはIntrinsicWidthが何をしているのかを尋ねます。
Instead, first tell them that Flutter layout is very different from HTML layout (which is probably where they’re coming from), and then make them memorize the following rule:
Constraints go down. Sizes go up. Parent sets position.
Flutterのレイアウトは、このルールを知らなければ実際には理解できないため、Flutterの開発者は早い段階でそれを学ぶ必要があります。
さらに詳細に:
- ウィジェットは、その親から自身の制約を取得します。 制約は、minimum width,とmaximum width、およびminimum heightとmaximum heightの4つのdoubleのセットです。
- 次に、ウィジェットは自分の子ウィジェットを一つずつ調べます。 ウィジェットは1つずつ、子に制約(constraints)が何であるか(子ごとに異なる可能性があります)を伝え、次に各子にどのサイズにしたいかを尋ねます。
- 次に、ウィジェットはその子を1つずつ(水平方向にx軸に、垂直方向にy軸に)配置します。
- そして最後に、ウィジェットはその親に自身のサイズについて通知します(もちろん、元の制約内で)。
たとえば、作成されたウィジェットにパディングのあるColumnが含まれていて、その2つの子を次のようにレイアウトしたい場合:
Widget間のやりとりは以下のようになります。
Widget:「Parentさん、私の制約(constraints)はどうなりますか?」
Parent:「あなた(Widget)は幅は80〜300ピクセル、高さは30〜85ピクセルである必要があります。」
Widget:「うーん、5ピクセルのパディングが必要なので、子ウィジェットは最大で幅290ピクセル、高さ75ピクセルにすることができます。」
Widget:「最初の子さん、あなたは幅0〜290ピクセル、高さ0〜75である必要があります。」
First child:「では、自分は幅290ピクセル、高さ20ピクセルにします。」
Widget:「うーん、2番目の子ウィジェットを最初の子ウィジェットの下に配置したいので、2番目の子の高さは55ピクセルしか残りません。」
Widget:「2番目の子ねえ、あなたは幅0から290、高さ0から55でなければなりません。」
Second child:「OK、自分は幅140ピクセル、高さ30ピクセルにしたいです。」
Widget:「結構。 私の最初の子供は位置x:5とy:5で、2番目の子供はx:80とy:25です。」
Widget:「親の皆さん、(ここまでのやりとりで)私のサイズは幅300ピクセル、高さ60ピクセルに決まりました。」
Contents
Limitations(制限)
上記のレイアウトルールの結果として、Flutterのレイアウトエンジンにはいくつかの重要な制限があります。
- ウィジェットは、親から与えられた制約の範囲内でのみ、ウィジェット自体のサイズを決定できます。 つまり、ウィジェットは常に(自身が望む)必要なサイズになるとは限りません(親ウィジェットの制約があるから)。
- ウィジェットの位置を決定するのはウィジェットの親であるため、ウィジェット自身は画面内での自身の位置を認識できず、決定しません。
- 親のサイズと位置も(親の)親に依存するため、ツリー全体を考慮せずにウィジェットのサイズと位置を正確に定義することは不可能です。
Example(例)
import 'package:flutter/material.dart'; void main() => runApp(HomePage()); const red = Colors.red; const green = Colors.green; const blue = Colors.blue; const big = const TextStyle(fontSize: 30); ////////////////////////////////////////////////// class HomePage extends StatelessWidget { @override Widget build(BuildContext context) => FlutterLayoutArticle(<Example>[ Example1(), Example2(), Example3(), Example4(), Example5(), Example6(), Example7(), Example8(), Example9(), Example10(), Example11(), Example12(), Example13(), Example14(), Example15(), Example16(), Example17(), Example18(), Example19(), Example20(), Example21(), Example22(), Example23(), Example24(), Example25(), Example26(), Example27(), Example28(), Example29(), ]); } ////////////////////////////////////////////////// abstract class Example extends StatelessWidget { String get code; String get explanation; } ////////////////////////////////////////////////// class FlutterLayoutArticle extends StatefulWidget { final List<Example> examples; FlutterLayoutArticle(this.examples); @override _FlutterLayoutArticleState createState() => _FlutterLayoutArticleState(); } ////////////////////////////////////////////////// class _FlutterLayoutArticleState extends State<FlutterLayoutArticle> { int count; Widget example; String code; String explanation; @override void initState() { count = 1; code = Example1().code; explanation = Example1().explanation; super.initState(); } @override void didUpdateWidget(FlutterLayoutArticle oldWidget) { super.didUpdateWidget(oldWidget); var example = widget.examples[count - 1]; code = example.code; explanation = example.explanation; } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Layout Article', home: SafeArea( child: Material( color: Colors.black, child: FittedBox( child: Container( width: 400, height: 670, color: Color(0xFFCCCCCC), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: ConstrainedBox( constraints: BoxConstraints.tightFor( width: double.infinity, height: double.infinity), child: widget.examples[count - 1])), Container( height: 50, width: double.infinity, color: Colors.black, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( mainAxisSize: MainAxisSize.min, children: [ for (int i = 0; i < widget.examples.length; i++) Container( width: 58, padding: const EdgeInsets.only(left: 4.0, right: 4.0), child: button(i + 1)), ], ), ), ), Container( child: Scrollbar( child: SingleChildScrollView( key: ValueKey(count), child: Padding( padding: const EdgeInsets.all(10.0), child: Column( children: <Widget>[ Center(child: Text(code)), SizedBox(height: 15), Text(explanation, style: TextStyle( color: Colors.blue[900], fontStyle: FontStyle.italic)), ], ), )), ), height: 273, color: Colors.grey[50]), ], ), ), )), ), ); } Widget button(int exampleNumber) => Button( key: ValueKey('button$exampleNumber'), isSelected: this.count == exampleNumber, exampleNumber: exampleNumber, onPressed: () { showExample( exampleNumber, widget.examples[exampleNumber - 1].code, widget.examples[exampleNumber - 1].explanation, ); }, ); void showExample(int exampleNumber, String code, String explanation) => setState(() { this.count = exampleNumber; this.code = code; this.explanation = explanation; }); } ////////////////////////////////////////////////// class Button extends StatelessWidget { final Key key; final bool isSelected; final int exampleNumber; final VoidCallback onPressed; Button({ this.key, this.isSelected, this.exampleNumber, this.onPressed, }) : super(key: key); @override Widget build(BuildContext context) { return TextButton( style: TextButton.styleFrom( primary: Colors.white, backgroundColor: isSelected ? Colors.grey : Colors.grey[800], ), child: Text(exampleNumber.toString()), onPressed: () { Scrollable.ensureVisible( context, duration: Duration(milliseconds: 350), curve: Curves.easeOut, alignment: 0.5, ); onPressed(); }); } } ////////////////////////////////////////////////// class Example1 extends Example { final String code = 'Container(color: red)'; final String explanation = 'The screen is the parent of the Container, ' 'and it forces the Container to be exactly the same size as the screen.' '\n\n' 'So the Container fills the screen and paints it red.'; @override Widget build(BuildContext context) { return Container(color: red); } } ////////////////////////////////////////////////// class Example2 extends Example { final String code = 'Container(width: 100, height: 100, color: red)'; final String explanation = 'The red Container wants to be 100x100, but it can\'t, ' 'because the screen forces it to be exactly the same size as the screen.' '\n\n' 'So the Container fills the screen.'; @override Widget build(BuildContext context) { return Container(width: 100, height: 100, color: red); } } ////////////////////////////////////////////////// class Example3 extends Example { final String code = 'Center(\n' ' child: Container(width: 100, height: 100, color: red))'; final String explanation = 'The screen forces the Center to be exactly the same size as the screen,' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' 'Now the Container can indeed be 100x100.'; @override Widget build(BuildContext context) { return Center( child: Container(width: 100, height: 100, color: red), ); } } ////////////////////////////////////////////////// class Example4 extends Example { final String code = 'Align(\n' ' alignment: Alignment.bottomRight,\n' ' child: Container(width: 100, height: 100, color: red))'; final String explanation = 'This is different from the previous example in that it uses Align instead of Center.' '\n\n' 'Align also tells the Container that it can be any size it wants, but if there is empty space it won\'t center the Container. ' 'Instead, it aligns the Container to the bottom-right of the available space.'; @override Widget build(BuildContext context) { return Align( alignment: Alignment.bottomRight, child: Container(width: 100, height: 100, color: red), ); } } ////////////////////////////////////////////////// class Example5 extends Example { final String code = 'Center(\n' ' child: Container(\n' ' color: red,\n' ' width: double.infinity,\n' ' height: double.infinity))'; final String explanation = 'The screen forces the Center to be exactly the same size as the screen,' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' 'The Container wants to be of infinite size, but since it can\'t be bigger than the screen, it just fills the screen.'; @override Widget build(BuildContext context) { return Center( child: Container(width: double.infinity, height: double.infinity, color: red), ); } } ////////////////////////////////////////////////// class Example6 extends Example { final String code = 'Center(child: Container(color: red))'; final String explanation = 'The screen forces the Center to be exactly the same size as the screen,' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' '\n\n' 'Since the Container has no child and no fixed size, it decides it wants to be as big as possible, so it fills the whole screen.' '\n\n' 'But why does the Container decide that? ' 'Simply because that\'s a design decision by those who created the Container widget. ' 'It could have been created differently, and you have to read the Container documentation to understand how it behaves, depending on the circumstances. '; @override Widget build(BuildContext context) { return Center( child: Container(color: red), ); } } ////////////////////////////////////////////////// class Example7 extends Example { final String code = 'Center(\n' ' child: Container(color: red\n' ' child: Container(color: green, width: 30, height: 30)))'; final String explanation = 'The screen forces the Center to be exactly the same size as the screen,' 'so the Center fills the screen.' '\n\n' 'The Center tells the red Container that it can be any size it wants, but not bigger than the screen.' 'Since the red Container has no size but has a child, it decides it wants to be the same size as its child.' '\n\n' 'The red Container tells its child that if can be any size it wants, but not bigger than the screen.' '\n\n' 'The child is a green Container that wants to be 30x30.' '\n\n' 'Since the red `Container` has no size but has a child, it decides it wants to be the same size as its child. ' 'The red color isn\'t visible, since the green Container entirely covers all of the red Container.'; @override Widget build(BuildContext context) { return Center( child: Container( color: red, child: Container(color: green, width: 30, height: 30), ), ); } } ////////////////////////////////////////////////// class Example8 extends Example { final String code = 'Center(\n' ' child: Container(color: red\n' ' padding: const EdgeInsets.all(20.0),\n' ' child: Container(color: green, width: 30, height: 30)))'; final String explanation = 'The red Container sizes itself to its children size, but it takes its own padding into consideration. ' 'So it is also 30x30 plus padding. ' 'The red color is visible because of the padding, and the green Container has the same size as in the previous example.'; @override Widget build(BuildContext context) { return Center( child: Container( padding: const EdgeInsets.all(20.0), color: red, child: Container(color: green, width: 30, height: 30), ), ); } } ////////////////////////////////////////////////// class Example9 extends Example { final String code = 'ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 10, height: 10)))'; final String explanation = 'You might guess that the Container has to be between 70 and 150 pixels, but you would be wrong. ' 'The ConstrainedBox only imposes ADDITIONAL constraints from those it receives from its parent.' '\n\n' 'Here, the screen forces the ConstrainedBox to be exactly the same size as the screen, ' 'so it tells its child Container to also assume the size of the screen, ' 'thus ignoring its \'constraints\' parameter.'; @override Widget build(BuildContext context) { return ConstrainedBox( constraints: BoxConstraints(minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150), child: Container(color: red, width: 10, height: 10), ); } } ////////////////////////////////////////////////// class Example10 extends Example { final String code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 10, height: 10))))'; final String explanation = 'Now, Center allows ConstrainedBox to be any size up to the screen size.' '\n\n' 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 10 pixels, so it will end up having 70 (the MINIMUM).'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: BoxConstraints(minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150), child: Container(color: red, width: 10, height: 10), ), ); } } ////////////////////////////////////////////////// class Example11 extends Example { final String code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 1000, height: 1000))))'; final String explanation = 'Center allows ConstrainedBox to be any size up to the screen size.' 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 1000 pixels, so it ends up having 150 (the MAXIMUM).'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: BoxConstraints(minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150), child: Container(color: red, width: 1000, height: 1000), ), ); } } ////////////////////////////////////////////////// class Example12 extends Example { final String code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 100, height: 100))))'; final String explanation = 'Center allow ConstrainedBox to be any size up to the screen size.' 'ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 100 pixels, and that\'s the size it has, since that\'s between 70 and 150.'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: BoxConstraints(minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150), child: Container(color: red, width: 100, height: 100), ), ); } } ////////////////////////////////////////////////// class Example13 extends Example { final String code = 'UnconstrainedBox(\n' ' child: Container(color: red, width: 20, height: 50));'; final String explanation = 'The screen forces the UnconstrainedBox to be exactly the same size as the screen.' 'However, the UnconstrainedBox lets its child Container be any size it wants.'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: red, width: 20, height: 50), ); } } ////////////////////////////////////////////////// class Example14 extends Example { final String code = 'UnconstrainedBox(\n' ' child: Container(color: red, width: 4000, height: 50));'; final String explanation = 'The screen forces the UnconstrainedBox to be exactly the same size as the screen, ' 'and UnconstrainedBox lets its child Container be any size it wants.' '\n\n' 'Unfortunately, in this case the Container has 4000 pixels of width and is too big to fit in the UnconstrainedBox, ' 'so the UnconstrainedBox displays the much dreaded "overflow warning".'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: red, width: 4000, height: 50), ); } } ////////////////////////////////////////////////// class Example15 extends Example { final String code = 'OverflowBox(\n' ' minWidth: 0.0,' ' minHeight: 0.0,' ' maxWidth: double.infinity,' ' maxHeight: double.infinity,' ' child: Container(color: red, width: 4000, height: 50));'; final String explanation = 'The screen forces the OverflowBox to be exactly the same size as the screen, ' 'and OverflowBox lets its child Container be any size it wants.' '\n\n' 'OverflowBox is similar to UnconstrainedBox, and the difference is that it won\'t display any warnings if the child doesn\'t fit the space.' '\n\n' 'In this case the Container is 4000 pixels wide, and is too big to fit in the OverflowBox, ' 'but the OverflowBox simply shows as much as it can, with no warnings given.'; @override Widget build(BuildContext context) { return OverflowBox( minWidth: 0.0, minHeight: 0.0, maxWidth: double.infinity, maxHeight: double.infinity, child: Container(color: red, width: 4000, height: 50), );} } ////////////////////////////////////////////////// class Example16 extends Example { final String code = 'UnconstrainedBox(\n' ' child: Container(color: Colors.red, width: double.infinity, height: 100));'; final String explanation = 'This won\'t render anything, and you\'ll see an error in the console.' '\n\n' 'The UnconstrainedBox lets its child be any size it wants, ' 'however its child is a Container with infinite size.' '\n\n' 'Flutter can\'t render infinite sizes, so it throws an error with the following message: ' '"BoxConstraints forces an infinite width."'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: Colors.red, width: double.infinity, height: 100), ); } } ////////////////////////////////////////////////// class Example17 extends Example { final String code = 'UnconstrainedBox(\n' ' child: LimitedBox(maxWidth: 100,\n' ' child: Container(color: Colors.red,\n' ' width: double.infinity, height: 100));'; final String explanation = 'Here you won\'t get an error anymore, ' 'because when the LimitedBox is given an infinite size by the UnconstrainedBox, ' 'it passes a maximum width of 100 down to its child.' '\n\n' 'If you swap the UnconstrainedBox for a Center widget, ' 'the LimitedBox won\'t apply its limit anymore (since its limit is only applied when it gets infinite constraints), ' 'and the width of the Container is allowed to grow past 100.' '\n\n' 'This explains the difference between a LimitedBox and a ConstrainedBox.'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: LimitedBox( maxWidth: 100, child: Container(color: Colors.red, width: double.infinity, height: 100), ), ); } } ////////////////////////////////////////////////// class Example18 extends Example { final String code = 'FittedBox(\n' ' child: Text(\'Some Example Text.\'));'; final String explanation = 'The screen forces the FittedBox to be exactly the same size as the screen.' 'The Text has some natural width (also called its intrinsic width) that depends on the amount of text, its font size, and so on.' '\n\n' 'The FittedBox lets the Text be any size it wants, ' 'but after the Text tells its size to the FittedBox, ' 'the FittedBox scales the Text until it fills all of the available width.'; @override Widget build(BuildContext context) { return FittedBox( child: Text('Some Example Text.'), ); } } ////////////////////////////////////////////////// class Example19 extends Example { final String code = 'Center(\n' ' child: FittedBox(\n' ' child: Text(\'Some Example Text.\')));'; final String explanation = 'But what happens if you put the FittedBox inside of a Center widget? ' 'The Center lets the FittedBox be any size it wants, up to the screen size.' '\n\n' 'The FittedBox then sizes itself to the Text, and lets the Text be any size it wants.' '\n\n' 'Since both FittedBox and the Text have the same size, no scaling happens.'; @override Widget build(BuildContext context) { return Center( child: FittedBox( child: Text('Some Example Text.'), ), ); } } //////////////////////////////////////////////////// class Example20 extends Example { final String code = 'Center(\n' ' child: FittedBox(\n' ' child: Text(\'…\')));'; final String explanation = 'However, what happens if FittedBox is inside of a Center widget, but the Text is too large to fit the screen?' '\n\n' 'FittedBox tries to size itself to the Text, but it can\'t be bigger than the screen. ' 'It then assumes the screen size, and resizes Text so that it fits the screen, too.'; @override Widget build(BuildContext context) { return Center( child: FittedBox( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ), ); } } ////////////////////////////////////////////////// class Example21 extends Example { final String code = 'Center(\n' ' child: Text(\'…\'));'; final String explanation = 'If, however, you remove the FittedBox, ' 'the Text gets its maximum width from the screen, ' 'and breaks the line so that it fits the screen.'; @override Widget build(BuildContext context) { return Center( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ); } } ////////////////////////////////////////////////// class Example22 extends Example { final String code = 'FittedBox(\n' ' child: Container(\n' ' height: 20.0, width: double.infinity));'; final String explanation = 'FittedBox can only scale a widget that is BOUNDED (has non-infinite width and height).' 'Otherwise, it won\'t render anything, and you\'ll see an error in the console.'; @override Widget build(BuildContext context) { return FittedBox( child: Container( height: 20.0, width: double.infinity, color: Colors.red, ), ); } } ////////////////////////////////////////////////// class Example23 extends Example { final String code = 'Row(children:[\n' ' Container(color: red, child: Text(\'Hello!\'))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; final String explanation = 'The screen forces the Row to be exactly the same size as the screen.' '\n\n' 'Just like an UnconstrainedBox, the Row won\'t impose any constraints onto its children, ' 'and instead lets them be any size they want.' '\n\n' 'The Row then puts them side-by-side, and any extra space remains empty.'; @override Widget build(BuildContext context) { return Row( children: [ Container(color: red, child: Text('Hello!', style: big)), Container(color: green, child: Text('Goodbye!', style: big)), ], ); } } ////////////////////////////////////////////////// class Example24 extends Example { final String code = 'Row(children:[\n' ' Container(color: red, child: Text(\'…\'))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; final String explanation = 'Since the Row won\'t impose any constraints onto its children, ' 'it\'s quite possible that the children might be too big to fit the available width of the Row.' 'In this case, just like an UnconstrainedBox, the Row displays the "overflow warning".'; @override Widget build(BuildContext context) { return Row( children: [ Container( color: red, child: Text('This is a very long text that won\'t fit the line.', style: big)), Container(color: green, child: Text('Goodbye!', style: big)), ], ); } } ////////////////////////////////////////////////// class Example25 extends Example { final String code = 'Row(children:[\n' ' Expanded(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; final String explanation = 'When a Row\'s child is wrapped in an Expanded widget, the Row won\'t let this child define its own width anymore.' '\n\n' 'Instead, it defines the Expanded width according to the other children, and only then the Expanded widget forces the original child to have the Expanded\'s width.' '\n\n' 'In other words, once you use Expanded, the original child\'s width becomes irrelevant, and is ignored.'; @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: Center( child: Container( color: red, child: Text('This is a very long text that won\'t fit the line.', style: big)), )), Container(color: green, child: Text('Goodbye!', style: big)), ], ); } } ////////////////////////////////////////////////// class Example26 extends Example { final String code = 'Row(children:[\n' ' Expanded(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Expanded(\n' ' child: Container(color: green, child: Text(\'Goodbye!\'))]'; final String explanation = 'If all of Row\'s children are wrapped in Expanded widgets, each Expanded has a size proportional to its flex parameter, ' 'and only then each Expanded widget forces its child to have the Expanded\'s width.' '\n\n' 'In other words, Expanded ignores the preffered width of its children.'; @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: Container( color: red, child: Text('This is a very long text that won\'t fit the line.', style: big))), Expanded(child: Container(color: green, child: Text('Goodbye!', style: big))), ], ); } } ////////////////////////////////////////////////// class Example27 extends Example { final String code = 'Row(children:[\n' ' Flexible(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Flexible(\n' ' child: Container(color: green, child: Text(\'Goodbye!\'))]'; final String explanation = 'The only difference if you use Flexible instead of Expanded, ' 'is that Flexible lets its child be SMALLER than the Flexible width, ' 'while Expanded forces its child to have the same width of the Expanded.' '\n\n' 'But both Expanded and Flexible ignore their children\'s width when sizing themselves.' '\n\n' 'This means that it\'s IMPOSSIBLE to expand Row children proportionally to their sizes. ' 'The Row either uses the exact child\'s width, or ignores it completely when you use Expanded or Flexible.'; @override Widget build(BuildContext context) { return Row( children: [ Flexible( child: Container( color: red, child: Text('This is a very long text that won\'t fit the line.', style: big))), Flexible(child: Container(color: green, child: Text('Goodbye!', style: big))), ], ); } } ////////////////////////////////////////////////// class Example28 extends Example { final String code = 'Scaffold(\n' ' body: Container(color: blue,\n' ' child: Column(\n' ' children: [\n' ' Text(\'Hello!\'),\n' ' Text(\'Goodbye!\')])))'; final String explanation = 'The screen forces the Scaffold to be exactly the same size as the screen,' 'so the Scaffold fills the screen.' '\n\n' 'The Scaffold tells the Container that it can be any size it wants, but not bigger than the screen.' '\n\n' 'When a widget tells its child that it can be smaller than a certain size, ' 'we say the widget supplies "loose" constraints to its child. More on that later.'; @override Widget build(BuildContext context) { return Scaffold( body: Container( color: blue, child: Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ); } } ////////////////////////////////////////////////// class Example29 extends Example { final String code = 'Scaffold(\n' ' body: Container(color: blue,\n' ' child: SizedBox.expand(\n' ' child: Column(\n' ' children: [\n' ' Text(\'Hello!\'),\n' ' Text(\'Goodbye!\')]))))'; final String explanation = 'If you want the Scaffold\'s child to be exactly the same size as the Scaffold itself, ' 'you can wrap its child with SizedBox.expand.' '\n\n' 'When a widget tells its child that it must be of a certain size, ' 'we say the widget supplies "tight" constraints to its child. More on that later.'; @override Widget build(BuildContext context) { return Scaffold( body: SizedBox.expand( child: Container( color: blue, child: Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ), ); } } //////////////////////////////////////////////////
Example1
Container(color: Colors.red)
スクリーンはContainerの親ウィジェットです。
Containerというのは2行上の
Container(color: Colors.red)
のこと。
そしてスクリーンはContainerに対し、スクリーンと同じサイズであることを要求します。
ですから、Containerはスクリーンと同じ領域を占有し、赤くペイントします。
Example2
Container(width: 100, height: 100, color: Colors.red)
↑赤いContainer自体は幅100,高さ100ですが、その大きさにはなれません。なぜなら、親であるスクリーンがContainerに対して、スクリーンと同じサイズであることを要求するからです。
ですから、Containerはスクリーンと同じ領域を占有します。
長くなるのでページ変えます。
参考