編輯個人資料功能

This commit is contained in:
kuei
2024-09-07 18:31:07 +08:00
parent d2a2030979
commit edd2f57800

View File

@@ -20,13 +20,18 @@ class PersonalInfo extends StatefulWidget {
class _PersonalInfoState extends State<PersonalInfo> { class _PersonalInfoState extends State<PersonalInfo> {
String _name = '', _phone = '', _gender = '', _address='', _email = '', _password = ''; String _name = '', _phone = '', _gender = '', _address='', _email = '', _password = '';
bool _isEditing = false; //是否為編輯狀態 bool _isEditing = false; //是否為編輯狀態
final TextEditingController _emailController = final TextEditingController _nameController = TextEditingController();
TextEditingController(); //email輸入text final TextEditingController _phoneController = TextEditingController();
final TextEditingController _genderController = TextEditingController();
final TextEditingController _addressController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override @override
void initState() { void initState() {
//初始化 //初始化
super.initState(); super.initState();
print('狀態:$_isEditing');
_fetchData(); _fetchData();
} }
@@ -36,23 +41,20 @@ class _PersonalInfoState extends State<PersonalInfo> {
final conn = await MySQLConnection.createConnection( final conn = await MySQLConnection.createConnection(
host: '203.64.84.154', host: '203.64.84.154',
//host: '10.0.2.2',
//127.0.0.1 10.0.2.2 //127.0.0.1 10.0.2.2
port: 33061, port: 33061,
userName: 'root', userName: 'root',
password: 'Topic@2024', password: 'Topic@2024',
//password: '0000', databaseName: 'care',
databaseName: 'care', //testdb
//databaseName: 'testdb',
); );
await conn.connect(); await conn.connect();
try { try {
print('ok'); print('ok');
var result = await conn.execute( print('狀態:$_isEditing');
'SELECT * FROM HomeLogin WHERE homeemail = :email', var result = await conn.execute('SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // 傳入參數 email {'email': widget.email}, // 傳入參數 email
); );
if (result.rows.isNotEmpty) { if (result.rows.isNotEmpty) {
//有資料 //有資料
@@ -75,7 +77,17 @@ class _PersonalInfoState extends State<PersonalInfo> {
void _toggleEdit() { void _toggleEdit() {
setState(() { setState(() {
if (!_isEditing) {
// 當進入編輯模式時,將當前的資料賦予控制器
_nameController.text = _name;
_phoneController.text = _phone;
_genderController.text = _gender;
_addressController.text = _address;
_emailController.text = _email;
_passwordController.text = _password;
}
_isEditing = !_isEditing; _isEditing = !_isEditing;
print('編輯狀態:$_isEditing');
}); });
} }
@@ -92,17 +104,42 @@ class _PersonalInfoState extends State<PersonalInfo> {
try { try {
await conn.execute( await conn.execute(
'UPDATE HomeLogin SET homeemail = :email WHERE homeemail = :old_email', 'UPDATE HomeLogin SET homeName = :homeName, homePhone = :homePhone, homeGender = :homeGender, homeAddress = :homeAddress, homeEmail = :new_email, homePassword = :homePassword WHERE homeEmail = :old_email',
{'email': _emailController.text, 'old_email': widget.email}, {
'homeName':_nameController.text,
'homePhone': _phoneController.text,
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'new_email': _emailController.text,
'old_email': widget.email, // 用戶的舊 email 作為查找條件
'homePassword': _passwordController.text,
},
); );
setState(() { setState(() {
_name = _nameController.text;
_phone = _phoneController.text;
_gender = _genderController.text;
_address = _addressController.text;
_email = _emailController.text; _email = _emailController.text;
_password = _passwordController.text;
// 清空控制器內容
_nameController.clear();
_phoneController.clear();
_genderController.clear();
_addressController.clear();
_emailController.clear();
_passwordController.clear();
_isEditing = false; // 退出編輯 _isEditing = false; // 退出編輯
print('儲存狀態:$_isEditing');
}); });
} catch (e) { } catch (e) {
print('Error: $e'); print('Error: $e');
} finally { } finally {
await conn.close(); await conn.close();
await conn.close();
print('finally儲存更新狀態:$_isEditing');
} }
} }
@@ -120,6 +157,8 @@ class _PersonalInfoState extends State<PersonalInfo> {
)); ));
} }
//頁面
//頁面 //頁面
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -132,7 +171,7 @@ class _PersonalInfoState extends State<PersonalInfo> {
children: [ children: [
Container( Container(
height: 100, height: 100,
color: Color(0xFFF5E3C3), //背景底色 color: Color(0xFFF5E3C3),//背景底色
width: double.infinity, width: double.infinity,
padding: EdgeInsets.all(10.0), padding: EdgeInsets.all(10.0),
child: Center( child: Center(
@@ -148,82 +187,97 @@ class _PersonalInfoState extends State<PersonalInfo> {
children: [ children: [
ListTile( ListTile(
title: Text( title: Text('姓名',style: TextStyle(fontSize: 20),),
'姓名',
style: TextStyle(fontSize: 20),
),
subtitle: Text(_name), subtitle: Text(_name),
//trailing: Icon(Icons.arrow_forward_ios),//>
onTap: () {
//點了之後
},
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('手機'), title: Text('手機'),
subtitle: Text(_phone), subtitle: _isEditing
//trailing: Icon(Icons.arrow_forward_ios), ? TextField(
onTap: () {}, controller: _phoneController,
onChanged: (value) {
setState(() {
_phone = value;
});
},
)
: Text(_phone),
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('性別'), title: Text('性別'),
subtitle: Text(_gender), subtitle: _isEditing
//trailing: Icon(Icons.arrow_forward_ios), ? TextField(
onTap: () {}, controller: _genderController,
onChanged: (value) {
setState(() {
_gender = value;
});
},
)
: Text(_gender),
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('地址'), title: Text('地址'),
subtitle: Text(_address), subtitle: _isEditing
//trailing: Icon(Icons.arrow_forward_ios), ? TextField(
onTap: () {}, controller: _addressController,
onChanged: (value) {
setState(() {
_address = value;
});
},
)
: Text(_address),
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('電子信箱'), title: Text('電子信箱'),
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _emailController, controller: _emailController,
autofocus: true, onChanged: (value) {
onChanged: (value) { setState(() {
setState(() { _email = value;
_email = value; });
}); },
}, )
)
: Text(_email), : Text(_email),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
_toggleEdit(); // 切換到編輯狀態
},
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('密碼'), title: Text('密碼'),
subtitle: Text(_password), subtitle: _isEditing
//trailing: Icon(Icons.arrow_forward_ios), ? TextField(
onTap: () { controller: _passwordController,
}, onChanged: (value) {
setState(() {
_password = value;
});
},
)
: Text(_password),
), ),
Divider(), Divider(),
SizedBox( SizedBox(height: 20,width: 60,),
height: 20,
width: 60,
),
ElevatedButton( ElevatedButton(
onPressed: () {}, onPressed: () {
child: Text('修改資料'), if (_isEditing) {
_saveChanges(); // 儲存變更
} else {
_toggleEdit(); // 進入編輯模式
}
},
child: Text(_isEditing ? '儲存變更' : '修改資料'),
style: TextButton.styleFrom( style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3), // 無背景颜色 backgroundColor: Color(0xFFF5E3C3), // 無背景颜色
textStyle: TextStyle(fontSize: 18), textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, // 去除陰影 shadowColor: Colors.transparent, // 去除陰影
), ),
), ),
SizedBox( SizedBox(height: 10,width: 60,),
height: 10,
width: 60,
),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
_loginOut(); _loginOut();