text
stringlengths
1
474
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
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';
void main() {
runApp(const FadeAppTest());
}
class FadeAppTest extends StatelessWidget {
/// This widget is the root of your application.
const FadeAppTest({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Fade Demo',
home: MyFadeTest(title: 'Fade Demo'),
);
}
}
class MyFadeTest extends StatefulWidget {
const MyFadeTest({super.key, required this.title});
final String title;
@override
State<MyFadeTest> createState() => _MyFadeTest();
}
class _MyFadeTest extends State<MyFadeTest> with TickerProviderStateMixin {
late final AnimationController controller;
late final CurvedAnimation curve;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 2000),
vsync: this,
);
curve = CurvedAnimation(
parent: controller,
curve: Curves.easeIn,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: FadeTransition(
opacity: curve,
child: const FlutterLogo(size: 100),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
controller.forward();
},
tooltip: 'Fade',
child: const Icon(Icons.brush),
),
);
}
}<code_end>
For more information, see Animation & Motion widgets,
the Animations tutorial, and the Animations overview.<topic_end>
<topic_start>
How do I draw/paint on the screen?
Xamarin.Forms never had a built-in way to draw directly on the screen.
Many would use SkiaSharp, if they needed a custom image drawn.
In Flutter, you have direct access to the Skia Canvas
and can easily draw on screen.Flutter has two classes that help you draw to the canvas: CustomPaint
and CustomPainter, the latter of which implements your algorithm to draw to
the canvas.To learn how to implement a signature painter in Flutter,
see Collin’s answer on Custom Paint.
<code_start>import 'package:flutter/material.dart';
void main() {