text
stringlengths
1
372
in Xamarin.Forms, most developers write layouts in XAML,
though sometimes in c#.
in flutter, you write your layouts with a widget tree in code.
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: ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.only(left: 20, right: 30),
),
onPressed: () {},
child: const Text('Hello'),
),
),
);
}
<code_end>
you can view the layouts that flutter has to offer in the
widget catalog.
<topic_end>
<topic_start>
how do i add or remove an element from my layout?
in Xamarin.Forms, you had to remove or add an element in code.
this involved either setting the content property or calling
add() or remove() if it was a list.
in flutter, because widgets are immutable there is no direct equivalent.
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>
how do i animate a widget?
in Xamarin.Forms, you create simple animations using ViewExtensions that
include methods such as FadeTo and TranslateTo.
you would use these methods on a view
to perform the required animations.
then in code behind, or a behavior, this would fade in the image,
over a 1-second period.
in flutter, you animate widgets using the animation library
by wrapping widgets inside an animated widget.
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 moreAnimations and attach them to the controller.
for example, you might use CurvedAnimation