Event Loop in Dart
Event Loop in Dart
https://www.dhiwise.com/post/how-dart-event-
loop-influence-the-execution-dart-code
When you create a Container and give it a child widget, remember that
it's the parent widget in the render tree that decides the Container's size.
SizedBox
Constraints go down
This means that the parent widget imposes constraints on its children.
Constraints define the minimum and maximum width and height that a
child widget can have. These constraints are passed from the parent to
the child during the layout phase.
Column(
children: [
Container(
width: 100, // Explicit width constraint
height: 50, // Explicit height constraint
color: Colors.red,
),
],
)
In this case, the Container receives constraints from the Column, and it
will use these constraints to decide its own size.
Sizes go up
After a child widget receives constraints from its parent, it decides its
own size based on these constraints. Once the child widget has
determined its size, it reports this size back to the parent.
Continuing with the previous example, the Container decides that it will
be 100 pixels wide and 50 pixels tall (as specified). This size information
is then reported back to the Column.
For instance, the Column will position the Container within its own layout
according to the specified alignment. By default, children in a Column
are positioned one after another vertically.
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Flutter Layout Rule')),
body: Center(
child: Container(
color: Colors.blue,
width: 200,
height: 200,
child: Container(
width: 100,
height: 100,
color: Colors.red,
child: const Text('Hello, World!'),
),
),
),
),
);
}
}
Explanation of the Layout Process
Constraints go down:
The Center widget centers its child within itself and passes down loose constraints allowing the child to
be as small or as large as it wants (within the constraints of the screen size).
The outer Container (blue) receives these loose constraints from the Center. It then passes constraints down to
its child (the inner Container).
Sizes go up:
The outer Container (blue) specifies its own size as 200x200, within the constraints it received from the
Center.
The inner Container (red) receives the constraints from its parent (the blue Container). Since it has
specified its own width and height as 100x100, it chooses this size within the constraints provided by the blue
Container.
Parent sets position:
The Center positions the blue Container at the center of the available space.
The blue Container then positions the red Container at the top-left corner of its own bounding box, because the
default alignment for a Container's child is the top-left corner.
Visual Representation
Here's a step-by-step visualization:
Step 1: The Center widget centers its child (the blue Container) in the middle of the screen.
Step 2: The blue Container sets its own size to 200x200 pixels.
Step 3: The blue Container passes constraints to its child (the red Container), allowing it to be at most 200x200
pixels.
Step 4: The red Container sets its own size to 100x100 pixels.
Step 5: The blue Container positions the red Container at the top-left corner of the 200x200 space it occupies.
Step 6: The text "Hello, World!" is placed inside the red Container.
Conclusion
This example demonstrates the layout rule in Flutter effectively:
Constraints go down: Constraints flow from the Center to the blue Container, and then to the red
Container.
Sizes go up: The red Container determines its size (100x100), which is then used by the blue Container
(200x200) to finalize its own layout.
Parent sets position: The Center positions the blue Container in the center, and the blue Container
positions the red Container at its top-left corner.
Factory Constructors
Factory constructors are a key part of Flutter. They can return an
instance of a class that is either a fresh instance or an instance that has
been previously created, i.e., an existing instance. This is different from
a default constructor which always returns a new instance. The factory
constructor can also return an instance of a different class.