From daa70608825963bbef7a473d44e56195410dceea Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Fri, 24 May 2019 09:24:16 -0700 Subject: [PATCH] Add stateful_widget_animation snippet template (#33295) --- dev/snippets/config/templates/README.md | 5 ++ .../templates/stateful_widget_animation.tmpl | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 dev/snippets/config/templates/stateful_widget_animation.tmpl diff --git a/dev/snippets/config/templates/README.md b/dev/snippets/config/templates/README.md index 8d7f260e68..5ae2e9cdf7 100644 --- a/dev/snippets/config/templates/README.md +++ b/dev/snippets/config/templates/README.md @@ -86,3 +86,8 @@ follows: - [`stateless_widget_scaffold`](stateless_widget_scaffold.tmpl) : Similar to `stateless_widget_material`, except that it wraps the stateless widget with a Scaffold. + +- [`stateful_widget_animation`](stateful_widget_animation.tmpl) : Similar to + `stateful_widget`, except that it declares an `AnimationController` instance + variable called `_controller`, and it properly initializes the controller + in `initState()` and properly disposes of the controller in `dispose()`. diff --git a/dev/snippets/config/templates/stateful_widget_animation.tmpl b/dev/snippets/config/templates/stateful_widget_animation.tmpl new file mode 100644 index 0000000000..2e2b4a5fd3 --- /dev/null +++ b/dev/snippets/config/templates/stateful_widget_animation.tmpl @@ -0,0 +1,53 @@ +// Flutter code sample for {{id}} + +{{description}} + +import 'package:flutter/widgets.dart'; + +{{code-imports}} + +void main() => runApp(new MyApp()); + +/// This Widget is the main application widget. +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return WidgetsApp( + title: 'Flutter Code Sample', + home: MyStatefulWidget(), + color: const Color(0xffffffff), + ); + } +} + +{{code-preamble}} + +/// This is the stateful widget that the main application instantiates. +class MyStatefulWidget extends StatefulWidget { + MyStatefulWidget({Key key}) : super(key: key); + + @override + _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); +} + +// This is the private State class that goes with MyStatefulWidget. +class _MyStatefulWidgetState extends State { + AnimationController _controller; + + @override + void initState() { + super.initState(); + _controller = AnimationController( + vsync: this, + duration: const Duration(seconds: 1), + ); + } + + @override + void dispose() { + _controller.dispose(); + super.dispose(); + } + + {{code}} +}