I basically have an edit name feature on my app, (for now hardcoded), that allows users to change their name. However the name is fetched before the page loads, so I do that in a future builder type widget. Unfortunatly the name changing itself works, but when I go back into the text field and attempt to change the name again, the name reverts back to its old self. here are some screenshots showing what I mean
Before I tried to change the name:
REVERTED BACK TO 'BOB'
Here is my code (THANK YOU SOOO MUCH! I'VE BEEN WORKING ON THIS FOR DAYS!!!!):
class ProfileInfo extends StatefulWidget {
String? name;
String? email;
ProfileInfo({this.name, this.email});
@override
_ProfileInfoState createState() => _ProfileInfoState();
}
class _ProfileInfoState extends State<ProfileInfo> {
var nameController = TextEditingController();
void updateName(String newName) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('name', newName);
setState(() {
nameController.text = newName;
widget.name = newName;
});
}
void _showEditNameDialog(BuildContext context) {
// Set the nameController.text to the current name
nameController.text = widget.name ?? '';
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Edit Name'),
content: TextField(
controller: nameController,
decoration: InputDecoration(hintText: 'Enter your name'),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Save the edited name if needed
updateName(nameController.text);
Navigator.of(context).pop();
},
child: Text('Save'),
),
],
);
},
);
}
void redirtohome() async {
var t = await getQuote();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
fullscreenDialog: true,
builder: (context) => home_page(quotes: t),
),
);
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Column(
children: [
SizedBox(
width: size.width * .9,
child: Row(
children: [
InkWell(
onTap: Navigator.of(context).pop,
child: Image.asset(
"assets/images/whitebackarrow.png",
width: size.width * .05,
),
),
SizedBox(width: 10),
Text(
'Edit Profile',
style: TextStyle(color: Colors.white, fontSize: 20),
),
Spacer(),
SizedBox(
width: size.width * .3,
height: size.height * .065,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
),
onPressed: () {
redirtohome();
},
child: Text(
'JustLift',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
)),
SizedBox(height: 10),
SizedBox(height: 10),
Container(
height: size.height * 0.3,
width: size.width * .9,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Icon(
Icons.circle,
color: Color(0xff11b779),
size: size.height * 0.3,
),
Text(
widget.name!.length >= 2
? widget.name!.substring(0, 2).toUpperCase()
: widget.name!,
style: TextStyle(
fontSize: size.height * 0.13,
color: Colors.white,
fontWeight: FontWeight.bold),
),
],
),
),
SizedBox(height: 10),
Container(
width: size.width * .9,
height: 30,
color: Colors.grey.withOpacity(.14),
padding: EdgeInsets.only(top: 4, left: 10),
child: Text(
'NAME',
style: TextStyle(
fontSize: 18,
color: Colors.grey.withOpacity(.8),
fontWeight: FontWeight.bold,
),
),
),
Container(
width: size.width * .9,
child: TextButton(
onPressed: () {
_showEditNameDialog(context);
},
child: Row(children: [
Text(
widget.name!,
style: const TextStyle(
color: Color(0xFFCECECE),
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Spacer(),
Icon(Icons.edit, color: Color(0xFFCECECE)),
]))),
Container(
width: size.width * .9,
height: 30,
color: Colors.grey.withOpacity(.14),
padding: EdgeInsets.only(top: 4, left: 10),
child: Text(
'EMAIL',
style: TextStyle(
fontSize: 18,
color: Colors.grey.withOpacity(.8),
fontWeight: FontWeight.bold,
),
),
),
Container(
width: size.width * .9,
padding: EdgeInsets.all(10),
child: Text(
widget.email!,
style: const TextStyle(
color: Color(0xFFCECECE),
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
)
],
);
}
}
class EditProfilePage extends StatefulWidget {
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
String? name = '';
String? email = '';
@override
void initState() {
super.initState();
getInfo();
}
Future<void> getInfo() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
name = prefs.getString('name');
email = prefs.getString('email');
});
}
@override
Widget build(BuildContext context) {
if (name == null || email == null) {
return Center(
child: SizedBox(
height: 50.0, // or another size
width: 50.0,
child: CircularProgressIndicator(),
),
);
} else {
return buildPage(context);
}
}
@override
Widget buildPage(BuildContext context) {
Size size = MediaQuery.of(context).size;
double height = MediaQuery.of(context).viewPadding.top;
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: AppColors('Default').main2,
body: Column(children: [
SizedBox(height: MediaQuery.of(context).padding.top + 10),
Expanded(
child: ProfileInfo(name: name, email: email),
),
Align(alignment: Alignment.bottomCenter, child: Footer(tab: 'More'))
]));
}
}
For now you can put widget.name to TextEditingController in initState()
var nameController = TextEditingController();
@override
void initState() {
super.initState();
nameController.text = widget.name??'';
}
Then use the nameController in Text widget.
Text(nameController.text,)
Use widget.name
to initialize nameController
then user nameController
to implement business logic.