mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Added sample code to AnimatedWidget (#27803)
* Added sample to AnimatedWidget * Updated AnimatedWidget example to use listenable from parent * Made progress private and changed widget name
This commit is contained in:
parent
307848953f
commit
088b6062fd
@ -23,6 +23,58 @@ export 'package:flutter/rendering.dart' show RelativeRect;
|
||||
/// [AnimatedWidget] is most useful for widgets that are otherwise stateless. To
|
||||
/// use [AnimatedWidget], simply subclass it and implement the build function.
|
||||
///
|
||||
///{@tool sample}
|
||||
///
|
||||
/// This code defines a widget called `Spinner` that spins a green square
|
||||
/// continually. It is built with an [AnimatedWidget].
|
||||
///
|
||||
/// ```dart
|
||||
/// class Spinner extends StatefulWidget {
|
||||
/// @override
|
||||
/// _SpinnerState createState() => _SpinnerState();
|
||||
/// }
|
||||
///
|
||||
/// class _SpinnerState extends State<Spinner> with TickerProviderStateMixin {
|
||||
/// AnimationController _controller;
|
||||
///
|
||||
/// @override
|
||||
/// void initState() {
|
||||
/// super.initState();
|
||||
/// _controller = AnimationController(
|
||||
/// duration: const Duration(seconds: 10),
|
||||
/// vsync: this,
|
||||
/// )..repeat();
|
||||
/// }
|
||||
///
|
||||
/// @override
|
||||
/// void dispose() {
|
||||
/// _controller.dispose();
|
||||
/// super.dispose();
|
||||
/// }
|
||||
///
|
||||
/// @override
|
||||
/// Widget build(BuildContext context) {
|
||||
/// return SpinningContainer(controller: _controller);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// class SpinningContainer extends AnimatedWidget {
|
||||
/// const SpinningContainer({Key key, AnimationController controller})
|
||||
/// : super(key: key, listenable: controller);
|
||||
///
|
||||
/// Animation<double> get _progress => listenable;
|
||||
///
|
||||
/// @override
|
||||
/// Widget build(BuildContext context) {
|
||||
/// return Transform.rotate(
|
||||
/// angle: _progress.value * 2.0 * math.pi,
|
||||
/// child: Container(width: 200.0, height: 200.0, color: Colors.green),
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// For more complex case involving additional state, consider using
|
||||
/// [AnimatedBuilder].
|
||||
///
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user