-
Notifications
You must be signed in to change notification settings - Fork 0
/
flutter_bloc_screen.dart
116 lines (106 loc) · 3.39 KB
/
flutter_bloc_screen.dart
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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/todo_bloc.dart';
import '../bloc/todo_event.dart';
import '../bloc/todo_state.dart';
import '../repository/todo_repository.dart';
class FlutterBlocScreen extends StatefulWidget {
@override
_FlutterBlocScreenState createState() => _FlutterBlocScreenState();
}
class _FlutterBlocScreenState extends State<FlutterBlocScreen> {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => TodoBloc(repository: TodoRepository()),
child: TodoListWidget(),
);
}
}
class TodoListWidget extends StatefulWidget {
@override
_TodoListWidgetState createState() => _TodoListWidgetState();
}
class _TodoListWidgetState extends State<TodoListWidget> {
String title = '';
@override
void initState() {
super.initState();
BlocProvider.of<TodoBloc>(context).add(ListTodosEvent());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter BloC'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.read<TodoBloc>().add(CreateTodoEvent(title: title));
},
child: const Icon(
Icons.edit,
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: [
TextField(
onChanged: (val) {
title = val;
},
),
const SizedBox(height: 16.0),
Expanded(
child: BlocBuilder<TodoBloc, TodoState>(
builder: (_, state) {
if (state is Empty) {
return Container();
} else if (state is Error) {
return Container(
child: Text(state.message),
);
} else if (state is Loading) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (state is Loaded) {
final items = state.todos;
return ListView.separated(
itemBuilder: (_, index) {
final item = items[index];
return Row(
children: [
Expanded(
child: Text(
item.title,
),
),
GestureDetector(
onTap: () {
BlocProvider.of<TodoBloc>(context)
.add(DeleteTodoEvent(todo: item));
},
child: const Icon(
Icons.delete,
),
),
],
);
},
separatorBuilder: (_, index) => const Divider(),
itemCount: items.length,
);
}
return Container();
},
),
),
],
),
),
);
}
}