/
main.dart
163 lines (151 loc) · 4.41 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'package:flutter/material.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "μPython on/off",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'μPython on/off'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _minutes = 60;
double _seconds = 60;
void _onOff() async {
try {
await http.get("http://10.0.1.60:8000/onoff");
} catch (e) {}
;
}
void _decrementTimer() {
setState(() {
_seconds -= 1;
if (_seconds == 0) {
_seconds = 60;
_minutes -= 1;
}
});
}
void _fromSlider(double v) {
setState(() {
_minutes = (v ~/ 5) * 5.0;
_seconds = 60;
_timer?.cancel();
});
}
Timer _timer;
void _startTimer() async {
_timer?.cancel();
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(() {
if (_minutes < 1) {
timer.cancel();
} else {
_decrementTimer();
}
}));
try {
await http
.get("http://10.0.1.60:8000/sleep=" + _minutes.toInt().toString());
} catch (e) {}
;
}
void _check_delta(d) {
setState(() {
_timer?.cancel();
if (d.delta.dy > 1.5 || d.delta.dx < -1.5) _minutes -= 5;
if (d.delta.dy < -1.5 || d.delta.dx > 1.5) _minutes += 5;
if (_minutes < 0) _minutes = 0;
if (_minutes > 180) _minutes = 180;
_minutes = (_minutes ~/ 5) * 5.0;
_seconds = 60;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onPanUpdate: _check_delta,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
CircularPercentIndicator(
radius: 120,
lineWidth: 13,
animation: true,
percent: _minutes / 180.0,
progressColor: Colors.pink[300],
backgroundColor: Colors.lightBlue[500],
),
CircularPercentIndicator(
radius: 150,
lineWidth: 13,
animation: true,
percent: _seconds / 60,
progressColor: Colors.grey[500],
backgroundColor: Colors.yellow[500],
),
FloatingActionButton(
backgroundColor: Colors.lightGreen[400],
),
Text(
_minutes.toInt().toString(),
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
Padding(padding: EdgeInsets.all(10)),
Slider(
value: _minutes,
min: 0,
max: 180.0,
onChanged: _fromSlider,
),
Padding(padding: EdgeInsets.all(30)),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: _startTimer,
tooltip: 'Timer',
child: Icon(Icons.timer),
),
Padding(
padding: EdgeInsets.all(15),
),
FloatingActionButton(
onPressed: _onOff,
tooltip: 'On/Off',
child: Icon(Icons.power_settings_new),
backgroundColor: Colors.red,
),
],
),
));
}
}