wuweijian1997 / FlutterCountdownTimer

A flutter countdown timer. [10 days 5:30:46] ⬇⬇⬇⬇ | flutter倒计时
BSD 3-Clause "New" or "Revised" License
95 stars 46 forks source link

Is it possible to show the milliseconds with the default countdown widget? #49

Open lwitthus opened 2 years ago

wuweijian1997 commented 2 years ago

` class _CountdownTimerPageState extends State with SingleTickerProviderStateMixin { late CountdownTimerController controller; int endTime = DateTime.now().millisecondsSinceEpoch + Duration(seconds: 30).inMilliseconds;

@override void initState() { super.initState(); controller = CountdownTimerController(endTime: endTime, onEnd: onEnd, vsync: this); }

@override Widget build(BuildContext context) { return Scaffold( body: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ CountdownTimer( controller: controller, widgetBuilder: (BuildContext context, CurrentRemainingTime? time) { if (time == null) { return Text('Game over'); } List list = []; if (time.sec != null) { list.add(Row( children: [ Icon(Icons.sentiment_very_satisfied), Text(time.sec.toString()), ], )); } if (time.milliseconds != null) { list.add(Row( children: [ Icon(Icons.sentiment_very_satisfied), AnimatedBuilder( animation: time.milliseconds!, builder: (context, child) { return Text("${(time.milliseconds!.value * 1000).toInt()}"); }, ) ], )); } return Row( mainAxisAlignment: MainAxisAlignment.center, children: list, ); }, ), ], ), ); }

} `

lwitthus commented 2 years ago

Okay, but i use the Default Countdown Controller

lwitthus commented 2 years ago

https://github.com/wuweijian1997/FlutterCountdownTimer/blob/master/lib/countdown_controller.dart

wuweijian1997 commented 2 years ago

import 'package:flutter/material.dart'; import 'package:flutter_countdown_timer/index.dart';

class CountdownPage extends StatefulWidget { @override _CountdownPageState createState() => _CountdownPageState(); }

class _CountdownPageState extends State with SingleTickerProviderStateMixin { CountdownController countdownController = CountdownController(duration: Duration(seconds: 30), onEnd: () { print('onEnd'); }); late AnimationController controller; bool isRunning = false;

@override void initState() { controller = AnimationController(vsync: this, value: 0, duration: Duration(seconds: 1)); countdownController.addListener(() { controller.reverse(from: 1); }); super.initState(); }

@override Widget build(BuildContext context) { return Scaffold( body: Center( child: Countdown( countdownController: countdownController, builder: (, Duration time) { return AnimatedBuilder( animation: controller, builder: (, __) { return Text( 'hours: ${time.inHours} minutes: ${time.inMinutes % 60} seconds: ${time.inSeconds % 60} milliseconds: ${(controller.value * 1000).toInt()}', style: TextStyle(fontSize: 20), ); }, ); }), ), floatingActionButton: FloatingActionButton( child: Icon(isRunning ? Icons.stop : Icons.play_arrow), onPressed: () { if (!countdownController.isRunning) { countdownController.start(); controller.reverse(from: 1); setState(() { isRunning = true; }); } else { countdownController.stop(); controller.stop(); setState(() { isRunning = false; }); } }, ), ); } }

wuweijian1997 commented 2 years ago

This can do the same effect