Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Commit 59c22dd

Browse files
author
Luis Correa
committed
get user time and countdown
1 parent aff34d5 commit 59c22dd

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

lib/components/connected_form.dart

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:progress_dialog/progress_dialog.dart';
35

@@ -16,6 +18,10 @@ class ConnectedForm extends StatefulWidget {
1618
class _ConnectedFormState extends State<ConnectedForm> {
1719
NautaClient nautaClient;
1820
ProgressDialog pr;
21+
get time => remaining.toString().split('.')[0];
22+
23+
Duration remaining;
24+
Timer _timer;
1925

2026
@override
2127
void initState() {
@@ -24,8 +30,50 @@ class _ConnectedFormState extends State<ConnectedForm> {
2430
setState(() {
2531
nautaClient = NautaClient(user: widget.username, password: '');
2632

27-
nautaClient.loadLastSession();
33+
nautaClient.loadLastSession().then((value) => {
34+
nautaClient.remainingTime().then((value) {
35+
final rtime = value.split(':');
36+
final hour = rtime[0];
37+
final min = rtime[1];
38+
final sec = rtime[2];
39+
setState(() {
40+
remaining = Duration(
41+
hours: int.parse(hour),
42+
minutes: int.parse(min),
43+
seconds: int.parse(sec));
44+
});
45+
})
46+
});
2847
});
48+
49+
startTime();
50+
}
51+
52+
void startTime() {
53+
const oneSec = const Duration(seconds: 1);
54+
55+
_timer = new Timer.periodic(
56+
oneSec,
57+
(Timer timer) => setState(
58+
() {
59+
if (remaining.inSeconds < 1) {
60+
timer.cancel();
61+
} else {
62+
final newtime = remaining.inSeconds - 1;
63+
setState(() {
64+
remaining = Duration(seconds: newtime);
65+
});
66+
print(remaining.toString());
67+
}
68+
},
69+
),
70+
);
71+
}
72+
73+
@override
74+
void dispose() {
75+
_timer.cancel();
76+
super.dispose();
2977
}
3078

3179
@override
@@ -48,7 +96,7 @@ class _ConnectedFormState extends State<ConnectedForm> {
4896
child: Column(
4997
children: <Widget>[
5098
Padding(
51-
padding: EdgeInsets.all(5.0),
99+
padding: EdgeInsets.all(4.0),
52100
child: Text(
53101
'Conectado',
54102
style: TextStyle(color: Colors.blue, fontSize: 20),
@@ -64,6 +112,13 @@ class _ConnectedFormState extends State<ConnectedForm> {
64112
),
65113
),
66114
),
115+
Padding(
116+
padding: EdgeInsets.all(5.0),
117+
child: Text(
118+
remaining == null ? '00:00:00' : time,
119+
style: TextStyle(color: Colors.red, fontSize: 20),
120+
),
121+
),
67122
Padding(
68123
padding: EdgeInsets.all(10.0),
69124
child: MaterialButton(
@@ -80,8 +135,16 @@ class _ConnectedFormState extends State<ConnectedForm> {
80135
try {
81136
await nautaClient.logout();
82137

83-
Navigator.pushReplacement(context,
84-
MaterialPageRoute(builder: (context) => LoginPage()));
138+
this.dispose();
139+
140+
Navigator.pushReplacement(
141+
context,
142+
MaterialPageRoute(
143+
builder: (context) => LoginPage(
144+
title: 'NAUTA',
145+
),
146+
),
147+
);
85148
} on NautaLogoutException catch (e) {
86149
await pr.hide();
87150
Scaffold.of(context).showSnackBar(SnackBar(

0 commit comments

Comments
 (0)