-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
148 lines (138 loc) · 4.42 KB
/
main.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
import 'package:flutter/material.dart';
import 'package:slim/slim.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return [
Slimer<User>(User()),
Slimer<LoginService>(LoginService()),
Slimer<LoginController>(LoginController()),
].slim(
child: MaterialApp(
builder: SlimMaterialAppBuilder.builder,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Login(),
localizationsDelegates: SlimLocalizations.delegates,
supportedLocales: SlimLocalizations.supportedLocales
..addAll([Locale('en', 'US')]),
),
);
}
}
class User {
String userName;
String password;
}
class LoginService extends SlimApi {
LoginService() : super("http://myserver.com/api");
Future<SlimResponse> login(User user) =>
post("login", {"userName": user.userName, "password": user.password});
Future<SlimResponse> logout(User user) =>
post("logout", {"userName": user.userName});
}
class LoginController extends SlimController {
badLogin(User user) async {
final loginService = slim<LoginService>();
showWidget(CircularProgressIndicator(), dismissible: false);
final result = await loginService.login(user);
forceClearOverlay();
if (result.success)
Home().pushReplacement(context);
else
context.showSnackBar(
context.translate("badcreds"),
backgroundColor: Colors.red,
);
}
goodLogin(User user) async {
final loginService = slim<LoginService>();
showWidget(CircularProgressIndicator(), dismissible: false);
await loginService.login(user);
forceClearOverlay();
Home().pushReplacement(context);
}
}
class Login extends SlimWidget<LoginController> {
@override
Widget slimBuild(BuildContext context, LoginController controller) {
final user = context.slim<User>();
return Scaffold(
backgroundColor: Colors.blue,
body: Center(
child: Container(
width: context.width * 0.8,
child: Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
context.translate("loginform"),
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
TextFormField(
decoration: InputDecoration(
hintText: "Username",
alignLabelWithHint: true,
),
initialValue: user.userName,
onChanged: (value) => user.userName = value,
),
TextFormField(
decoration: InputDecoration(
hintText: "Password",
alignLabelWithHint: true,
),
initialValue: user.password,
onChanged: (value) => user.password = value,
obscureText: true,
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
child: Text(context.translate("badlogin")),
onPressed: () => controller.badLogin(user),
color: Colors.pink,
),
FlatButton(
child: Text(context.translate("goodlogin")),
onPressed: () => controller.goodLogin(user),
color: Colors.green,
)
],
),
],
),
),
),
),
),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SlimBuilder<User>(
builder: (user) => Scaffold(
backgroundColor: Colors.lightBlue,
body: Center(
child: Text("${context.translate("hi")} ${user.userName}"),
),
),
);
}
}