Created
June 30, 2019 21:16
-
-
Save aneury1/6cab3e02adbbcb56b4c1a7c46f908ec5 to your computer and use it in GitHub Desktop.
This file contains 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
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Animation Demo', | |
theme: new ThemeData( | |
primaryColor: new Color(0xFFFF0000), | |
), | |
home: new FormDemo(), | |
); | |
} | |
} | |
class FormDemo extends StatefulWidget { | |
@override | |
_FormDemoState createState() => _FormDemoState(); | |
} | |
class _FormDemoState extends State<FormDemo> with SingleTickerProviderStateMixin { | |
AnimationController _controller; | |
Animation _animation; | |
FocusNode _focusNode = FocusNode(); | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController(vsync: this, duration: Duration(milliseconds: 300)); | |
_animation = Tween(begin: 300.0, end: 50.0).animate(_controller) | |
..addListener(() { | |
setState(() {}); | |
}); | |
_focusNode.addListener(() { | |
if (_focusNode.hasFocus) { | |
_controller.forward(); | |
} else { | |
_controller.reverse(); | |
} | |
}); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
_focusNode.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
resizeToAvoidBottomPadding: false, // this avoids the overflow error | |
appBar: AppBar( | |
title: Text('TextField Animation Demo'), | |
), | |
body: new InkWell( // to dismiss the keyboard when the user tabs out of the TextField | |
splashColor: Colors.transparent, | |
onTap: () { | |
FocusScope.of(context).requestFocus(FocusNode()); | |
}, | |
child: Container( | |
padding: const EdgeInsets.all(20.0), | |
child: Column( | |
children: <Widget>[ | |
SizedBox(height: _animation.value), | |
TextFormField( | |
decoration: InputDecoration( | |
labelText: 'I move!', | |
), | |
focusNode: _focusNode, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment