aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.dart
diff options
context:
space:
mode:
Diffstat (limited to 'main.dart')
-rw-r--r--main.dart220
1 files changed, 220 insertions, 0 deletions
diff --git a/main.dart b/main.dart
new file mode 100644
index 0000000..313f649
--- /dev/null
+++ b/main.dart
@@ -0,0 +1,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);
+ }
+}