text
stringlengths
1
372
slider, the radio buttons. as the user interacts with the UI, changes must be
reflected in every other place. worse, unless care is taken, a minor change to
one part of the user interface can cause ripple effects to seemingly unrelated
pieces of code.
one solution to this is an approach like MVC, where you push data changes to the
model via the controller, and then the model pushes the new state to the view
via the controller. however, this also is problematic, since creating and
updating UI elements are two separate steps that can easily get out of sync.
flutter, along with other reactive frameworks, takes an alternative approach to
this problem, by explicitly decoupling the user interface from its underlying
state. with react-style APIs, you only create the UI description, and the
framework takes care of using that one configuration to both create and/or
update the user interface as appropriate.
in flutter, widgets (akin to components in react) are represented by immutable
classes that are used to configure a tree of objects. these widgets are used to
manage a separate tree of objects for layout, which is then used to manage a
separate tree of objects for compositing. flutter is, at its core, a series of
mechanisms for efficiently walking the modified parts of trees, converting trees
of objects into lower-level trees of objects, and propagating changes across
these trees.
a widget declares its user interface by overriding the build() method, which
is a function that converts state to UI:
the build() method is by design fast to execute and should be free of side
effects, allowing it to be called by the framework whenever needed (potentially
as often as once per rendered frame).
this approach relies on certain characteristics of a language runtime (in
particular, fast object instantiation and deletion). fortunately, dart is
particularly well suited for this
task.
<topic_end>
<topic_start>
widgets
as mentioned, flutter emphasizes widgets as a unit of composition. widgets are
the building blocks of a flutter app’s user interface, and each widget is an
immutable declaration of part of the user interface.
widgets form a hierarchy based on composition. each widget nests inside its
parent and can receive context from the parent. this structure carries all the
way up to the root widget (the container that hosts the flutter app, typically
MaterialApp or CupertinoApp), as this trivial example shows:
<code_start>
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
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