Created
November 22, 2021 12:10
-
-
Save Origogi/113362474ce196345ecc6bc1d20ab848 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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/animation.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin | |
{ | |
int timerValue = 10; | |
int _counter = 10; | |
bool _showCompleted = false; | |
late Animation<double> animation; | |
late AnimationController controller; | |
@override | |
void initState() { | |
super.initState(); | |
controller = AnimationController(duration : Duration(seconds : timerValue.toInt()), vsync : this); | |
animation = Tween<double>(begin : timerValue.toDouble(), end : 0) | |
.animate(controller) | |
..addStatusListener((status) { | |
if (AnimationStatus.completed == status) { | |
_showCompleted = true; | |
} else { | |
_showCompleted = false; | |
} | |
setState((){ | |
}); | |
}); | |
// ..addListener(() { | |
// setState((){ | |
// _counter = animation.value.toInt(); | |
// }) ; | |
// }); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print("build()"); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
if (_showCompleted) | |
...[Text('Complete')], | |
AnimatedBuilder( | |
animation: controller, | |
child : Text('$_counter', style: Theme.of(context).textTheme.headline2), | |
builder : (buildContext, child) { | |
return Text('${animation.value.toInt()}', style: Theme.of(context).textTheme.headline2); | |
} | |
), | |
ElevatedButton( onPressed: () { | |
print('start'); | |
controller.reset(); | |
controller.forward(); | |
} | |
, child: Text('Start'), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment