Skip to content

Commit 095da61

Browse files
Add TextField to Todo-App
1 parent 387cffd commit 095da61

File tree

2 files changed

+94
-14
lines changed

2 files changed

+94
-14
lines changed

lib/TextField.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:flutter/material.dart';
2+
3+
void main() => runApp(MaterialApp(home: UserInput()));
4+
5+
class UserInput extends StatefulWidget {
6+
@override
7+
_UserInputState createState() => _UserInputState();
8+
}
9+
10+
class _UserInputState extends State<UserInput> {
11+
void updateUserText(String text) {
12+
setState(() {
13+
userText = text;
14+
});
15+
}
16+
17+
String userText = '';
18+
19+
@override
20+
Widget build(BuildContext context) {
21+
return Column(
22+
children: <Widget>[
23+
TextField(
24+
onChanged: updateUserText,
25+
),
26+
Text(userText),
27+
],
28+
29+
);
30+
}
31+
}

lib/main.dart

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,81 @@
11
import 'package:flutter/material.dart';
22

3-
void main() => runApp(MaterialApp(home: UserInput()));
3+
void main() => runApp(MaterialApp(home: ToDo()));
44

5-
class UserInput extends StatefulWidget {
5+
////////////////////////////////
6+
class ToDo extends StatefulWidget {
67
@override
7-
_UserInputState createState() => _UserInputState();
8+
_ToDoState createState() => _ToDoState();
89
}
910

10-
class _UserInputState extends State<UserInput> {
11-
void updateUserText(String text) {
11+
class _ToDoState extends State<ToDo> {
12+
List<String> products = ['Tomate', 'Käse', 'Lauch', 'Paprika', 'Wein'];
13+
14+
void addItem(String item) {
1215
setState(() {
13-
userText = text;
16+
products.add(item);
1417
});
18+
Navigator.of(context).pop();
1519
}
1620

17-
String userText = '';
21+
void newEntry(){
22+
showDialog<AlertDialog>(
23+
context: context,
24+
builder: (BuildContext context){
25+
return AlertDialog(
26+
content: TextField(
27+
onSubmitted: addItem,
28+
)
29+
);
30+
}
31+
);
32+
}
1833

1934
@override
2035
Widget build(BuildContext context) {
21-
return Column(
22-
children: <Widget>[
23-
TextField(
24-
onChanged: updateUserText,
36+
return Scaffold(
37+
appBar: AppBar(
38+
title: Text('To-Do-App'),
39+
backgroundColor: Color.fromRGBO(35, 0, 0, 100),
2540
),
26-
Text(userText),
27-
],
28-
41+
body: ListView.builder(
42+
itemCount: products.length,
43+
itemBuilder: (context, i) {
44+
return ToDoItem(products[i]);
45+
},
46+
),
47+
floatingActionButton: FloatingActionButton(
48+
onPressed: newEntry,
49+
child: Icon(Icons.add),
50+
)
2951
);
3052
}
3153
}
3254

55+
class ToDoItem extends StatelessWidget {
56+
final String title;
57+
58+
const ToDoItem(this.title);
59+
60+
@override
61+
Widget build(BuildContext context) {
62+
return Container(
63+
color: Colors.white,
64+
padding: EdgeInsets.symmetric(horizontal: 22),
65+
child: ListTile(
66+
contentPadding: EdgeInsets.symmetric(vertical: 8),
67+
leading: Checkbox(
68+
value: false,
69+
),
70+
title: Text(
71+
title,
72+
style: TextStyle(
73+
fontSize: 18.0,
74+
fontWeight: FontWeight.w600,
75+
color: Colors.black54),
76+
),
77+
trailing: Icon(Icons.delete_outline),
78+
),
79+
);
80+
}
81+
}

0 commit comments

Comments
 (0)