text
stringlengths
1
474
<topic_end>
<topic_start>
Widget layout
In UIKit, you might use a Storyboard file
to organize your views and set constraints,
or you might set your constraints programmatically in your view controllers.
In Flutter, declare your layout in code by composing a widget tree.The following example shows how to display a simple widget with padding:
<code_start>@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Sample App')),
body: Center(
child: CupertinoButton(
onPressed: () {},
padding: const EdgeInsets.only(left: 10, right: 10),
child: const Text('Hello'),
),
),
);
}<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