-
Notifications
You must be signed in to change notification settings - Fork 1
/
one_to_one.dart
169 lines (151 loc) · 4.23 KB
/
one_to_one.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
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
166
167
168
169
import 'dart:math';
import 'package:example/eloquents/car.dart';
import 'package:example/eloquents/user.dart';
import 'package:example/models/car.dart';
import 'package:example/models/user.dart';
import 'package:flutter/material.dart';
class OneToOneWidget extends StatefulWidget {
const OneToOneWidget({Key? key}) : super(key: key);
@override
State<OneToOneWidget> createState() => _OneToOneWidgetState();
}
const carNames = [
'Lamborghini Diablo',
'Ford Raptor',
'Ferrari Testarossa',
'Porsche 911 Carrera',
'Jeep Gladiator'
];
class _OneToOneWidgetState extends State<OneToOneWidget> {
final CarEloquent carEloquent = CarEloquent();
final UserEloquent userEloquent = UserEloquent();
List<User> users = [];
List<Car> cars = [];
@override
void initState() {
loadUsers();
loadCars();
super.initState();
}
showSnack(String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
duration: const Duration(milliseconds: 600),
));
}
loadCars() async {
var data = await carEloquent.all();
updateCarState(data);
}
loadUsers() async {
var data = await userEloquent.all();
updateUserState(data);
}
updateUserState(List<Map<String, Object?>>? data) async {
if (data == null) {
showSnack('Empty');
}
users = [];
for (var user in data!) {
users.add(await User.withCar(user));
}
setState(() {
users = users;
});
}
updateCarState(List<Map<String, Object?>>? data) async {
if (data == null) {
showSnack('Empty');
}
cars = [];
for (var car in data!) {
cars.add(await Car.withUser(car));
}
setState(() {
cars = cars;
});
}
update() async {
if (cars.isEmpty) {
showSnack('Empty car');
return;
}
User user = users[Random().nextInt(users.length)];
var data = await (await user.getCar()).update({'name': 'Audi R8'});
showSnack('Updated - $data');
loadCars();
}
create() async {
if (users.isEmpty) {
showSnack('Empty User');
return;
}
var data =
await (await users[Random().nextInt(users.length)].getCar()).create({
'name': carNames[Random().nextInt(carNames.length)],
'createdAt': DateTime.now().toIso8601String(),
'updatedAt': DateTime.now().toIso8601String()
});
showSnack('Created id - $data');
loadCars();
}
delete() async {
if (cars.isEmpty) {
showSnack('Empty car');
return;
}
User user = users[Random().nextInt(users.length)];
var data = await (await user.getCar()).delete();
showSnack('Delete rows - $data where user name : ${user.name}');
loadCars();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return SingleChildScrollView(
controller: ScrollController(),
child: SizedBox(
width: size.width * 0.95,
height: size.height,
child: Column(children: [
const SizedBox(height: 20),
SizedBox(
height: size.height * 0.7,
width: size.width * 0.7,
child: ListView.builder(
controller: ScrollController(),
itemCount: cars.length,
itemBuilder: (_, int index) {
Car car = cars[index];
return ListTile(
leading: Text(car.id.toString()),
title: Text(car.name),
subtitle: Text(car.user!.name),
);
})),
SizedBox(
width: size.width * 0.7,
child: Wrap(
spacing: 8,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: create, child: const Text('Create Car')),
ElevatedButton(
onPressed: delete, child: const Text('Delete Car')),
ElevatedButton(
onPressed: update, child: const Text('Update Car')),
ElevatedButton(
onPressed: loadUsers, child: const Text('Reload')),
],
),
)
]),
),
);
}
}