We Will Create A Blank Screen Called UserProfile
import 'package:flutter/cupertino.dart';
class UserProfile extends StatefulWidget {
const UserProfile({super.key});
@override
State<UserProfile> createState() => _UserProfileState();
}
class _UserProfileState extends State<UserProfile> {
@override
Widget build(BuildContext context) {
return const Placeholder();
}
}
We will go to Home Screen And Create A Navigation Link From the Drawer
Container(
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => UserProfile()));
},
child: Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.only(left: 20, top: 30),
child: Text(
'> My profile',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
Now we will go to the Register Screen and copy the necessary codes & Paste it in Our Profile Screen
//For the UI
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: 200,),
Container(
alignment: Alignment.center,
child: Text('Register For Free', style: TextStyle(fontSize: 25),),
),
SizedBox(height: 20,),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _userNameController,
decoration: InputDecoration(
labelText: 'Username',
border: OutlineInputBorder(),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _userEmailController,
decoration: InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _userMobileController,
decoration: InputDecoration(
labelText: 'Mobile',
border: OutlineInputBorder(),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _userPasswordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _registerUser,
child: Text('Register'),
),
],
),
),
);
//Create the controllers and functions
TextEditingController _userNameController = TextEditingController();
TextEditingController _userEmailController = TextEditingController();
TextEditingController _userMobileController = TextEditingController();
TextEditingController _userPasswordController = TextEditingController();
Future<void> _registerUser() async {
String apiUrl = 'https://aboutassam.in/flutter_app/api/register.php';
try {
var response = await http.post(Uri.parse(apiUrl), body: {
'user_name': _userNameController.text,
'user_email': _userEmailController.text,
'user_mobile': _userMobileController.text,
'user_password': _userPasswordController.text,
});
var responseData = json.decode(response.body);
if (responseData['success']) {
// Registration successful, handle navigation or any other action
print('Registration successful');
} else {
// Registration failed, show error message
print('Registration failed: ${responseData['message']}');
}
} catch (e) {
print('Error registering user: $e');
}
}
We will next go to the Checkout Screen, copy relevant codes, and paste them in our User Profile Screen
@override
void initState() {
super.initState();
_getUserInfo();
}
Future<void> _getUserInfo() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String usermobile = prefs.getString('userMobile') ?? ''; // Get user email
if (usermobile.isEmpty) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(),
),
);
} else {
setState(() {
userMobile = usermobile;
});
}
;
} catch (error) {
print('Error retrieving user info: $error');
}
}
//We will slightly modify the codes to the following
Future<void> _getUserInfo() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userMobile = prefs.getString('userMobile') ?? ''; // Get user mobile
setState(() {
_userMobileController.text = userMobile;
});
} catch (error) {
print('Error retrieving user info: $error');
}
}
Post Views: 102