-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.dart
More file actions
119 lines (106 loc) · 3.77 KB
/
Solution.dart
File metadata and controls
119 lines (106 loc) · 3.77 KB
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
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'package:nanoid/nanoid.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../classes/global.dart';
class Profile extends StatefulWidget {
final bool onLogin;
const Profile({Key? key, required this.onLogin}) : super(key: key);
@override
State<Profile> createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
final TextEditingController myName = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool loading = true;
String customLengthId = nanoid(6);
@override
void initState() {
super.initState();
getDetails();
}
@override
void dispose() {
myName.dispose();
super.dispose();
}
Future<void> getDetails() async {
final prefs = await SharedPreferences.getInstance();
final name = prefs.getString('p_name') ?? '';
final id = prefs.getString('p_id') ?? '';
setState(() {
myName.text = name;
if (id.isNotEmpty) customLengthId = id;
loading = false;
});
// If profile already exists and we arrived here from login flow, go home
if (name.isNotEmpty && id.isNotEmpty && widget.onLogin) {
navigateToHomeScreen();
}
}
void navigateToHomeScreen() {
Global.myName = myName.text;
if (!widget.onLogin) {
Navigator.pop(context);
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: loading
? const Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// show what the final username will look like
Text('Your username will be: ${myName.text}$customLengthId'),
const SizedBox(height: 16),
Form(
key: _formKey,
child: TextFormField(
controller: myName,
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name *',
border: OutlineInputBorder(),
),
validator: (value) {
final v = value?.trim() ?? '';
if (v.isEmpty) return 'Please enter a name';
if (v.contains('@')) return 'Do not use the @ character';
if (v.length <= 3) return 'Name should be greater than 3 characters';
return null;
},
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
if (!(_formKey.currentState?.validate() ?? false)) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please fix the errors')),
);
return;
}
final prefs = await SharedPreferences.getInstance();
await prefs.setString('p_name', myName.text.trim());
await prefs.setString('p_id', customLengthId);
navigateToHomeScreen();
},
child: const Text('Save'),
),
],
),
),
);
}
}