個人資料加入username

This commit is contained in:
kuei 2024-09-13 01:47:10 +08:00
parent 114613ae0d
commit 31a12467ba

View File

@ -28,19 +28,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
@override @override
void initState() { void initState() {
//
super.initState(); super.initState();
print('狀態:$_isEditing');
_fetchData(); _fetchData();
} }
void _fetchData() async { void _fetchData() async {
//MYSQL print('傳遞過來的 email: ${widget.email}');
print('傳遞過來的 email: ${widget.email}'); // email
final conn = await MySQLConnection.createConnection( final conn = await MySQLConnection.createConnection(
host: '203.64.84.154', host: '203.64.84.154',
//127.0.0.1 10.0.2.2
port: 33061, port: 33061,
userName: 'root', userName: 'root',
password: 'Topic@2024', password: 'Topic@2024',
@ -50,18 +46,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
await conn.connect(); await conn.connect();
try { try {
print('ok');
print('狀態:$_isEditing');
var result = await conn.execute( var result = await conn.execute(
'SELECT * FROM HomeLogin WHERE homeemail = :email', 'SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // email {'email': widget.email},
); );
if (result.rows.isNotEmpty) { if (result.rows.isNotEmpty) {
//
var row = result.rows.first; var row = result.rows.first;
setState(() { setState(() {
_username = row.colAt(0) ?? ''; _username = row.colAt(0) ?? '';
_realname = row.colAt(1) ?? ''; // _realname = row.colAt(1) ?? '';
_phone = row.colAt(2) ?? ''; _phone = row.colAt(2) ?? '';
_gender = row.colAt(3) ?? ''; _gender = row.colAt(3) ?? '';
_address = row.colAt(4) ?? ''; _address = row.colAt(4) ?? '';
@ -79,7 +72,6 @@ class _PersonalInfoState extends State<PersonalInfo> {
void _toggleEdit() { void _toggleEdit() {
setState(() { setState(() {
if (!_isEditing) { if (!_isEditing) {
//
_realnameController.text = _realname; _realnameController.text = _realname;
_phoneController.text = _phone; _phoneController.text = _phone;
_genderController.text = _gender; _genderController.text = _gender;
@ -88,7 +80,6 @@ class _PersonalInfoState extends State<PersonalInfo> {
_passwordController.text = _password; _passwordController.text = _password;
} }
_isEditing = !_isEditing; _isEditing = !_isEditing;
print('編輯狀態:$_isEditing');
}); });
} }
@ -112,7 +103,7 @@ class _PersonalInfoState extends State<PersonalInfo> {
'homeGender': _genderController.text, 'homeGender': _genderController.text,
'homeAddress': _addressController.text, 'homeAddress': _addressController.text,
'new_email': _emailController.text, 'new_email': _emailController.text,
'old_email': widget.email, // email 'old_email': widget.email,
'homePassword': _passwordController.text, 'homePassword': _passwordController.text,
}, },
); );
@ -123,91 +114,227 @@ class _PersonalInfoState extends State<PersonalInfo> {
_address = _addressController.text; _address = _addressController.text;
_email = _emailController.text; _email = _emailController.text;
_password = _passwordController.text; _password = _passwordController.text;
_isEditing = false;
//
_realnameController.clear();
_phoneController.clear();
_genderController.clear();
_addressController.clear();
_emailController.clear();
_passwordController.clear();
_isEditing = false; // 退
print('儲存狀態:$_isEditing');
}); });
} catch (e) { } catch (e) {
print('Error: $e'); print('Error: $e');
} finally { } finally {
await conn.close(); await conn.close();
print('finally儲存更新狀態:$_isEditing');
} }
} }
void _loginOut() async { void _loginOut() async {
print("loyout");
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
// remove all data in share preference(user data which save to identify user or others)
prefs.clear(); prefs.clear();
// navbar router setting
// replace screen with LoginPage and without navbar
pushReplacementWithoutNavBar( pushReplacementWithoutNavBar(
context, context,
NoSwipeBackRoute( NoSwipeBackRoute(
builder: (context) => LoginPage(), builder: (context) => LoginPage(),
)); ),
);
} }
//
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
// appBar: AppBar(
// title: Text('個人資料'),
// backgroundColor: Color(0xFFF5E3C3),
// ),
body: Column( body: Column(
children: [ children: [
Container( Container(
height: 100, height: 120,
color: Color(0xFFF5E3C3), color: Color(0xFFF5E3C3),
// padding: EdgeInsets.only(top: 50, left: 20, right: 20),
width: double.infinity, child: Row(
padding: EdgeInsets.all(10.0), mainAxisAlignment: MainAxisAlignment.spaceBetween,
child: Center( children: [
child: Text( Text(
'個人資料', _username + ' 您好',
style: TextStyle(fontSize: 24, height: 5), style: TextStyle(fontSize: 28, color: Colors.black),
), ),
],
), ),
), ),
Expanded( Expanded(
child: ListView( child: ListView(
padding: EdgeInsets.symmetric(vertical: 5), // padding padding: EdgeInsets.symmetric(vertical: 5),
children: [ children: [
ListTile( ListTile(
title: Text( leading: Icon(Icons.manage_accounts_outlined),
'使用名稱', title: Text('基本資料', style: TextStyle(fontSize: 18)),
style: TextStyle(fontSize: 20), onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BasicInfoPage(
realname: _realname,
phone: _phone,
gender: _gender,
address: _address,
email: _email,
isEditing: _isEditing,
), ),
subtitle: Text(_username), ),
);
},
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text( leading: Icon(Icons.lock_open_outlined),
'姓名', title: Text('帳號設定', style: TextStyle(fontSize: 18)),
style: TextStyle(fontSize: 20), onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AccountPage(
email: _email,
password: _password,
isEditing: _isEditing,
), ),
),
);
},
),
Divider(),
ListTile(
leading: Icon(Icons.language_outlined),
title: Text('語言設定', style: TextStyle(fontSize: 18)),
onTap: () {},
),
Divider(),
Divider(),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges();
} else {
_toggleEdit();
}
},
child: Text(_isEditing ? '儲存變更' : '修改資料'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
ElevatedButton(
onPressed: _loginOut,
child: Text('登出'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
],
),
),
],
),
);
}
}
// BasicInfoPage
class BasicInfoPage extends StatefulWidget {
String realname;
String phone;
String gender;
String address;
String email;
bool isEditing;
BasicInfoPage({
required this.realname,
required this.phone,
required this.gender,
required this.address,
required this.email,
required this.isEditing,
});
@override
_BasicInfoPageState createState() => _BasicInfoPageState();
}
class _BasicInfoPageState extends State<BasicInfoPage> {
late TextEditingController _realnameController;
late TextEditingController _phoneController;
late TextEditingController _genderController;
late TextEditingController _addressController;
bool _isEditing = false;
@override
void initState() {
super.initState();
_realnameController = TextEditingController(text: widget.realname);
_phoneController = TextEditingController(text: widget.phone);
_genderController = TextEditingController(text: widget.gender);
_addressController = TextEditingController(text: widget.address);
_isEditing = widget.isEditing;
}
void _toggleEdit() {
setState(() {
_isEditing = !_isEditing;
});
}
void _saveChanges() async {
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
port: 33061,
userName: 'root',
password: 'Topic@2024',
databaseName: 'care',
);
await conn.connect();
try {
await conn.execute(
'UPDATE HomeLogin SET homeRealName = :homeRealName, homePhone = :homePhone, homeGender = :homeGender, homeAddress = :homeAddress WHERE homeEmail = :old_email',
{
'homeRealName': _realnameController.text,
'homePhone': _phoneController.text,
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'old_email': widget.email,
},
);
setState(() {
widget.realname = _realnameController.text;
widget.phone = _phoneController.text;
widget.gender = _genderController.text;
widget.address = _addressController.text;
_isEditing = false;
});
print('successssssssssssssssss');
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('基本資料'),
backgroundColor: Color(0xFFE0D0A9),
),
body: ListView(
padding: EdgeInsets.all(16),
children: [
ListTile(
title: Text('姓名'),
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _realnameController, controller: _realnameController,
onChanged: (value) {
setState(() {
_realname = value;
});
},
) )
: Text(_realname), : Text(widget.realname),
), ),
Divider(), Divider(),
ListTile( ListTile(
@ -215,13 +342,8 @@ class _PersonalInfoState extends State<PersonalInfo> {
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _phoneController, controller: _phoneController,
onChanged: (value) {
setState(() {
_phone = value;
});
},
) )
: Text(_phone), : Text(widget.phone),
), ),
Divider(), Divider(),
ListTile( ListTile(
@ -229,13 +351,8 @@ class _PersonalInfoState extends State<PersonalInfo> {
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _genderController, controller: _genderController,
onChanged: (value) {
setState(() {
_gender = value;
});
},
) )
: Text(_gender), : Text(widget.gender),
), ),
Divider(), Divider(),
ListTile( ListTile(
@ -243,27 +360,123 @@ class _PersonalInfoState extends State<PersonalInfo> {
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _addressController, controller: _addressController,
onChanged: (value) {
setState(() {
_address = value;
});
},
) )
: Text(_address), : Text(widget.address),
), ),
Divider(), SizedBox(height: 20),
ListTile( ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges();
} else {
_toggleEdit();
}
},
child: Text(_isEditing ? '儲存變更' : '修改資料'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
],
),
);
}
}
// BasicInfoPage
class AccountPage extends StatefulWidget {
String email;
String password;
bool isEditing;
AccountPage({
required this.email,
required this.password,
required this.isEditing,
});
@override
_AccountPageState createState() => _AccountPageState();
}
class _AccountPageState extends State<AccountPage> {
late TextEditingController _emailController;
late TextEditingController _passwordController;
bool _isEditing = false;
bool _passwordNotVisible = true; //
@override
void initState() {
super.initState();
_emailController = TextEditingController(text: widget.email);
_passwordController = TextEditingController(text: widget.password);
_isEditing = widget.isEditing;
}
void _toggleEdit() {
setState(() {
_isEditing = !_isEditing;
});
}
void _saveChanges() async {
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
port: 33061,
userName: 'root',
password: 'Topic@2024',
databaseName: 'care',
);
await conn.connect();
print('connectttttttttttttttttttttttttt');
try {
await conn.execute(
'UPDATE HomeLogin SET homeEmail = :homeEmail,homePassword= :homePassword WHERE homeEmail = :old_email',
{
'homeEmail': _emailController.text,
'homePassword': _passwordController.text,
'old_email': widget.email,
},
);
setState(() {
widget.email = _emailController.text;
widget.password = _passwordController.text;
_isEditing = false;
});
print('資料更新成功');
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('帳號設定'),
backgroundColor: Color(0xFFE0D0A9),
),
body: ListView(
padding: EdgeInsets.all(16),
children: [ListTile(
title: Text('電子信箱'), title: Text('電子信箱'),
subtitle: _isEditing subtitle: _isEditing
? TextField( ? TextField(
controller: _emailController, controller: _emailController,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_email = value; widget.email = value;
}); });
}, },
) )
: Text(_email), : Text(widget.email),
), ),
Divider(), Divider(),
ListTile( ListTile(
@ -274,66 +487,39 @@ class _PersonalInfoState extends State<PersonalInfo> {
obscureText: _passwordNotVisible, obscureText: _passwordNotVisible,
decoration: InputDecoration( decoration: InputDecoration(
suffixIcon: IconButton( suffixIcon: IconButton(
icon: Icon(_passwordNotVisible icon: Icon(_passwordNotVisible ? Icons.visibility : Icons.visibility_off),
? Icons.visibility
: Icons.visibility_off),
onPressed: () { onPressed: () {
setState( setState(() {
() {
_passwordNotVisible = !_passwordNotVisible; _passwordNotVisible = !_passwordNotVisible;
});
}, },
); ),
}, ),
)),
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_password = value; widget.password = value;
}); });
}, },
) )
: Text(_password), : Text(widget.password),
),
Divider(),
SizedBox(
height: 20,
width: 60,
), ),
SizedBox(height: 20),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
if (_isEditing) { if (_isEditing) {
_saveChanges(); // _saveChanges();
} else { }
_toggleEdit(); // else {
_toggleEdit();
} }
}, },
child: Text(_isEditing ? '儲存變更' : '修改資料'), child: Text(_isEditing ? '儲存變更' : '修改資料'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3), //
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, //
),
),
SizedBox(
height: 10,
width: 60,
),
ElevatedButton(
onPressed: () {
_loginOut();
},
child: Text('登出'),
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(
height: 50, //
),
],
),
),
], ],
), ),
); );