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 createState() => _MyHomePageState(); } class _MyHomePageState extends State { final TextEditingController _todoController = TextEditingController(); final TextEditingController _usernameController = TextEditingController(); final List _values = ['first todo', 'second work']; final List _dones = [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 _showTodos() { List todos = []; for (int i = 0; i < _values.length; i++) { String context = _values[i]; todos.add(Row(children: [ 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 _showDialog() async { return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('Login'), content: TextField( controller: _usernameController, decoration: const InputDecoration(hintText: 'Username'), autofocus: true, ), actions: [ 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 values, List 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 values, List 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); } }