text stringlengths 1 474 |
|---|
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
home: Scaffold( |
appBar: AppBar( |
title: const Text('My Home Page'), |
), |
body: Center( |
child: Builder( |
builder: (context) { |
return Column( |
children: [ |
const Text('Hello World'), |
const SizedBox(height: 20), |
ElevatedButton( |
onPressed: () { |
print('Click!'); |
}, |
child: const Text('A button'), |
), |
], |
); |
}, |
), |
), |
), |
); |
} |
}<code_end> |
In the preceding code, all instantiated classes are widgets.Apps update their user interface in response to events (such as a user |
interaction) by telling the framework to replace a widget in the hierarchy with |
another widget. The framework then compares the new and old widgets, and |
efficiently updates the user interface.Flutter has its own implementations of each UI control, rather than deferring to |
those provided by the system: for example, there is a pure Dart |
implementation of both the |
iOS Toggle |
control |
and the one for the |
Android equivalent.This approach provides several benefits:<topic_end> |
<topic_start> |
Composition |
Widgets are typically composed of many other small, single-purpose widgets that |
combine to produce powerful effects.Where possible, the number of design concepts is kept to a minimum while |
allowing the total vocabulary to be large. For example, in the widgets layer, |
Flutter uses the same core concept (a Widget) to represent drawing to the |
screen, layout (positioning and sizing), user interactivity, state management, |
theming, animations, and navigation. In the animation layer, a pair of concepts, |
Animations and Tweens, cover most of the design space. In the rendering |
layer, RenderObjects are used to describe layout, painting, hit testing, and |
accessibility. In each of these cases, the corresponding vocabulary ends up |
being large: there are hundreds of widgets and render objects, and dozens of |
animation and tween types.The class hierarchy is deliberately shallow and broad to maximize the possible |
number of combinations, focusing on small, composable widgets that each do one |
thing well. Core features are abstract, with even basic features like padding |
and alignment being implemented as separate components rather than being built |
into the core. (This also contrasts with more traditional APIs where features |
like padding are built in to the common core of every layout component.) So, for |
example, to center a widget, rather than adjusting a notional Align property, |
you wrap it in a Center |
widget.There are widgets for padding, alignment, rows, columns, and grids. These layout |
widgets do not have a visual representation of their own. Instead, their sole |
purpose is to control some aspect of another widget’s layout. Flutter also |
includes utility widgets that take advantage of this compositional approach.For example, Container, a |
commonly used widget, is made up of several widgets responsible for layout, |
painting, positioning, and sizing. Specifically, Container is made up of the |
LimitedBox, |
ConstrainedBox, |
Align, |
Padding, |
DecoratedBox, and |
Transform widgets, as you |
can see by reading its source code. A defining characteristic of Flutter is that |
you can drill down into the source for any widget and examine it. So, rather |
than subclassing Container to produce a customized effect, you can compose it |
and other widgets in novel ways, or just create a new widget using |
Container as inspiration.<topic_end> |
<topic_start> |
Building widgets |
As mentioned earlier, you determine the visual representation of a widget by |
overriding the |
build() function to |
return a new element tree. This tree represents the widget’s part of the user |
interface in more concrete terms. For example, a toolbar widget might have a |
build function that returns a horizontal |
layout of some |
text and |
various |
buttons. As needed, |
the framework recursively asks each widget to build until the tree is entirely |
described by concrete renderable |
objects. The |
framework then stitches together the renderable objects into a renderable object |
tree.A widget’s build function should be free of side effects. Whenever the function |
is asked to build, the widget should return a new tree of widgets1, regardless of what the widget previously returned. The |
framework does the heavy lifting work to determine which build methods need to |
be called based on the render object tree (described in more detail later). More |
information about this process can be found in the Inside Flutter |
topic.On each rendered frame, Flutter can recreate just the parts of the UI where the |
state has changed by calling that widget’s build() method. Therefore it is |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.