Created
May 8, 2020 07:24
-
-
Save orestesgaolin/5798f76a62534aa3cf7f150b541cc175 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
import 'dart:math' as math; | |
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Animated Label', | |
theme: ThemeData( | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
double labelValue = 0.0; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Container( | |
child: Center( | |
child: AnimatedLabel( | |
labelValue: labelValue, | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.refresh), | |
onPressed: () { | |
labelValue = math.Random().nextDouble() * 100.0; | |
setState(() {}); | |
}, | |
), | |
); | |
} | |
} | |
class AnimatedLabel extends StatelessWidget { | |
final double labelValue; | |
const AnimatedLabel({ | |
Key key, | |
@required this.labelValue, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return TweenAnimationBuilder<double>( | |
duration: Duration(milliseconds: 500), | |
tween: Tween(begin: 0.0, end: labelValue), | |
builder: (context, value, child) { | |
return Text( | |
'${value.toStringAsFixed(2)}', | |
style: TextStyle(fontSize: 50), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment