From d4d9962016b2d6c3b533546d4261066ec796e95f Mon Sep 17 00:00:00 2001 From: Aman kumar vishwakarma <186990161+amankv1234@users.noreply.github.com> Date: Tue, 23 Sep 2025 00:24:44 +0530 Subject: [PATCH] Create solution --- solution | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 solution diff --git a/solution b/solution new file mode 100644 index 0000000..cdea1c7 --- /dev/null +++ b/solution @@ -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 createState() => _ProfileState(); +} + +class _ProfileState extends State { + final TextEditingController myName = TextEditingController(); + final _formKey = GlobalKey(); + + bool loading = true; + String customLengthId = nanoid(6); + + @override + void initState() { + super.initState(); + getDetails(); + } + + @override + void dispose() { + myName.dispose(); + super.dispose(); + } + + Future 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'), + ), + ], + ), + ), + ); + } +}