-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.dart
107 lines (99 loc) · 2.92 KB
/
session.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
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'index.dart';
import 'dart:convert';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String? _loginError;
final TextEditingController _usernameController = TextEditingController(text: 'milkshake');
final TextEditingController _passwordController = TextEditingController();
Future<void> _login(BuildContext context) async {
final String username = _usernameController.text;
final String password = _passwordController.text;
final Uri loginUri = Uri.parse('http://localhost:3000/api/login');
int statusCode = 200;
String body = '{"id": 1, "name": "milkshake"}';
try {
final response = await http.post(
loginUri,
body: {
'username': username,
'password': password,
},
);
statusCode = response.statusCode;
body = response.body;
} catch (e) {
print('Error: ${e.toString()}');
}
if (statusCode == 200) {
final jsonResponse = json.decode(body);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(jsonResponse, context),
),
);
} else {
setState(() {
_loginError = 'Login failed with status code: ${statusCode}';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Breeze'),
),
backgroundColor: Color.fromARGB(255, 255, 255, 255),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_loginError != null)
Container(
padding: EdgeInsets.all(10),
color: Colors.red,
child: Text(
_loginError!,
style: TextStyle(color: Colors.white),
),
),
Image.asset(
'assets/paws.jpg',
width: 280,
height: 124,
),
SizedBox(height: 16),
TextField(
controller: _usernameController,
decoration: InputDecoration(
hintText: 'Email',
border: OutlineInputBorder(),
),
),
SizedBox(height: 16),
TextField(
controller: _passwordController,
decoration: InputDecoration(
hintText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () => _login(context),
child: Text('Login'),
),
],
),
),
);
}
}