-
-
Notifications
You must be signed in to change notification settings - Fork 54
fix(profile): improve validation and fix padding error #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,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; | ||||||||||||||||||||||
|
Comment on lines
+88
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation off‑by‑one vs PR objective (“minimum of 3 characters”) Current rule enforces 4+. Adjust to 3+ and fix the message. - if (v.length <= 3) return 'Name should be greater than 3 characters';
+ if (v.length < 3) return 'Name must be at least 3 characters';📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| 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'), | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| ], | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix: avoid setState/navigation after dispose; schedule post‑frame navigation
getDetailsawaits then callssetStateand may navigate immediately. If the widget unmounts meanwhile, this will throw. Addmountedchecks and schedule navigation withaddPostFrameCallback.Future<void> getDetails() async { - final prefs = await SharedPreferences.getInstance(); - final name = prefs.getString('p_name') ?? ''; - final id = prefs.getString('p_id') ?? ''; + final prefs = await SharedPreferences.getInstance(); + if (!mounted) return; + final name = prefs.getString('p_name') ?? ''; + final id = prefs.getString('p_id') ?? ''; - setState(() { - myName.text = name; - if (id.isNotEmpty) customLengthId = id; - loading = false; - }); + if (!mounted) return; + 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(); - } + if (name.isNotEmpty && id.isNotEmpty && widget.onLogin) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) return; + navigateToHomeScreen(); + }); + } }📝 Committable suggestion
🤖 Prompt for AI Agents