Ekhlass commited on
Commit
f32f436
·
verified ·
1 Parent(s): b40e9ba

Upload flutter_constraints.txt

Browse files
Files changed (1) hide show
  1. flutter_constraints.txt +1966 -0
flutter_constraints.txt ADDED
@@ -0,0 +1,1966 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <topic_start>
2
+ Introduction to Flutter: Building Beautiful Cross-Platform Apps
3
+
4
+ Flutter is an open-source UI software development toolkit created by Google.
5
+ It is designed to build natively compiled applications for mobile, web, and desktop from a single codebase.
6
+ Flutter provides a fast, expressive, and flexible way to develop visually stunning
7
+ applications with high performance.
8
+ It is a powerful framework for building cross-platform applications with beautiful,
9
+ highly-customizable UIs. By leveraging Flutter's rich set of widgets, hot reload feature, and platform-specific integrations,
10
+ developers can create high-quality apps that run smoothly on multiple platforms with minimal effort.
11
+ <topic_end>
12
+ <topic_start>
13
+ Why Choose Flutter?
14
+
15
+ One of the primary advantages of Flutter is its ability to create beautiful,
16
+ highly-customized user interfaces (UI) quickly and efficiently. Here's why Flutter stands out:
17
+
18
+ 1- Single Codebase: With Flutter, you write one codebase that runs on multiple platforms, including iOS,
19
+ Android, web, and desktop. This means you can reach a broader audience with less effort.
20
+
21
+ 2- Expressive UI: Flutter uses a declarative approach to building UI, meaning you describe what you want your UI to look like,
22
+ and Flutter handles the rendering. This allows for highly expressive and customizable UI designs.
23
+
24
+ 3- Fast Development: Flutter's hot reload feature allows developers to instantly see changes they make
25
+ to the code reflected on the app UI, speeding up the development process significantly.
26
+
27
+ 4- Native Performance: Flutter apps are compiled directly to native machine code,
28
+ providing near-native performance on each platform.
29
+
30
+ 5- Rich Set of Widgets: Flutter comes with an extensive collection of pre-designed widgets for
31
+ building UI elements such as buttons, text inputs, sliders, and more. These widgets are highly
32
+ customizable and can be combined to create complex UI layouts.
33
+ <topic_end>
34
+ <topic_start>
35
+ Widgets: The Building Blocks of Flutter
36
+
37
+ In Flutter, everything is a widget. A widget is a lightweight,
38
+ reusable component that represents a part of the UI. Flutter provides two types of widgets:
39
+
40
+ 1- Stateless Widgets: These widgets are immutable and do not have any internal state.
41
+ Stateless widgets are used for UI elements that do not change over time, such as buttons, icons, or static text.
42
+
43
+ 2- Stateful Widgets: Stateful widgets maintain internal state that can change over time.
44
+ They are used for UI elements that need to update dynamically, such as input fields, animations, or lists.
45
+ <topic_end>
46
+ <topic_start>
47
+ How Flutter Works
48
+
49
+ At the heart of Flutter's architecture is the Flutter Engine,
50
+ which is written primarily in C++.
51
+ The Flutter Engine provides low-level rendering support through Google's Skia graphics library.
52
+ Here's a simplified overview of how Flutter works:
53
+
54
+ 1- UI Rendering: Flutter uses a custom rendering engine to draw UI elements directly to the screen,
55
+ bypassing the platform's native UI components. This allows for consistent UI across different platforms.
56
+
57
+ 2- Dart Programming Language: Flutter apps are written in the Dart programming language,
58
+ which was also developed by Google. Dart provides features such as a just-in-time (JIT) compiler for rapid development and a ahead-of-time (AOT) compiler for producing highly optimized native code.
59
+
60
+ 3- Widgets Layer: Developers use Flutter's extensive widget library to compose the UI of their applications.
61
+ Widgets are arranged in a tree hierarchy, with each widget responsible for rendering a specific part of the UI.
62
+
63
+ 4- Hot Reload: Flutter's hot reload feature allows developers to make changes to the code and see the results instantly without restarting the app.
64
+ This greatly speeds up the development process and enables iterative UI design
65
+
66
+ 5- Platform Channels: Flutter provides platform channels that allow communication between Flutter code and platform-specific code written in Java (for Android) or Objective-C/Swift (for iOS).
67
+ This enables developers to access platform-specific features and APIs when needed.
68
+ <topic_end>
69
+ <topic_start>
70
+ Understanding constraints in Flutter
71
+ info Note
72
+ If you are experiencing specific layout errors,
73
+ you might check out Common Flutter errors.
74
+ When someone learning Flutter asks you why some widget
75
+ with width: 100 isn’t 100 pixels wide,
76
+ the default answer is to tell them to put that widget
77
+ inside of a Center, right?
78
+ Don’t do that.
79
+ If you do, they’ll come back again and again,
80
+ asking why some FittedBox isn’t working,
81
+ why that Column is overflowing, or what
82
+ IntrinsicWidth is supposed to be doing.
83
+ Instead, first tell them that Flutter layout is very different
84
+ from HTML layout (which is probably where they’re coming from),
85
+ and then make them memorize the following rule:
86
+ Flutter layout can’t really be understood without knowing
87
+ this rule, so Flutter developers should learn it early on.
88
+ In more detail:
89
+ For example, if a composed widget contains a column
90
+ with some padding, and wants to lay out its two children
91
+ as follows:
92
+
93
+ The negotiation goes something like this:
94
+ Widget: “Hey parent, what are my constraints?”
95
+ Parent: “You must be from 0 to 300 pixels wide,
96
+ and 0 to 85 tall.”
97
+ Widget: “Hmmm, since I want to have 5 pixels of padding,
98
+ then my children can have at most 290 pixels of width
99
+ and 75 pixels of height.”
100
+ Widget: “Hey first child, You must be from 0 to 290
101
+ pixels wide, and 0 to 75 tall.”
102
+ First child: “OK, then I wish to be 290 pixels wide,
103
+ and 20 pixels tall.”
104
+ Widget: “Hmmm, since I want to put my second child below the
105
+ first one, this leaves only 55 pixels of height for
106
+ my second child.”
107
+ Widget: “Hey second child, You must be from 0 to 290 wide,
108
+ and 0 to 55 tall.”
109
+ Second child: “OK, I wish to be 140 pixels wide,
110
+ and 30 pixels tall.”
111
+ Widget: “Very well. My first child has position x: 5 and y: 5,
112
+ and my second child has x: 80 and y: 25.”
113
+ Widget: “Hey parent, I’ve decided that my size is going to be 300
114
+ pixels wide, and 60 pixels tall.”
115
+ <topic_end>
116
+ <topic_start>
117
+ Limitations
118
+ Flutter’s layout engine is designed to be a one-pass process.
119
+ This means that Flutter lays out its widgets very efficiently,
120
+ but does result in a few limitations:
121
+ A widget can decide its own size only within the
122
+ constraints given to it by its parent.
123
+ This means a widget usually
124
+ can’t have any size it wants.
125
+ A widget can’t know and doesn’t decide its own position
126
+ in the screen, since it’s the widget’s parent who decides
127
+ the position of the widget.
128
+ Since the parent’s size and position, in its turn,
129
+ also depends on its own parent, it’s impossible to
130
+ precisely define the size and position of any widget
131
+ without taking into consideration the tree as a whole.
132
+ If a child wants a different size from its parent and
133
+ the parent doesn’t have enough information to align it,
134
+ then the child’s size might be ignored.
135
+ Be specific when defining alignment.
136
+ In Flutter, widgets are rendered by their underlying
137
+ RenderBox objects. Many boxes in Flutter,
138
+ especially those that just take a single child,
139
+ pass their constraint on to their children.
140
+ Generally, there are three kinds of boxes,
141
+ in terms of how they handle their constraints:
142
+ Some widgets, for example Container,
143
+ vary from type to type based on their constructor arguments.
144
+ The Container constructor defaults
145
+ to trying to be as big as possible, but if you give it a width,
146
+ for instance, it tries to honor that and be that particular size.
147
+ Others, for example Row and Column (flex boxes)
148
+ vary based on the constraints they are given,
149
+ as described in the Flex section.
150
+ <topic_end>
151
+ <topic_start>
152
+ Examples
153
+ For an interactive experience, use the following DartPad.
154
+ Use the numbered horizontal scrolling bar to switch between
155
+ 29 different examples.
156
+ <code_start>
157
+ import 'package:flutter/material.dart';
158
+
159
+ void main() => runApp(const HomePage());
160
+
161
+ const red = Colors.red;
162
+ const green = Colors.green;
163
+ const blue = Colors.blue;
164
+ const big = TextStyle(fontSize: 30);
165
+
166
+
167
+ class HomePage extends StatelessWidget {
168
+ const HomePage({super.key});
169
+
170
+ @override
171
+ Widget build(BuildContext context) {
172
+ return const FlutterLayoutArticle([
173
+ Example1(),
174
+ Example2(),
175
+ Example3(),
176
+ Example4(),
177
+ Example5(),
178
+ Example6(),
179
+ Example7(),
180
+ Example8(),
181
+ Example9(),
182
+ Example10(),
183
+ Example11(),
184
+ Example12(),
185
+ Example13(),
186
+ Example14(),
187
+ Example15(),
188
+ Example16(),
189
+ Example17(),
190
+ Example18(),
191
+ Example19(),
192
+ Example20(),
193
+ Example21(),
194
+ Example22(),
195
+ Example23(),
196
+ Example24(),
197
+ Example25(),
198
+ Example26(),
199
+ Example27(),
200
+ Example28(),
201
+ Example29(),
202
+ ]);
203
+ }
204
+ }
205
+
206
+
207
+ abstract class Example extends StatelessWidget {
208
+ const Example({super.key});
209
+
210
+ String get code;
211
+
212
+ String get explanation;
213
+ }
214
+
215
+
216
+ class FlutterLayoutArticle extends StatefulWidget {
217
+ const FlutterLayoutArticle(
218
+ this.examples, {
219
+ super.key,
220
+ });
221
+
222
+ final List<Example> examples;
223
+
224
+ @override
225
+ State<FlutterLayoutArticle> createState() => _FlutterLayoutArticleState();
226
+ }
227
+
228
+
229
+ class _FlutterLayoutArticleState extends State<FlutterLayoutArticle> {
230
+ late int count;
231
+ late Widget example;
232
+ late String code;
233
+ late String explanation;
234
+
235
+ @override
236
+ void initState() {
237
+ count = 1;
238
+ code = const Example1().code;
239
+ explanation = const Example1().explanation;
240
+
241
+ super.initState();
242
+ }
243
+
244
+ @override
245
+ void didUpdateWidget(FlutterLayoutArticle oldWidget) {
246
+ super.didUpdateWidget(oldWidget);
247
+ var example = widget.examples[count - 1];
248
+ code = example.code;
249
+ explanation = example.explanation;
250
+ }
251
+
252
+ @override
253
+ Widget build(BuildContext context) {
254
+ return MaterialApp(
255
+ debugShowCheckedModeBanner: false,
256
+ title: 'Flutter Layout Article',
257
+ home: SafeArea(
258
+ child: Material(
259
+ color: Colors.black,
260
+ child: FittedBox(
261
+ child: Container(
262
+ width: 400,
263
+ height: 670,
264
+ color: const Color(0xFFCCCCCC),
265
+ child: Column(
266
+ crossAxisAlignment: CrossAxisAlignment.center,
267
+ children: [
268
+ Expanded(
269
+ child: ConstrainedBox(
270
+ constraints: const BoxConstraints.tightFor(
271
+ width: double.infinity, height: double.infinity),
272
+ child: widget.examples[count - 1])),
273
+ Container(
274
+ height: 50,
275
+ width: double.infinity,
276
+ color: Colors.black,
277
+ child: SingleChildScrollView(
278
+ scrollDirection: Axis.horizontal,
279
+ child: Row(
280
+ mainAxisSize: MainAxisSize.min,
281
+ children: [
282
+ for (int i = 0; i < widget.examples.length; i++)
283
+ Container(
284
+ width: 58,
285
+ padding: const EdgeInsets.only(left: 4, right: 4),
286
+ child: button(i + 1),
287
+ ),
288
+ ],
289
+ ),
290
+ ),
291
+ ),
292
+ Container(
293
+ height: 273,
294
+ color: Colors.grey[50],
295
+ child: Scrollbar(
296
+ child: SingleChildScrollView(
297
+ key: ValueKey(count),
298
+ child: Padding(
299
+ padding: const EdgeInsets.all(10),
300
+ child: Column(
301
+ children: [
302
+ Center(child: Text(code)),
303
+ const SizedBox(height: 15),
304
+ Text(
305
+ explanation,
306
+ style: TextStyle(
307
+ color: Colors.blue[900],
308
+ fontStyle: FontStyle.italic),
309
+ ),
310
+ ],
311
+ ),
312
+ ),
313
+ ),
314
+ ),
315
+ ),
316
+ ],
317
+ ),
318
+ ),
319
+ ),
320
+ ),
321
+ ),
322
+ );
323
+ }
324
+
325
+ Widget button(int exampleNumber) {
326
+ return Button(
327
+ key: ValueKey('button$exampleNumber'),
328
+ isSelected: count == exampleNumber,
329
+ exampleNumber: exampleNumber,
330
+ onPressed: () {
331
+ showExample(
332
+ exampleNumber,
333
+ widget.examples[exampleNumber - 1].code,
334
+ widget.examples[exampleNumber - 1].explanation,
335
+ );
336
+ },
337
+ );
338
+ }
339
+
340
+ void showExample(int exampleNumber, String code, String explanation) {
341
+ setState(() {
342
+ count = exampleNumber;
343
+ this.code = code;
344
+ this.explanation = explanation;
345
+ });
346
+ }
347
+ }
348
+
349
+
350
+ class Button extends StatelessWidget {
351
+ final bool isSelected;
352
+ final int exampleNumber;
353
+ final VoidCallback onPressed;
354
+
355
+ const Button({
356
+ super.key,
357
+ required this.isSelected,
358
+ required this.exampleNumber,
359
+ required this.onPressed,
360
+ });
361
+
362
+ @override
363
+ Widget build(BuildContext context) {
364
+ return TextButton(
365
+ style: TextButton.styleFrom(
366
+ foregroundColor: Colors.white,
367
+ backgroundColor: isSelected ? Colors.grey : Colors.grey[800],
368
+ ),
369
+ child: Text(exampleNumber.toString()),
370
+ onPressed: () {
371
+ Scrollable.ensureVisible(
372
+ context,
373
+ duration: const Duration(milliseconds: 350),
374
+ curve: Curves.easeOut,
375
+ alignment: 0.5,
376
+ );
377
+ onPressed();
378
+ },
379
+ );
380
+ }
381
+ }
382
+
383
+ class Example1 extends Example {
384
+ const Example1({super.key});
385
+
386
+ @override
387
+ final code = 'Container(color: red)';
388
+
389
+ @override
390
+ final explanation = 'The screen is the parent of the Container, '
391
+ 'and it forces the Container to be exactly the same size as the screen.'
392
+ '\n\n'
393
+ 'So the Container fills the screen and paints it red.';
394
+
395
+ @override
396
+ Widget build(BuildContext context) {
397
+ return Container(color: red);
398
+ }
399
+ }
400
+
401
+
402
+ class Example2 extends Example {
403
+ const Example2({super.key});
404
+
405
+ @override
406
+ final code = 'Container(width: 100, height: 100, color: red)';
407
+ @override
408
+ final String explanation =
409
+ 'The red Container wants to be 100x100, but it can\'t, '
410
+ 'because the screen forces it to be exactly the same size as the screen.'
411
+ '\n\n'
412
+ 'So the Container fills the screen.';
413
+
414
+ @override
415
+ Widget build(BuildContext context) {
416
+ return Container(width: 100, height: 100, color: red);
417
+ }
418
+ }
419
+
420
+
421
+ class Example3 extends Example {
422
+ const Example3({super.key});
423
+
424
+ @override
425
+ final code = 'Center(\n'
426
+ ' child: Container(width: 100, height: 100, color: red))';
427
+ @override
428
+ final String explanation =
429
+ 'The screen forces the Center to be exactly the same size as the screen, '
430
+ 'so the Center fills the screen.'
431
+ '\n\n'
432
+ 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.'
433
+ 'Now the Container can indeed be 100x100.';
434
+
435
+ @override
436
+ Widget build(BuildContext context) {
437
+ return Center(
438
+ child: Container(width: 100, height: 100, color: red),
439
+ );
440
+ }
441
+ }
442
+
443
+
444
+ class Example4 extends Example {
445
+ const Example4({super.key});
446
+
447
+ @override
448
+ final code = 'Align(\n'
449
+ ' alignment: Alignment.bottomRight,\n'
450
+ ' child: Container(width: 100, height: 100, color: red))';
451
+ @override
452
+ final String explanation =
453
+ 'This is different from the previous example in that it uses Align instead of Center.'
454
+ '\n\n'
455
+ '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. '
456
+ 'Instead, it aligns the Container to the bottom-right of the available space.';
457
+
458
+ @override
459
+ Widget build(BuildContext context) {
460
+ return Align(
461
+ alignment: Alignment.bottomRight,
462
+ child: Container(width: 100, height: 100, color: red),
463
+ );
464
+ }
465
+ }
466
+
467
+
468
+ class Example5 extends Example {
469
+ const Example5({super.key});
470
+
471
+ @override
472
+ final code = 'Center(\n'
473
+ ' child: Container(\n'
474
+ ' color: red,\n'
475
+ ' width: double.infinity,\n'
476
+ ' height: double.infinity))';
477
+ @override
478
+ final String explanation =
479
+ 'The screen forces the Center to be exactly the same size as the screen, '
480
+ 'so the Center fills the screen.'
481
+ '\n\n'
482
+ 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.'
483
+ 'The Container wants to be of infinite size, but since it can\'t be bigger than the screen, it just fills the screen.';
484
+
485
+ @override
486
+ Widget build(BuildContext context) {
487
+ return Center(
488
+ child: Container(
489
+ width: double.infinity, height: double.infinity, color: red),
490
+ );
491
+ }
492
+ }
493
+
494
+
495
+ class Example6 extends Example {
496
+ const Example6({super.key});
497
+
498
+ @override
499
+ final code = 'Center(child: Container(color: red))';
500
+ @override
501
+ final String explanation =
502
+ 'The screen forces the Center to be exactly the same size as the screen, '
503
+ 'so the Center fills the screen.'
504
+ '\n\n'
505
+ 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.'
506
+ '\n\n'
507
+ '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.'
508
+ '\n\n'
509
+ 'But why does the Container decide that? '
510
+ 'Simply because that\'s a design decision by those who created the Container widget. '
511
+ 'It could have been created differently, and you have to read the Container documentation to understand how it behaves, depending on the circumstances. ';
512
+
513
+ @override
514
+ Widget build(BuildContext context) {
515
+ return Center(
516
+ child: Container(color: red),
517
+ );
518
+ }
519
+ }
520
+
521
+
522
+ class Example7 extends Example {
523
+ const Example7({super.key});
524
+
525
+ @override
526
+ final code = 'Center(\n'
527
+ ' child: Container(color: red\n'
528
+ ' child: Container(color: green, width: 30, height: 30)))';
529
+ @override
530
+ final String explanation =
531
+ 'The screen forces the Center to be exactly the same size as the screen, '
532
+ 'so the Center fills the screen.'
533
+ '\n\n'
534
+ 'The Center tells the red Container that it can be any size it wants, but not bigger than the screen.'
535
+ 'Since the red Container has no size but has a child, it decides it wants to be the same size as its child.'
536
+ '\n\n'
537
+ 'The red Container tells its child that it can be any size it wants, but not bigger than the screen.'
538
+ '\n\n'
539
+ 'The child is a green Container that wants to be 30x30.'
540
+ '\n\n'
541
+ 'Since the red `Container` has no size but has a child, it decides it wants to be the same size as its child. '
542
+ 'The red color isn\'t visible, since the green Container entirely covers all of the red Container.';
543
+
544
+ @override
545
+ Widget build(BuildContext context) {
546
+ return Center(
547
+ child: Container(
548
+ color: red,
549
+ child: Container(color: green, width: 30, height: 30),
550
+ ),
551
+ );
552
+ }
553
+ }
554
+
555
+
556
+ class Example8 extends Example {
557
+ const Example8({super.key});
558
+
559
+ @override
560
+ final code = 'Center(\n'
561
+ ' child: Container(color: red\n'
562
+ ' padding: const EdgeInsets.all(20),\n'
563
+ ' child: Container(color: green, width: 30, height: 30)))';
564
+ @override
565
+ final String explanation =
566
+ 'The red Container sizes itself to its children size, but it takes its own padding into consideration. '
567
+ 'So it is also 30x30 plus padding. '
568
+ 'The red color is visible because of the padding, and the green Container has the same size as in the previous example.';
569
+
570
+ @override
571
+ Widget build(BuildContext context) {
572
+ return Center(
573
+ child: Container(
574
+ padding: const EdgeInsets.all(20),
575
+ color: red,
576
+ child: Container(color: green, width: 30, height: 30),
577
+ ),
578
+ );
579
+ }
580
+ }
581
+
582
+
583
+ class Example9 extends Example {
584
+ const Example9({super.key});
585
+
586
+ @override
587
+ final code = 'ConstrainedBox(\n'
588
+ ' constraints: BoxConstraints(\n'
589
+ ' minWidth: 70, minHeight: 70,\n'
590
+ ' maxWidth: 150, maxHeight: 150),\n'
591
+ ' child: Container(color: red, width: 10, height: 10)))';
592
+ @override
593
+ final String explanation =
594
+ 'You might guess that the Container has to be between 70 and 150 pixels, but you would be wrong. '
595
+ 'The ConstrainedBox only imposes ADDITIONAL constraints from those it receives from its parent.'
596
+ '\n\n'
597
+ 'Here, the screen forces the ConstrainedBox to be exactly the same size as the screen, '
598
+ 'so it tells its child Container to also assume the size of the screen, '
599
+ 'thus ignoring its \'constraints\' parameter.';
600
+
601
+ @override
602
+ Widget build(BuildContext context) {
603
+ return ConstrainedBox(
604
+ constraints: const BoxConstraints(
605
+ minWidth: 70,
606
+ minHeight: 70,
607
+ maxWidth: 150,
608
+ maxHeight: 150,
609
+ ),
610
+ child: Container(color: red, width: 10, height: 10),
611
+ );
612
+ }
613
+ }
614
+
615
+
616
+ class Example10 extends Example {
617
+ const Example10({super.key});
618
+
619
+ @override
620
+ final code = 'Center(\n'
621
+ ' child: ConstrainedBox(\n'
622
+ ' constraints: BoxConstraints(\n'
623
+ ' minWidth: 70, minHeight: 70,\n'
624
+ ' maxWidth: 150, maxHeight: 150),\n'
625
+ ' child: Container(color: red, width: 10, height: 10))))';
626
+ @override
627
+ final String explanation =
628
+ 'Now, Center allows ConstrainedBox to be any size up to the screen size.'
629
+ '\n\n'
630
+ 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.'
631
+ '\n\n'
632
+ 'The Container must be between 70 and 150 pixels. It wants to have 10 pixels, so it will end up having 70 (the MINIMUM).';
633
+
634
+ @override
635
+ Widget build(BuildContext context) {
636
+ return Center(
637
+ child: ConstrainedBox(
638
+ constraints: const BoxConstraints(
639
+ minWidth: 70,
640
+ minHeight: 70,
641
+ maxWidth: 150,
642
+ maxHeight: 150,
643
+ ),
644
+ child: Container(color: red, width: 10, height: 10),
645
+ ),
646
+ );
647
+ }
648
+ }
649
+
650
+
651
+ class Example11 extends Example {
652
+ const Example11({super.key});
653
+
654
+ @override
655
+ final code = 'Center(\n'
656
+ ' child: ConstrainedBox(\n'
657
+ ' constraints: BoxConstraints(\n'
658
+ ' minWidth: 70, minHeight: 70,\n'
659
+ ' maxWidth: 150, maxHeight: 150),\n'
660
+ ' child: Container(color: red, width: 1000, height: 1000))))';
661
+ @override
662
+ final String explanation =
663
+ 'Center allows ConstrainedBox to be any size up to the screen size.'
664
+ 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child'
665
+ '\n\n'
666
+ 'The Container must be between 70 and 150 pixels. It wants to have 1000 pixels, so it ends up having 150 (the MAXIMUM).';
667
+
668
+ @override
669
+ Widget build(BuildContext context) {
670
+ return Center(
671
+ child: ConstrainedBox(
672
+ constraints: const BoxConstraints(
673
+ minWidth: 70,
674
+ minHeight: 70,
675
+ maxWidth: 150,
676
+ maxHeight: 150,
677
+ ),
678
+ child: Container(color: red, width: 1000, height: 1000),
679
+ ),
680
+ );
681
+ }
682
+ }
683
+
684
+
685
+ class Example12 extends Example {
686
+ const Example12({super.key});
687
+
688
+ @override
689
+ final code = 'Center(\n'
690
+ ' child: ConstrainedBox(\n'
691
+ ' constraints: BoxConstraints(\n'
692
+ ' minWidth: 70, minHeight: 70,\n'
693
+ ' maxWidth: 150, maxHeight: 150),\n'
694
+ ' child: Container(color: red, width: 100, height: 100))))';
695
+ @override
696
+ final String explanation =
697
+ 'Center allows ConstrainedBox to be any size up to the screen size.'
698
+ 'ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.'
699
+ '\n\n'
700
+ '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.';
701
+
702
+ @override
703
+ Widget build(BuildContext context) {
704
+ return Center(
705
+ child: ConstrainedBox(
706
+ constraints: const BoxConstraints(
707
+ minWidth: 70,
708
+ minHeight: 70,
709
+ maxWidth: 150,
710
+ maxHeight: 150,
711
+ ),
712
+ child: Container(color: red, width: 100, height: 100),
713
+ ),
714
+ );
715
+ }
716
+ }
717
+
718
+
719
+ class Example13 extends Example {
720
+ const Example13({super.key});
721
+
722
+ @override
723
+ final code = 'UnconstrainedBox(\n'
724
+ ' child: Container(color: red, width: 20, height: 50));';
725
+ @override
726
+ final String explanation =
727
+ 'The screen forces the UnconstrainedBox to be exactly the same size as the screen.'
728
+ 'However, the UnconstrainedBox lets its child Container be any size it wants.';
729
+
730
+ @override
731
+ Widget build(BuildContext context) {
732
+ return UnconstrainedBox(
733
+ child: Container(color: red, width: 20, height: 50),
734
+ );
735
+ }
736
+ }
737
+
738
+
739
+ class Example14 extends Example {
740
+ const Example14({super.key});
741
+
742
+ @override
743
+ final code = 'UnconstrainedBox(\n'
744
+ ' child: Container(color: red, width: 4000, height: 50));';
745
+ @override
746
+ final String explanation =
747
+ 'The screen forces the UnconstrainedBox to be exactly the same size as the screen, '
748
+ 'and UnconstrainedBox lets its child Container be any size it wants.'
749
+ '\n\n'
750
+ 'Unfortunately, in this case the Container has 4000 pixels of width and is too big to fit in the UnconstrainedBox, '
751
+ 'so the UnconstrainedBox displays the much dreaded "overflow warning".';
752
+
753
+ @override
754
+ Widget build(BuildContext context) {
755
+ return UnconstrainedBox(
756
+ child: Container(color: red, width: 4000, height: 50),
757
+ );
758
+ }
759
+ }
760
+
761
+
762
+ class Example15 extends Example {
763
+ const Example15({super.key});
764
+
765
+ @override
766
+ final code = 'OverflowBox(\n'
767
+ ' minWidth: 0,'
768
+ ' minHeight: 0,'
769
+ ' maxWidth: double.infinity,'
770
+ ' maxHeight: double.infinity,'
771
+ ' child: Container(color: red, width: 4000, height: 50));';
772
+ @override
773
+ final String explanation =
774
+ 'The screen forces the OverflowBox to be exactly the same size as the screen, '
775
+ 'and OverflowBox lets its child Container be any size it wants.'
776
+ '\n\n'
777
+ 'OverflowBox is similar to UnconstrainedBox, and the difference is that it won\'t display any warnings if the child doesn\'t fit the space.'
778
+ '\n\n'
779
+ 'In this case the Container is 4000 pixels wide, and is too big to fit in the OverflowBox, '
780
+ 'but the OverflowBox simply shows as much as it can, with no warnings given.';
781
+
782
+ @override
783
+ Widget build(BuildContext context) {
784
+ return OverflowBox(
785
+ minWidth: 0,
786
+ minHeight: 0,
787
+ maxWidth: double.infinity,
788
+ maxHeight: double.infinity,
789
+ child: Container(color: red, width: 4000, height: 50),
790
+ );
791
+ }
792
+ }
793
+
794
+
795
+ class Example16 extends Example {
796
+ const Example16({super.key});
797
+
798
+ @override
799
+ final code = 'UnconstrainedBox(\n'
800
+ ' child: Container(color: Colors.red, width: double.infinity, height: 100));';
801
+ @override
802
+ final String explanation =
803
+ 'This won\'t render anything, and you\'ll see an error in the console.'
804
+ '\n\n'
805
+ 'The UnconstrainedBox lets its child be any size it wants, '
806
+ 'however its child is a Container with infinite size.'
807
+ '\n\n'
808
+ 'Flutter can\'t render infinite sizes, so it throws an error with the following message: '
809
+ '"BoxConstraints forces an infinite width."';
810
+
811
+ @override
812
+ Widget build(BuildContext context) {
813
+ return UnconstrainedBox(
814
+ child: Container(color: Colors.red, width: double.infinity, height: 100),
815
+ );
816
+ }
817
+ }
818
+
819
+
820
+ class Example17 extends Example {
821
+ const Example17({super.key});
822
+
823
+ @override
824
+ final code = 'UnconstrainedBox(\n'
825
+ ' child: LimitedBox(maxWidth: 100,\n'
826
+ ' child: Container(color: Colors.red,\n'
827
+ ' width: double.infinity, height: 100));';
828
+ @override
829
+ final String explanation = 'Here you won\'t get an error anymore, '
830
+ 'because when the LimitedBox is given an infinite size by the UnconstrainedBox, '
831
+ 'it passes a maximum width of 100 down to its child.'
832
+ '\n\n'
833
+ 'If you swap the UnconstrainedBox for a Center widget, '
834
+ 'the LimitedBox won\'t apply its limit anymore (since its limit is only applied when it gets infinite constraints), '
835
+ 'and the width of the Container is allowed to grow past 100.'
836
+ '\n\n'
837
+ 'This explains the difference between a LimitedBox and a ConstrainedBox.';
838
+
839
+ @override
840
+ Widget build(BuildContext context) {
841
+ return UnconstrainedBox(
842
+ child: LimitedBox(
843
+ maxWidth: 100,
844
+ child: Container(
845
+ color: Colors.red,
846
+ width: double.infinity,
847
+ height: 100,
848
+ ),
849
+ ),
850
+ );
851
+ }
852
+ }
853
+
854
+
855
+ class Example18 extends Example {
856
+ const Example18({super.key});
857
+
858
+ @override
859
+ final code = 'FittedBox(\n'
860
+ ' child: Text(\'Some Example Text.\'));';
861
+ @override
862
+ final String explanation =
863
+ 'The screen forces the FittedBox to be exactly the same size as the screen.'
864
+ 'The Text has some natural width (also called its intrinsic width) that depends on the amount of text, its font size, and so on.'
865
+ '\n\n'
866
+ 'The FittedBox lets the Text be any size it wants, '
867
+ 'but after the Text tells its size to the FittedBox, '
868
+ 'the FittedBox scales the Text until it fills all of the available width.';
869
+
870
+ @override
871
+ Widget build(BuildContext context) {
872
+ return const FittedBox(
873
+ child: Text('Some Example Text.'),
874
+ );
875
+ }
876
+ }
877
+
878
+
879
+ class Example19 extends Example {
880
+ const Example19({super.key});
881
+
882
+ @override
883
+ final code = 'Center(\n'
884
+ ' child: FittedBox(\n'
885
+ ' child: Text(\'Some Example Text.\')));';
886
+ @override
887
+ final String explanation =
888
+ 'But what happens if you put the FittedBox inside of a Center widget? '
889
+ 'The Center lets the FittedBox be any size it wants, up to the screen size.'
890
+ '\n\n'
891
+ 'The FittedBox then sizes itself to the Text, and lets the Text be any size it wants.'
892
+ '\n\n'
893
+ 'Since both FittedBox and the Text have the same size, no scaling happens.';
894
+
895
+ @override
896
+ Widget build(BuildContext context) {
897
+ return const Center(
898
+ child: FittedBox(
899
+ child: Text('Some Example Text.'),
900
+ ),
901
+ );
902
+ }
903
+ }
904
+
905
+ class Example20 extends Example {
906
+ const Example20({super.key});
907
+
908
+ @override
909
+ final code = 'Center(\n'
910
+ ' child: FittedBox(\n'
911
+ ' child: Text(\'…\')));';
912
+ @override
913
+ final String explanation =
914
+ 'However, what happens if FittedBox is inside of a Center widget, but the Text is too large to fit the screen?'
915
+ '\n\n'
916
+ 'FittedBox tries to size itself to the Text, but it can\'t be bigger than the screen. '
917
+ 'It then assumes the screen size, and resizes Text so that it fits the screen, too.';
918
+
919
+ @override
920
+ Widget build(BuildContext context) {
921
+ return const Center(
922
+ child: FittedBox(
923
+ child: Text(
924
+ 'This is some very very very large text that is too big to fit a regular screen in a single line.'),
925
+ ),
926
+ );
927
+ }
928
+ }
929
+
930
+
931
+ class Example21 extends Example {
932
+ const Example21({super.key});
933
+
934
+ @override
935
+ final code = 'Center(\n'
936
+ ' child: Text(\'…\'));';
937
+ @override
938
+ final String explanation = 'If, however, you remove the FittedBox, '
939
+ 'the Text gets its maximum width from the screen, '
940
+ 'and breaks the line so that it fits the screen.';
941
+
942
+ @override
943
+ Widget build(BuildContext context) {
944
+ return const Center(
945
+ child: Text(
946
+ 'This is some very very very large text that is too big to fit a regular screen in a single line.'),
947
+ );
948
+ }
949
+ }
950
+
951
+
952
+ class Example22 extends Example {
953
+ const Example22({super.key});
954
+
955
+ @override
956
+ final code = 'FittedBox(\n'
957
+ ' child: Container(\n'
958
+ ' height: 20, width: double.infinity));';
959
+ @override
960
+ final String explanation =
961
+ 'FittedBox can only scale a widget that is BOUNDED (has non-infinite width and height).'
962
+ 'Otherwise, it won\'t render anything, and you\'ll see an error in the console.';
963
+
964
+ @override
965
+ Widget build(BuildContext context) {
966
+ return FittedBox(
967
+ child: Container(
968
+ height: 20,
969
+ width: double.infinity,
970
+ color: Colors.red,
971
+ ),
972
+ );
973
+ }
974
+ }
975
+
976
+
977
+ class Example23 extends Example {
978
+ const Example23({super.key});
979
+
980
+ @override
981
+ final code = 'Row(children:[\n'
982
+ ' Container(color: red, child: Text(\'Hello!\'))\n'
983
+ ' Container(color: green, child: Text(\'Goodbye!\'))]';
984
+ @override
985
+ final String explanation =
986
+ 'The screen forces the Row to be exactly the same size as the screen.'
987
+ '\n\n'
988
+ 'Just like an UnconstrainedBox, the Row won\'t impose any constraints onto its children, '
989
+ 'and instead lets them be any size they want.'
990
+ '\n\n'
991
+ 'The Row then puts them side-by-side, and any extra space remains empty.';
992
+
993
+ @override
994
+ Widget build(BuildContext context) {
995
+ return Row(
996
+ children: [
997
+ Container(color: red, child: const Text('Hello!', style: big)),
998
+ Container(color: green, child: const Text('Goodbye!', style: big)),
999
+ ],
1000
+ );
1001
+ }
1002
+ }
1003
+
1004
+
1005
+ class Example24 extends Example {
1006
+ const Example24({super.key});
1007
+
1008
+ @override
1009
+ final code = 'Row(children:[\n'
1010
+ ' Container(color: red, child: Text(\'…\'))\n'
1011
+ ' Container(color: green, child: Text(\'Goodbye!\'))]';
1012
+ @override
1013
+ final String explanation =
1014
+ 'Since the Row won\'t impose any constraints onto its children, '
1015
+ 'it\'s quite possible that the children might be too big to fit the available width of the Row.'
1016
+ 'In this case, just like an UnconstrainedBox, the Row displays the "overflow warning".';
1017
+
1018
+ @override
1019
+ Widget build(BuildContext context) {
1020
+ return Row(
1021
+ children: [
1022
+ Container(
1023
+ color: red,
1024
+ child: const Text(
1025
+ 'This is a very long text that '
1026
+ 'won\'t fit the line.',
1027
+ style: big,
1028
+ ),
1029
+ ),
1030
+ Container(color: green, child: const Text('Goodbye!', style: big)),
1031
+ ],
1032
+ );
1033
+ }
1034
+ }
1035
+
1036
+
1037
+ class Example25 extends Example {
1038
+ const Example25({super.key});
1039
+
1040
+ @override
1041
+ final code = 'Row(children:[\n'
1042
+ ' Expanded(\n'
1043
+ ' child: Container(color: red, child: Text(\'…\')))\n'
1044
+ ' Container(color: green, child: Text(\'Goodbye!\'))]';
1045
+ @override
1046
+ final String explanation =
1047
+ 'When a Row\'s child is wrapped in an Expanded widget, the Row won\'t let this child define its own width anymore.'
1048
+ '\n\n'
1049
+ '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.'
1050
+ '\n\n'
1051
+ 'In other words, once you use Expanded, the original child\'s width becomes irrelevant, and is ignored.';
1052
+
1053
+ @override
1054
+ Widget build(BuildContext context) {
1055
+ return Row(
1056
+ children: [
1057
+ Expanded(
1058
+ child: Center(
1059
+ child: Container(
1060
+ color: red,
1061
+ child: const Text(
1062
+ 'This is a very long text that won\'t fit the line.',
1063
+ style: big,
1064
+ ),
1065
+ ),
1066
+ ),
1067
+ ),
1068
+ Container(color: green, child: const Text('Goodbye!', style: big)),
1069
+ ],
1070
+ );
1071
+ }
1072
+ }
1073
+
1074
+
1075
+ class Example26 extends Example {
1076
+ const Example26({super.key});
1077
+
1078
+ @override
1079
+ final code = 'Row(children:[\n'
1080
+ ' Expanded(\n'
1081
+ ' child: Container(color: red, child: Text(\'…\')))\n'
1082
+ ' Expanded(\n'
1083
+ ' child: Container(color: green, child: Text(\'Goodbye!\'))]';
1084
+ @override
1085
+ final String explanation =
1086
+ 'If all of Row\'s children are wrapped in Expanded widgets, each Expanded has a size proportional to its flex parameter, '
1087
+ 'and only then each Expanded widget forces its child to have the Expanded\'s width.'
1088
+ '\n\n'
1089
+ 'In other words, Expanded ignores the preferred width of its children.';
1090
+
1091
+ @override
1092
+ Widget build(BuildContext context) {
1093
+ return Row(
1094
+ children: [
1095
+ Expanded(
1096
+ child: Container(
1097
+ color: red,
1098
+ child: const Text(
1099
+ 'This is a very long text that won\'t fit the line.',
1100
+ style: big,
1101
+ ),
1102
+ ),
1103
+ ),
1104
+ Expanded(
1105
+ child: Container(
1106
+ color: green,
1107
+ child: const Text(
1108
+ 'Goodbye!',
1109
+ style: big,
1110
+ ),
1111
+ ),
1112
+ ),
1113
+ ],
1114
+ );
1115
+ }
1116
+ }
1117
+
1118
+
1119
+ class Example27 extends Example {
1120
+ const Example27({super.key});
1121
+
1122
+ @override
1123
+ final code = 'Row(children:[\n'
1124
+ ' Flexible(\n'
1125
+ ' child: Container(color: red, child: Text(\'…\')))\n'
1126
+ ' Flexible(\n'
1127
+ ' child: Container(color: green, child: Text(\'Goodbye!\'))]';
1128
+ @override
1129
+ final String explanation =
1130
+ 'The only difference if you use Flexible instead of Expanded, '
1131
+ 'is that Flexible lets its child be SMALLER than the Flexible width, '
1132
+ 'while Expanded forces its child to have the same width of the Expanded.'
1133
+ '\n\n'
1134
+ 'But both Expanded and Flexible ignore their children\'s width when sizing themselves.'
1135
+ '\n\n'
1136
+ 'This means that it\'s IMPOSSIBLE to expand Row children proportionally to their sizes. '
1137
+ 'The Row either uses the exact child\'s width, or ignores it completely when you use Expanded or Flexible.';
1138
+
1139
+ @override
1140
+ Widget build(BuildContext context) {
1141
+ return Row(
1142
+ children: [
1143
+ Flexible(
1144
+ child: Container(
1145
+ color: red,
1146
+ child: const Text(
1147
+ 'This is a very long text that won\'t fit the line.',
1148
+ style: big,
1149
+ ),
1150
+ ),
1151
+ ),
1152
+ Flexible(
1153
+ child: Container(
1154
+ color: green,
1155
+ child: const Text(
1156
+ 'Goodbye!',
1157
+ style: big,
1158
+ ),
1159
+ ),
1160
+ ),
1161
+ ],
1162
+ );
1163
+ }
1164
+ }
1165
+
1166
+
1167
+ class Example28 extends Example {
1168
+ const Example28({super.key});
1169
+
1170
+ @override
1171
+ final code = 'Scaffold(\n'
1172
+ ' body: Container(color: blue,\n'
1173
+ ' child: Column(\n'
1174
+ ' children: [\n'
1175
+ ' Text(\'Hello!\'),\n'
1176
+ ' Text(\'Goodbye!\')])))';
1177
+
1178
+ @override
1179
+ final String explanation =
1180
+ 'The screen forces the Scaffold to be exactly the same size as the screen, '
1181
+ 'so the Scaffold fills the screen.'
1182
+ '\n\n'
1183
+ 'The Scaffold tells the Container that it can be any size it wants, but not bigger than the screen.'
1184
+ '\n\n'
1185
+ 'When a widget tells its child that it can be smaller than a certain size, '
1186
+ 'we say the widget supplies "loose" constraints to its child. More on that later.';
1187
+
1188
+ @override
1189
+ Widget build(BuildContext context) {
1190
+ return Scaffold(
1191
+ body: Container(
1192
+ color: blue,
1193
+ child: const Column(
1194
+ children: [
1195
+ Text('Hello!'),
1196
+ Text('Goodbye!'),
1197
+ ],
1198
+ ),
1199
+ ),
1200
+ );
1201
+ }
1202
+ }
1203
+
1204
+
1205
+ class Example29 extends Example {
1206
+ const Example29({super.key});
1207
+
1208
+ @override
1209
+ final code = 'Scaffold(\n'
1210
+ ' body: Container(color: blue,\n'
1211
+ ' child: SizedBox.expand(\n'
1212
+ ' child: Column(\n'
1213
+ ' children: [\n'
1214
+ ' Text(\'Hello!\'),\n'
1215
+ ' Text(\'Goodbye!\')]))))';
1216
+
1217
+ @override
1218
+ final String explanation =
1219
+ 'If you want the Scaffold\'s child to be exactly the same size as the Scaffold itself, '
1220
+ 'you can wrap its child with SizedBox.expand.'
1221
+ '\n\n'
1222
+ 'When a widget tells its child that it must be of a certain size, '
1223
+ 'we say the widget supplies "tight" constraints to its child. More on that later.';
1224
+
1225
+ @override
1226
+ Widget build(BuildContext context) {
1227
+ return Scaffold(
1228
+ body: SizedBox.expand(
1229
+ child: Container(
1230
+ color: blue,
1231
+ child: const Column(
1232
+ children: [
1233
+ Text('Hello!'),
1234
+ Text('Goodbye!'),
1235
+ ],
1236
+ ),
1237
+ ),
1238
+ ),
1239
+ );
1240
+ }
1241
+ }
1242
+
1243
+ <code_end>
1244
+
1245
+ If you prefer, you can grab the code from
1246
+ this GitHub repo.
1247
+ The examples are explained in the following sections.
1248
+ <topic_end>
1249
+ <topic_start>
1250
+ Example 1
1251
+
1252
+ <code_start>
1253
+ Container(color: red)
1254
+ <code_end>
1255
+
1256
+ The screen is the parent of the Container, and it
1257
+ forces the Container to be exactly the same size as the screen.
1258
+ So the Container fills the screen and paints it red.
1259
+ <topic_end>
1260
+ <topic_start>
1261
+ Example 2
1262
+
1263
+ <code_start>
1264
+ Container(width: 100, height: 100, color: red)
1265
+ <code_end>
1266
+
1267
+ The red Container wants to be 100 × 100,
1268
+ but it can’t, because the screen forces it to be
1269
+ exactly the same size as the screen.
1270
+ So the Container fills the screen.
1271
+ <topic_end>
1272
+ <topic_start>
1273
+ Example 3
1274
+
1275
+ <code_start>
1276
+ Center(
1277
+ child: Container(width: 100, height: 100, color: red),
1278
+ )
1279
+ <code_end>
1280
+
1281
+ The screen forces the Center to be exactly the same size
1282
+ as the screen, so the Center fills the screen.
1283
+ The Center tells the Container that it can be any size it
1284
+ wants, but not bigger than the screen. Now the Container
1285
+ can indeed be 100 × 100.
1286
+ <topic_end>
1287
+ <topic_start>
1288
+ Example 4
1289
+
1290
+ <code_start>
1291
+ Align(
1292
+ alignment: Alignment.bottomRight,
1293
+ child: Container(width: 100, height: 100, color: red),
1294
+ )
1295
+ <code_end>
1296
+
1297
+ This is different from the previous example in that it uses
1298
+ Align instead of Center.
1299
+ Align also tells the Container that it can be any size it
1300
+ wants, but if there is empty space it won’t center the Container.
1301
+ Instead, it aligns the container to the bottom-right of the
1302
+ available space.
1303
+ <topic_end>
1304
+ <topic_start>
1305
+ Example 5
1306
+
1307
+ <code_start>
1308
+ Center(
1309
+ child: Container(
1310
+ width: double.infinity, height: double.infinity, color: red),
1311
+ )
1312
+ <code_end>
1313
+
1314
+ The screen forces the Center to be exactly the
1315
+ same size as the screen, so the Center fills the screen.
1316
+ The Center tells the Container that it can be any size it wants,
1317
+ but not bigger than the screen. The Container wants to be
1318
+ of infinite size, but since it can’t be bigger than the screen,
1319
+ it just fills the screen.
1320
+ <topic_end>
1321
+ <topic_start>
1322
+ Example 6
1323
+
1324
+ <code_start>
1325
+ Center(
1326
+ child: Container(color: red),
1327
+ )
1328
+ <code_end>
1329
+
1330
+ The screen forces the Center to be exactly the
1331
+ same size as the screen, so the Center fills the screen.
1332
+ The Center tells the Container that it can be any
1333
+ size it wants, but not bigger than the screen.
1334
+ Since the Container has no child and no fixed size,
1335
+ it decides it wants to be as big as possible,
1336
+ so it fills the whole screen.
1337
+ But why does the Container decide that?
1338
+ Simply because that’s a design decision by those who
1339
+ created the Container widget. It could have been
1340
+ created differently, and you have to read the
1341
+ Container API documentation to understand
1342
+ how it behaves, depending on the circumstances.
1343
+ <topic_end>
1344
+ <topic_start>
1345
+ Example 7
1346
+
1347
+ <code_start>
1348
+ Center(
1349
+ child: Container(
1350
+ color: red,
1351
+ child: Container(color: green, width: 30, height: 30),
1352
+ ),
1353
+ )
1354
+ <code_end>
1355
+
1356
+ The screen forces the Center to be exactly the same
1357
+ size as the screen, so the Center fills the screen.
1358
+ The Center tells the red Container that it can be any size
1359
+ it wants, but not bigger than the screen. Since the red
1360
+ Container has no size but has a child,
1361
+ it decides it wants to be the same size as its child.
1362
+ The red Container tells its child that it can be any size
1363
+ it wants, but not bigger than the screen.
1364
+ The child is a green Container that wants to
1365
+ be 30 × 30. Given that the red Container sizes itself to
1366
+ the size of its child, it is also 30 × 30.
1367
+ The red color isn’t visible because the green Container
1368
+ entirely covers the red Container.
1369
+ <topic_end>
1370
+ <topic_start>
1371
+ Example 8
1372
+
1373
+ <code_start>
1374
+ Center(
1375
+ child: Container(
1376
+ padding: const EdgeInsets.all(20),
1377
+ color: red,
1378
+ child: Container(color: green, width: 30, height: 30),
1379
+ ),
1380
+ )
1381
+ <code_end>
1382
+
1383
+ The red Container sizes itself to its children’s size,
1384
+ but it takes its own padding into consideration.
1385
+ So it is also 30 × 30 plus padding.
1386
+ The red color is visible because of the padding,
1387
+ and the green Container has the same size as
1388
+ in the previous example.
1389
+ <topic_end>
1390
+ <topic_start>
1391
+ Example 9
1392
+
1393
+ <code_start>
1394
+ ConstrainedBox(
1395
+ constraints: const BoxConstraints(
1396
+ minWidth: 70,
1397
+ minHeight: 70,
1398
+ maxWidth: 150,
1399
+ maxHeight: 150,
1400
+ ),
1401
+ child: Container(color: red, width: 10, height: 10),
1402
+ )
1403
+ <code_end>
1404
+
1405
+ You might guess that the Container has to be
1406
+ between 70 and 150 pixels, but you would be wrong.
1407
+ The ConstrainedBox only imposes additional constraints
1408
+ from those it receives from its parent.
1409
+ Here, the screen forces the ConstrainedBox to be exactly
1410
+ the same size as the screen, so it tells its child Container
1411
+ to also assume the size of the screen, thus ignoring its
1412
+ constraints parameter.
1413
+ <topic_end>
1414
+ <topic_start>
1415
+ Example 10
1416
+
1417
+ <code_start>
1418
+ Center(
1419
+ child: ConstrainedBox(
1420
+ constraints: const BoxConstraints(
1421
+ minWidth: 70,
1422
+ minHeight: 70,
1423
+ maxWidth: 150,
1424
+ maxHeight: 150,
1425
+ ),
1426
+ child: Container(color: red, width: 10, height: 10),
1427
+ ),
1428
+ )
1429
+ <code_end>
1430
+
1431
+ Now, Center allows ConstrainedBox to be any size up to
1432
+ the screen size. The ConstrainedBox imposes additional
1433
+ constraints from its constraints parameter onto its child.
1434
+ The Container must be between 70 and 150 pixels.
1435
+ It wants to have 10 pixels,
1436
+ so it ends up having 70 (the minimum).
1437
+ <topic_end>
1438
+ <topic_start>
1439
+ Example 11
1440
+
1441
+ <code_start>
1442
+ Center(
1443
+ child: ConstrainedBox(
1444
+ constraints: const BoxConstraints(
1445
+ minWidth: 70,
1446
+ minHeight: 70,
1447
+ maxWidth: 150,
1448
+ maxHeight: 150,
1449
+ ),
1450
+ child: Container(color: red, width: 1000, height: 1000),
1451
+ ),
1452
+ )
1453
+ <code_end>
1454
+
1455
+ Center allows ConstrainedBox to be any size up to the
1456
+ screen size. The ConstrainedBox imposes additional
1457
+ constraints from its constraints parameter onto its child.
1458
+ The Container must be between 70 and 150 pixels.
1459
+ It wants to have 1000 pixels,
1460
+ so it ends up having 150 (the maximum).
1461
+ <topic_end>
1462
+ <topic_start>
1463
+ Example 12
1464
+
1465
+ <code_start>
1466
+ Center(
1467
+ child: ConstrainedBox(
1468
+ constraints: const BoxConstraints(
1469
+ minWidth: 70,
1470
+ minHeight: 70,
1471
+ maxWidth: 150,
1472
+ maxHeight: 150,
1473
+ ),
1474
+ child: Container(color: red, width: 100, height: 100),
1475
+ ),
1476
+ )
1477
+ <code_end>
1478
+
1479
+ Center allows ConstrainedBox to be any size up to the
1480
+ screen size. The ConstrainedBox imposes additional
1481
+ constraints from its constraints parameter onto its child.
1482
+ The Container must be between 70 and 150 pixels.
1483
+ It wants to have 100 pixels, and that’s the size it has,
1484
+ since that’s between 70 and 150.
1485
+ <topic_end>
1486
+ <topic_start>
1487
+ Example 13
1488
+
1489
+ <code_start>
1490
+ UnconstrainedBox(
1491
+ child: Container(color: red, width: 20, height: 50),
1492
+ )
1493
+ <code_end>
1494
+
1495
+ The screen forces the UnconstrainedBox to be exactly
1496
+ the same size as the screen. However, the UnconstrainedBox
1497
+ lets its child Container be any size it wants.
1498
+ <topic_end>
1499
+ <topic_start>
1500
+ Example 14
1501
+
1502
+ <code_start>
1503
+ UnconstrainedBox(
1504
+ child: Container(color: red, width: 4000, height: 50),
1505
+ )
1506
+ <code_end>
1507
+
1508
+ The screen forces the UnconstrainedBox to be exactly
1509
+ the same size as the screen, and UnconstrainedBox
1510
+ lets its child Container be any size it wants.
1511
+ Unfortunately, in this case the Container is
1512
+ 4000 pixels wide and is too big to fit in
1513
+ the UnconstrainedBox, so the UnconstrainedBox displays
1514
+ the much dreaded “overflow warning”.
1515
+ <topic_end>
1516
+ <topic_start>
1517
+ Example 15
1518
+
1519
+ <code_start>
1520
+ OverflowBox(
1521
+ minWidth: 0,
1522
+ minHeight: 0,
1523
+ maxWidth: double.infinity,
1524
+ maxHeight: double.infinity,
1525
+ child: Container(color: red, width: 4000, height: 50),
1526
+ )
1527
+ <code_end>
1528
+
1529
+ The screen forces the OverflowBox to be exactly the same
1530
+ size as the screen, and OverflowBox lets its child Container
1531
+ be any size it wants.
1532
+ OverflowBox is similar to UnconstrainedBox;
1533
+ the difference is that it won’t display any warnings
1534
+ if the child doesn’t fit the space.
1535
+ In this case, the Container has 4000 pixels of width,
1536
+ and is too big to fit in the OverflowBox,
1537
+ but the OverflowBox simply shows as much as it can,
1538
+ with no warnings given.
1539
+ <topic_end>
1540
+ <topic_start>
1541
+ Example 16
1542
+
1543
+ <code_start>
1544
+ UnconstrainedBox(
1545
+ child: Container(color: Colors.red, width: double.infinity, height: 100),
1546
+ )
1547
+ <code_end>
1548
+
1549
+ This won’t render anything, and you’ll see an error in the console.
1550
+ The UnconstrainedBox lets its child be any size it wants,
1551
+ however its child is a Container with infinite size.
1552
+ Flutter can’t render infinite sizes, so it throws an error with
1553
+ the following message: BoxConstraints forces an infinite width.
1554
+ <topic_end>
1555
+ <topic_start>
1556
+ Example 17
1557
+
1558
+ <code_start>
1559
+ UnconstrainedBox(
1560
+ child: LimitedBox(
1561
+ maxWidth: 100,
1562
+ child: Container(
1563
+ color: Colors.red,
1564
+ width: double.infinity,
1565
+ height: 100,
1566
+ ),
1567
+ ),
1568
+ )
1569
+ <code_end>
1570
+
1571
+ Here you won’t get an error anymore,
1572
+ because when the LimitedBox is given an
1573
+ infinite size by the UnconstrainedBox;
1574
+ it passes a maximum width of 100 down to its child.
1575
+ If you swap the UnconstrainedBox for a Center widget,
1576
+ the LimitedBox won’t apply its limit anymore
1577
+ (since its limit is only applied when it gets infinite
1578
+ constraints), and the width of the Container
1579
+ is allowed to grow past 100.
1580
+ This explains the difference between a LimitedBox
1581
+ and a ConstrainedBox.
1582
+ <topic_end>
1583
+ <topic_start>
1584
+ Example 18
1585
+
1586
+ <code_start>
1587
+ const FittedBox(
1588
+ child: Text('Some Example Text.'),
1589
+ )
1590
+ <code_end>
1591
+
1592
+ The screen forces the FittedBox to be exactly the same
1593
+ size as the screen. The Text has some natural width
1594
+ (also called its intrinsic width) that depends on the
1595
+ amount of text, its font size, and so on.
1596
+ The FittedBox lets the Text be any size it wants,
1597
+ but after the Text tells its size to the FittedBox,
1598
+ the FittedBox scales the Text until it fills all of
1599
+ the available width.
1600
+ <topic_end>
1601
+ <topic_start>
1602
+ Example 19
1603
+
1604
+ <code_start>
1605
+ const Center(
1606
+ child: FittedBox(
1607
+ child: Text('Some Example Text.'),
1608
+ ),
1609
+ )
1610
+ <code_end>
1611
+
1612
+ But what happens if you put the FittedBox inside of a
1613
+ Center widget? The Center lets the FittedBox
1614
+ be any size it wants, up to the screen size.
1615
+ The FittedBox then sizes itself to the Text,
1616
+ and lets the Text be any size it wants.
1617
+ Since both FittedBox and the Text have the same size,
1618
+ no scaling happens.
1619
+ <topic_end>
1620
+ <topic_start>
1621
+ Example 20
1622
+
1623
+ <code_start>
1624
+ const Center(
1625
+ child: FittedBox(
1626
+ child: Text(
1627
+ 'This is some very very very large text that is too big to fit a regular screen in a single line.'),
1628
+ ),
1629
+ )
1630
+ <code_end>
1631
+
1632
+ However, what happens if FittedBox is inside of a Center
1633
+ widget, but the Text is too large to fit the screen?
1634
+ FittedBox tries to size itself to the Text,
1635
+ but it can’t be bigger than the screen.
1636
+ It then assumes the screen size,
1637
+ and resizes Text so that it fits the screen, too.
1638
+ <topic_end>
1639
+ <topic_start>
1640
+ Example 21
1641
+
1642
+ <code_start>
1643
+ const Center(
1644
+ child: Text(
1645
+ 'This is some very very very large text that is too big to fit a regular screen in a single line.'),
1646
+ )
1647
+ <code_end>
1648
+
1649
+ If, however, you remove the FittedBox, the Text
1650
+ gets its maximum width from the screen,
1651
+ and breaks the line so that it fits the screen.
1652
+ <topic_end>
1653
+ <topic_start>
1654
+ Example 22
1655
+
1656
+ <code_start>
1657
+ FittedBox(
1658
+ child: Container(
1659
+ height: 20,
1660
+ width: double.infinity,
1661
+ color: Colors.red,
1662
+ ),
1663
+ )
1664
+ <code_end>
1665
+
1666
+ FittedBox can only scale a widget that is bounded
1667
+ (has non-infinite width and height). Otherwise,
1668
+ it won’t render anything,
1669
+ and you’ll see an error in the console.
1670
+ <topic_end>
1671
+ <topic_start>
1672
+ Example 23
1673
+
1674
+ <code_start>
1675
+ Row(
1676
+ children: [
1677
+ Container(color: red, child: const Text('Hello!', style: big)),
1678
+ Container(color: green, child: const Text('Goodbye!', style: big)),
1679
+ ],
1680
+ )
1681
+ <code_end>
1682
+
1683
+ The screen forces the Row to be exactly the same size
1684
+ as the screen.
1685
+ Just like an UnconstrainedBox, the Row won’t
1686
+ impose any constraints onto its children,
1687
+ and instead lets them be any size they want.
1688
+ The Row then puts them side-by-side,
1689
+ and any extra space remains empty.
1690
+ <topic_end>
1691
+ <topic_start>
1692
+ Example 24
1693
+
1694
+ <code_start>
1695
+ Row(
1696
+ children: [
1697
+ Container(
1698
+ color: red,
1699
+ child: const Text(
1700
+ 'This is a very long text that '
1701
+ 'won\'t fit the line.',
1702
+ style: big,
1703
+ ),
1704
+ ),
1705
+ Container(color: green, child: const Text('Goodbye!', style: big)),
1706
+ ],
1707
+ )
1708
+ <code_end>
1709
+
1710
+ Since Row won’t impose any constraints onto its children,
1711
+ it’s quite possible that the children might be too big to fit
1712
+ the available width of the Row. In this case, just like an
1713
+ UnconstrainedBox, the Row displays the “overflow warning”.
1714
+ <topic_end>
1715
+ <topic_start>
1716
+ Example 25
1717
+
1718
+ <code_start>
1719
+ Row(
1720
+ children: [
1721
+ Expanded(
1722
+ child: Center(
1723
+ child: Container(
1724
+ color: red,
1725
+ child: const Text(
1726
+ 'This is a very long text that won\'t fit the line.',
1727
+ style: big,
1728
+ ),
1729
+ ),
1730
+ ),
1731
+ ),
1732
+ Container(color: green, child: const Text('Goodbye!', style: big)),
1733
+ ],
1734
+ )
1735
+ <code_end>
1736
+
1737
+ When a Row’s child is wrapped in an Expanded widget,
1738
+ the Row won’t let this child define its own width anymore.
1739
+ Instead, it defines the Expanded width according to the
1740
+ other children, and only then the Expanded widget forces
1741
+ the original child to have the Expanded’s width.
1742
+ In other words, once you use Expanded,
1743
+ the original child’s width becomes irrelevant, and is ignored.
1744
+ <topic_end>
1745
+ <topic_start>
1746
+ Example 26
1747
+
1748
+ <code_start>
1749
+ Row(
1750
+ children: [
1751
+ Expanded(
1752
+ child: Container(
1753
+ color: red,
1754
+ child: const Text(
1755
+ 'This is a very long text that won\'t fit the line.',
1756
+ style: big,
1757
+ ),
1758
+ ),
1759
+ ),
1760
+ Expanded(
1761
+ child: Container(
1762
+ color: green,
1763
+ child: const Text(
1764
+ 'Goodbye!',
1765
+ style: big,
1766
+ ),
1767
+ ),
1768
+ ),
1769
+ ],
1770
+ )
1771
+ <code_end>
1772
+
1773
+ If all of Row’s children are wrapped in Expanded widgets,
1774
+ each Expanded has a size proportional to its flex parameter,
1775
+ and only then each Expanded widget forces its child to have
1776
+ the Expanded’s width.
1777
+ In other words, Expanded ignores the preferred width of
1778
+ its children.
1779
+ <topic_end>
1780
+ <topic_start>
1781
+ Example 27
1782
+
1783
+ <code_start>
1784
+ Row(
1785
+ children: [
1786
+ Flexible(
1787
+ child: Container(
1788
+ color: red,
1789
+ child: const Text(
1790
+ 'This is a very long text that won\'t fit the line.',
1791
+ style: big,
1792
+ ),
1793
+ ),
1794
+ ),
1795
+ Flexible(
1796
+ child: Container(
1797
+ color: green,
1798
+ child: const Text(
1799
+ 'Goodbye!',
1800
+ style: big,
1801
+ ),
1802
+ ),
1803
+ ),
1804
+ ],
1805
+ )
1806
+ <code_end>
1807
+
1808
+ The only difference if you use Flexible instead of Expanded,
1809
+ is that Flexible lets its child have the same or smaller
1810
+ width than the Flexible itself, while Expanded forces
1811
+ its child to have the exact same width of the Expanded.
1812
+ But both Expanded and Flexible ignore their children’s width
1813
+ when sizing themselves.
1814
+ info Note
1815
+ This means that it’s impossible to expand Row children
1816
+ proportionally to their sizes. The Row either uses
1817
+ the exact child’s width, or ignores it completely
1818
+ when you use Expanded or Flexible.
1819
+ <topic_end>
1820
+ <topic_start>
1821
+ Example 28
1822
+
1823
+ <code_start>
1824
+ Scaffold(
1825
+ body: Container(
1826
+ color: blue,
1827
+ child: const Column(
1828
+ children: [
1829
+ Text('Hello!'),
1830
+ Text('Goodbye!'),
1831
+ ],
1832
+ ),
1833
+ ),
1834
+ )
1835
+ <code_end>
1836
+
1837
+ The screen forces the Scaffold to be exactly the same size
1838
+ as the screen, so the Scaffold fills the screen.
1839
+ The Scaffold tells the Container that it can be any size it wants,
1840
+ but not bigger than the screen.
1841
+ info Note
1842
+ When a widget tells its child that it can be smaller than a
1843
+ certain size, we say the widget supplies loose constraints
1844
+ to its child. More on that later.
1845
+ <topic_end>
1846
+ <topic_start>
1847
+ Example 29
1848
+
1849
+ <code_start>
1850
+ Scaffold(
1851
+ body: SizedBox.expand(
1852
+ child: Container(
1853
+ color: blue,
1854
+ child: const Column(
1855
+ children: [
1856
+ Text('Hello!'),
1857
+ Text('Goodbye!'),
1858
+ ],
1859
+ ),
1860
+ ),
1861
+ ),
1862
+ )
1863
+ <code_end>
1864
+
1865
+ If you want the Scaffold’s child to be exactly the same size
1866
+ as the Scaffold itself, you can wrap its child with
1867
+ SizedBox.expand.
1868
+ <topic_end>
1869
+ <topic_start>
1870
+ Tight vs loose constraints
1871
+ It’s very common to hear that some constraint is
1872
+ “tight” or “loose”, so what does that mean?
1873
+ <topic_end>
1874
+ <topic_start>
1875
+ Tight constraints
1876
+ A tight constraint offers a single possibility,
1877
+ an exact size. In other words, a tight constraint
1878
+ has its maximum width equal to its minimum width;
1879
+ and has its maximum height equal to its minimum height.
1880
+ An example of this is the App widget,
1881
+ which is contained by the RenderView class:
1882
+ the box used by the child returned by the
1883
+ application’s build function is given a constraint
1884
+ that forces it to exactly fill the application’s content area
1885
+ (typically, the entire screen).
1886
+ Another example: if you nest a bunch of boxes inside
1887
+ each other at the root of your application’s render tree,
1888
+ they’ll all exactly fit in each other,
1889
+ forced by the box’s tight constraints.
1890
+ If you go to Flutter’s box.dart file and search for
1891
+ the BoxConstraints constructors,
1892
+ you’ll find the following:
1893
+ If you revisit Example 2,
1894
+ the screen forces the red Container to be
1895
+ exactly the same size as the screen.
1896
+ The screen achieves that, of course, by passing tight
1897
+ constraints to the Container.
1898
+ <topic_end>
1899
+ <topic_start>
1900
+ Loose constraints
1901
+ A loose constraint is one that has a minimum
1902
+ of zero and a maximum non-zero.
1903
+ Some boxes loosen the incoming constraints,
1904
+ meaning the maximum is maintained but the
1905
+ minimum is removed, so the widget can have
1906
+ a minimum width and height both equal to zero.
1907
+ Ultimately, Center’s purpose is to transform
1908
+ the tight constraints it received from its parent
1909
+ (the screen) to loose constraints for its child
1910
+ (the Container).
1911
+ If you revisit Example 3,
1912
+ the Center allows the red Container to be smaller,
1913
+ but not bigger than the screen.
1914
+
1915
+ <topic_end>
1916
+ <topic_start>
1917
+ Unbounded constraints
1918
+ info Note
1919
+ You might be directed here if the framework
1920
+ detects a problem involving box constraints.
1921
+ The Flex section below might also apply.
1922
+ In certain situations,
1923
+ a box’s constraint is unbounded, or infinite.
1924
+ This means that either the maximum width or
1925
+ the maximum height is set to double.infinity.
1926
+ A box that tries to be as big as possible won’t
1927
+ function usefully when given an unbounded constraint and,
1928
+ in debug mode, throws an exception.
1929
+ The most common case where a render box ends up
1930
+ with an unbounded constraint is within a flex box
1931
+ (Row or Column),
1932
+ and within a scrollable region
1933
+ (such as ListView and other ScrollView subclasses).
1934
+ ListView, for example,
1935
+ tries to expand to fit the space available
1936
+ in its cross-direction
1937
+ (perhaps it’s a vertically-scrolling block and
1938
+ tries to be as wide as its parent).
1939
+ If you nest a vertically scrolling ListView
1940
+ inside a horizontally scrolling ListView,
1941
+ the inner list tries to be as wide as possible,
1942
+ which is infinitely wide,
1943
+ since the outer one is scrollable in that direction.
1944
+ The next section describes the error you might
1945
+ encounter with unbounded constraints in a Flex widget.
1946
+ <topic_end>
1947
+ <topic_start>
1948
+ Flex
1949
+ A flex box (Row and Column) behaves
1950
+ differently depending on whether its
1951
+ constraint is bounded or unbounded in
1952
+ its primary direction.
1953
+ A flex box with a bounded constraint in its
1954
+ primary direction tries to be as big as possible.
1955
+ A flex box with an unbounded constraint
1956
+ in its primary direction tries to fit its children
1957
+ in that space. Each child’s flex value must be
1958
+ set to zero, meaning that you can’t use
1959
+ Expanded when the flex box is inside
1960
+ another flex box or a scrollable;
1961
+ otherwise it throws an exception.
1962
+ The cross direction
1963
+ (width for Column or height for Row),
1964
+ must never be unbounded,
1965
+ or it can’t reasonably align its children.
1966
+ <topic_end>