text
stringlengths 1
474
|
---|
}<code_end> |
<topic_end> |
<topic_start> |
How do I animate a widget? |
In Android, you either create animations using XML, or call the animate() |
method on a view. In Flutter, animate widgets using the animation |
library by wrapping 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'; |
void main() { |
runApp(const FadeAppTest()); |
} |
class FadeAppTest extends StatelessWidget { |
const FadeAppTest({super.key}); |
// This widget is the root of your application. |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Fade Demo', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const 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 AnimationController controller; |
late 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( |
tooltip: 'Fade', |
onPressed: () { |
controller.forward(); |
}, |
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 use a Canvas to draw/paint? |
In Android, you would use the Canvas and Drawable |
to draw images and shapes to the screen. |
Flutter has a similar Canvas API as well, |
since it’s based on the same low-level rendering engine, Skia. |
As a result, painting to a canvas in Flutter |
is a very familiar task for Android developers.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() => runApp(const MaterialApp(home: DemoApp())); |
class DemoApp extends StatelessWidget { |
const DemoApp({super.key}); |