Created
October 15, 2018 13:07
-
-
Save brianegan/7eef9d2f0d6683cec28a662842aa0e99 to your computer and use it in GitHub Desktop.
Animator Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool _selected = false; | |
void _incrementCounter() { | |
setState(() { | |
_selected = !_selected; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(widget.title), | |
), | |
body: Animator( | |
title: widget.title, | |
selected: _selected, | |
), | |
floatingActionButton: new FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: new Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} | |
class Animator extends StatefulWidget { | |
final String title; | |
final bool selected; | |
const Animator({Key key, @required this.title, @required this.selected}) | |
: super(key: key); | |
@override | |
_AnimatorState createState() => _AnimatorState(); | |
} | |
class _AnimatorState extends State<Animator> | |
with SingleTickerProviderStateMixin<Animator> { | |
AnimationController _controller; | |
Animation _animation; | |
@override | |
void initState() { | |
_controller = | |
AnimationController(vsync: this, duration: Duration(milliseconds: 500)); | |
_animation = | |
ColorTween(begin: Colors.white, end: Colors.red).animate(_controller); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
void didUpdateWidget(Animator oldWidget) { | |
super.didUpdateWidget(oldWidget); | |
if (oldWidget.selected != widget.selected) { | |
if (widget.selected) { | |
_controller.forward(); | |
} else { | |
_controller.reverse(); | |
} | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: _controller, | |
builder: (context, child) { | |
print('${_animation.value}'); | |
return Container( | |
color: _animation.value, | |
child: Center( | |
child: Text(widget.title), | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment