Commit 15aee99e authored by Dasun Madushanka's avatar Dasun Madushanka

Update timecard File

parent e5dd4c31
import 'dart:async';
import 'package:flutter/material.dart';
class TimerCard extends StatefulWidget {
const TimerCard({Key key}) : super(key: key);
@override
TimerCardState createState() => TimerCardState();
}
class TimerCardState extends State<TimerCard> {
// The timer's value shown on app's UI.
int _timerValue = 0;
bool _paused = true;
// A stream that updates every second. The stream's value is the number of
// seconds elapsed since the app is started. We simply print this stream value
// out in command-line. The value shown on UI is this._timerValue.
final Stream<int> _periodicStream =
Stream.periodic(Duration(milliseconds: 1000), (i) => i);
// Record of the latest stream value that we saw. Because the StreamBuilder is
// rebuilt when we call setState(), and in the re-build we shouldn't increment
// this._timerValue if the latest snapshot's value hasn't changed.
int _previousStreamValue = 0;
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
margin: EdgeInsets.all(8),
child: StreamBuilder(
stream: this._periodicStream,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
if (snapshot.data != this._previousStreamValue) {
print('Latest snapshot from stream: ${snapshot.data}');
this._previousStreamValue = snapshot.data;
if (!_paused) {
this._timerValue++;
}
}
}
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Card(child: _buildTimerUI()),
],
);
},
),
);
}
Widget _buildTimerUI() {
return Column(
children: <Widget>[
Text(
'$_timerValue',
style: Theme.of(context).textTheme.headline4,
),
ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(this._paused ? Icons.play_arrow : Icons.pause),
onPressed: () => setState(() => this._paused = !this._paused),
),
IconButton(
icon: Icon(Icons.stop),
onPressed: () => setState(
() {
this._timerValue = 0;
this._paused = true;
},
),
),
],
),
],
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment