Last active
March 1, 2022 07:09
-
-
Save demirdev/81ae0aca6a56167c2cf27ac68f9dd9fc to your computer and use it in GitHub Desktop.
Count Down Widget with Tween Animation in 24 lines of code.
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'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: CountDown( | |
onEnd: () {}, durationInSeconds: 10, onEndText: 'onEnd'), | |
), | |
), | |
); | |
} | |
} | |
class CountDown extends StatelessWidget { | |
final int durationInSeconds; | |
final Function onEnd; | |
final String? onEndText; | |
const CountDown( | |
{Key? key, | |
required this.onEnd, | |
required this.durationInSeconds, | |
this.onEndText}) | |
: super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
final _duration = Duration(seconds: durationInSeconds); | |
return TweenAnimationBuilder<Duration>( | |
onEnd: () => onEnd(), | |
duration: _duration, | |
tween: Tween( | |
begin: _duration, | |
end: Duration.zero, | |
), | |
builder: (context, Duration value, Widget? child) { | |
return Text((value == Duration.zero) | |
? (onEndText ?? '0') | |
: (value.inMilliseconds / 1000).toStringAsFixed(3)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment