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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.white,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _todoController = TextEditingController();
final TextEditingController _usernameController = TextEditingController();
final List<String> _values = <String>['first todo', 'second work'];
final List<bool> _dones = <bool>[true, false];
var _username = "test";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Text App"),
backgroundColor: Color(0xff2d3f76),
),
body: Center(
child: Container(
padding: EdgeInsets.all(16.0),
child: Column(children: _showTodos()))),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
if (_todoController.text.isNotEmpty) {
_values.add(_todoController.text);
_dones.add(false);
_todoController.clear();
}
});
},
tooltip: 'Add Todo',
child: const Icon(Icons.add),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: IconButton(
onPressed: () => _showDialog(), icon: Icon(Icons.login)),
label: "Login"),
BottomNavigationBarItem(
icon: IconButton(
onPressed: () => _saveTodos(), icon: Icon(Icons.save)),
label: "Save"),
],
backgroundColor: Color(0xff222436),
));
}
List<Widget> _showTodos() {
List<Widget> todos = [];
for (int i = 0; i < _values.length; i++) {
String context = _values[i];
todos.add(Row(children: <Widget>[
Checkbox(
value: _dones[i],
onChanged: (bool? value) {
setState(() {
_dones[i] = value!;
});
}),
SizedBox(width: 10),
Text(context),
Spacer(),
IconButton(
onPressed: () {
setState(() {
int i = _values.indexOf(context);
_values.remove(context);
_dones.removeAt(i);
});
},
icon: Icon(
Icons.delete,
color: Colors.red,
),
),
]));
}
if (_values.isEmpty) {
todos.add(Text('Nothing here'));
}
todos.add(SizedBox(height: 10));
todos.add(TextField(
controller: _todoController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Insert Todo',
),
));
return todos;
}
Future<void> _showDialog() async {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Login'),
content: TextField(
controller: _usernameController,
decoration: const InputDecoration(hintText: 'Username'),
autofocus: true,
),
actions: <Widget>[
OutlinedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancel'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {
Navigator.of(context).pop();
_username = _usernameController.text;
_values.clear();
_dones.clear();
_loadTodos();
_usernameController.clear();
},
child: const Text('Login'),
),
],
);
});
}
void _loadTodos() async {
String uri = "http://127.0.0.1:6969/$_username.txt";
try {
http.Response response = await http.get(Uri.parse(uri));
if (response.statusCode == 200) {
print("Successfully got the data");
} else {
print('Error getting data: ${response.statusCode}');
return;
}
setState(() {
todoDecode(response.body, _values, _dones);
});
print(_values);
print(_dones);
} on SocketException catch (e) {
print(e.message);
}
}
void _saveTodos() async {
String uri = "http://127.0.0.1:6969/$_username.txt";
var data = todoEncode(_values, _dones);
print(data);
try {
http.Response response = await http.post(Uri.parse(uri), body: data);
if (response.statusCode == 200) {
print('Data sent successfully');
} else {
print('Error sending data: ${response.statusCode}');
}
} catch (e) {
print(e.toString());
}
}
}
String todoEncode(List<String> values, List<bool> dones) {
String data = "";
for (int i = 0; i < values.length; i++) {
data += '${dones[i] ? 1 : 0}:${values[i]}\n';
}
return data;
}
void todoDecode(String todos, List<String> values, List<bool> dones) {
var tmp = todos.split('\n');
for (int i = 0; i < tmp.length - 1; i++) {
values.add(tmp[i].substring(2));
dones.add(tmp[i][0] == '0' ? false : true);
}
}
|