text
stringlengths
1
372
}
<code_end>
you can add padding to any widget,
which mimics the functionality of constraints in iOS.
you can view the layouts that flutter has to offer
in the widget catalog.
<topic_end>
<topic_start>
removing widgets
in UIKit, you call addSubview() on the parent,
or removeFromSuperview() on a child view
to dynamically add or remove child views.
in flutter, because widgets are immutable,
there is no direct equivalent to addSubview().
instead, you can pass a function to the parent
that returns a widget, and control that child’s creation
with a boolean flag.
the following example shows how to toggle between two widgets
when the user clicks the FloatingActionButton:
<code_start>
class SampleApp extends StatelessWidget {
// this widget is the root of your application.
const SampleApp({super.key});
@override
widget build(BuildContext context) {
return const MaterialApp(
title: 'sample app',
home: SampleAppPage(),
);
}
}
class SampleAppPage extends StatefulWidget {
const SampleAppPage({super.key});
@override
State<SampleAppPage> createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage> {
// default value for toggle.
bool toggle = true;
void _toggle() {
setState(() {
toggle = !toggle;
});
}
widget _getToggleChild() {
if (toggle) {
return const Text('Toggle one');
}
return CupertinoButton(
onPressed: () {},
child: const Text('Toggle two'),
);
}
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: const Text('Sample app'),
),
body: center(
child: _getToggleChild(),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggle,
tooltip: 'update text',
child: const Icon(Icons.update),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
animations
in UIKit, you create an animation by calling the
animate(withDuration:animations:) method on a view.
in flutter, use the animation library
to wrap widgets inside an animated widget.
in flutter, use an AnimationController, which is an animation<double>
that can pause, seek, stop, and reverse the animation.
it requires a ticker that signals when vsync happens
and produces a linear interpolation
between 0 and 1 on each frame while it’s running.
you then create one or more
animations and attach them to the controller.
for example, you might use CurvedAnimation
to implement an animation along an interpolated curve.
in this sense, the controller is the “master” source
of the animation progress
and the CurvedAnimation computes the curve
that replaces the controller’s default linear motion.
like widgets, animations in flutter work with composition.
when building the widget tree you assign the animation to an animated
property of a widget, such as the opacity of a FadeTransition,
and tell the controller to start the animation.
the following example shows how to write a FadeTransition that
fades the widget into a logo when you press the FloatingActionButton:
<code_start>
import 'package:flutter/material.dart';
class SampleApp extends StatelessWidget {