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
| import 'package:flutter/material.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'todo list',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.orange,
),
),
debugShowCheckedModeBanner: false,
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Todo List'),
backgroundColor: Theme.of(context).colorScheme.primary,
titleTextStyle: TextStyle(
color: Theme.of(context).colorScheme.onPrimary,
fontSize: 30,
),
),
body: TodoList(),
);
}
}
class TodoList extends StatefulWidget {
const TodoList({super.key});
@override
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
List<String> itemList = [];
int _editIndex = -1; // -1 means add new item. other number is editing.
final TextEditingController _controller = TextEditingController();
@override
void initState() {
super.initState();
}
void _addTodo() {
if (_controller.text.isNotEmpty) {
setState(() {
itemList.add(_controller.text);
_controller.clear();
});
}
}
void _editTodoItem() {
if (_controller.text.isNotEmpty) {
setState(() {
itemList[_editIndex] = _controller.text;
_editIndex = -1;
_controller.clear();
});
} else {
_removeTodo(index: _editIndex);
}
}
void _removeTodo({required int index}) {
setState(() {
itemList.removeAt(index);
_editIndex = -1;
});
}
void _editTodo({required int index}) {
setState(() {
_controller.text = itemList[index];
_editIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
flex: 90,
child: ListView.builder(
itemCount: itemList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(itemList[index]),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {
_editTodo(index: index);
},
icon: Icon(Icons.edit),
),
IconButton(
onPressed: () {
_removeTodo(index: index);
},
icon: Icon(Icons.delete),
),
],
),
);
},
),
),
Expanded(
flex: 10,
child: Padding(
padding: const EdgeInsets.only(
left: 10,
right: 10,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextFormField(
controller: _controller,
autocorrect: false,
decoration: InputDecoration(
labelText: _editIndex == -1 ? "New todo" : "Edit todo",
),
onFieldSubmitted: (value) {
_editIndex == -1 ? _addTodo() : _editTodoItem();
},
),
),
SizedBox(
width: 10,
),
IconButton(
onPressed: _editIndex == -1 ? _addTodo : _editTodoItem,
icon: _editIndex == -1 ? Icon(Icons.add) : Icon(Icons.edit),
),
],
),
),
),
],
);
}
}
|