個人資料加入username

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

View File

@@ -14,7 +14,7 @@ class PersonalInfo extends StatefulWidget {
}
class _PersonalInfoState extends State<PersonalInfo> {
String _username='',_realname = '', _phone = '', _gender = '', _address = '', _email = '', _password = '';
String _username = '', _realname = '', _phone = '', _gender = '', _address = '', _email = '', _password = '';
bool _isEditing = false; //是否為編輯狀態
bool _passwordNotVisible = true;
@@ -28,19 +28,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
@override
void initState() {
//初始化
super.initState();
print('狀態:$_isEditing');
_fetchData();
}
void _fetchData() async {
//MYSQL
print('傳遞過來的 email: ${widget.email}'); // 打印 email 來確認它是否正確傳遞
print('傳遞過來的 email: ${widget.email}');
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
//127.0.0.1 10.0.2.2
port: 33061,
userName: 'root',
password: 'Topic@2024',
@@ -50,18 +46,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
await conn.connect();
try {
print('ok');
print('狀態:$_isEditing');
var result = await conn.execute(
'SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // 傳入參數 email
{'email': widget.email},
);
if (result.rows.isNotEmpty) {
//有資料
var row = result.rows.first;
setState(() {
_username =row.colAt(0)??'';
_realname = row.colAt(1) ?? ''; //如果沒有資料就是空直
_username = row.colAt(0) ?? '';
_realname = row.colAt(1) ?? '';
_phone = row.colAt(2) ?? '';
_gender = row.colAt(3) ?? '';
_address = row.colAt(4) ?? '';
@@ -79,7 +72,6 @@ class _PersonalInfoState extends State<PersonalInfo> {
void _toggleEdit() {
setState(() {
if (!_isEditing) {
// 當進入編輯模式時,將當前的資料賦予控制器
_realnameController.text = _realname;
_phoneController.text = _phone;
_genderController.text = _gender;
@@ -88,7 +80,6 @@ class _PersonalInfoState extends State<PersonalInfo> {
_passwordController.text = _password;
}
_isEditing = !_isEditing;
print('編輯狀態:$_isEditing');
});
}
@@ -112,7 +103,7 @@ class _PersonalInfoState extends State<PersonalInfo> {
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'new_email': _emailController.text,
'old_email': widget.email, // 用戶的舊 email 作為查找條件
'old_email': widget.email,
'homePassword': _passwordController.text,
},
);
@@ -123,213 +114,117 @@ class _PersonalInfoState extends State<PersonalInfo> {
_address = _addressController.text;
_email = _emailController.text;
_password = _passwordController.text;
// 清空控制器內容
_realnameController.clear();
_phoneController.clear();
_genderController.clear();
_addressController.clear();
_emailController.clear();
_passwordController.clear();
_isEditing = false; // 退出編輯
print('儲存狀態:$_isEditing');
_isEditing = false;
});
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
print('finally儲存更新狀態:$_isEditing');
}
}
void _loginOut() async {
print("loyout");
SharedPreferences prefs = await SharedPreferences.getInstance();
// remove all data in share preference(user data which save to identify user or others)
prefs.clear();
// navbar router setting
// replace screen with LoginPage and without navbar
pushReplacementWithoutNavBar(
context,
NoSwipeBackRoute(
builder: (context) => LoginPage(),
));
context,
NoSwipeBackRoute(
builder: (context) => LoginPage(),
),
);
}
//頁面
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: Text('個人資料'),
// backgroundColor: Color(0xFFF5E3C3),
// ),
body: Column(
children: [
Container(
height: 100,
height: 120,
color: Color(0xFFF5E3C3),
//背景底色
width: double.infinity,
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
'個人資料',
style: TextStyle(fontSize: 24, height: 5),
),
padding: EdgeInsets.only(top: 50, left: 20, right: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_username + ' 您好',
style: TextStyle(fontSize: 28, color: Colors.black),
),
],
),
),
Expanded(
child: ListView(
padding: EdgeInsets.symmetric(vertical: 5), // 调整列表视图的 padding
padding: EdgeInsets.symmetric(vertical: 5),
children: [
ListTile(
title: Text(
'使用名稱',
style: TextStyle(fontSize: 20),
),
subtitle: Text(_username),
leading: Icon(Icons.manage_accounts_outlined),
title: Text('基本資料', style: TextStyle(fontSize: 18)),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BasicInfoPage(
realname: _realname,
phone: _phone,
gender: _gender,
address: _address,
email: _email,
isEditing: _isEditing,
),
),
);
},
),
Divider(),
ListTile(
title: Text(
'姓名',
style: TextStyle(fontSize: 20),
),
subtitle: _isEditing
? TextField(
controller: _realnameController,
onChanged: (value) {
setState(() {
_realname = value;
});
},
)
: Text(_realname),
leading: Icon(Icons.lock_open_outlined),
title: Text('帳號設定', style: TextStyle(fontSize: 18)),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AccountPage(
email: _email,
password: _password,
isEditing: _isEditing,
),
),
);
},
),
Divider(),
ListTile(
title: Text('手機'),
subtitle: _isEditing
? TextField(
controller: _phoneController,
onChanged: (value) {
setState(() {
_phone = value;
});
},
)
: Text(_phone),
leading: Icon(Icons.language_outlined),
title: Text('語言設定', style: TextStyle(fontSize: 18)),
onTap: () {},
),
Divider(),
ListTile(
title: Text('性別'),
subtitle: _isEditing
? TextField(
controller: _genderController,
onChanged: (value) {
setState(() {
_gender = value;
});
},
)
: Text(_gender),
),
Divider(),
ListTile(
title: Text('地址'),
subtitle: _isEditing
? TextField(
controller: _addressController,
onChanged: (value) {
setState(() {
_address = value;
});
},
)
: Text(_address),
),
Divider(),
ListTile(
title: Text('電子信箱'),
subtitle: _isEditing
? TextField(
controller: _emailController,
onChanged: (value) {
setState(() {
_email = value;
});
},
)
: Text(_email),
),
Divider(),
ListTile(
title: Text('密碼'),
subtitle: _isEditing
? TextField(
controller: _passwordController,
obscureText: _passwordNotVisible,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(_passwordNotVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
_passwordNotVisible = !_passwordNotVisible;
},
);
},
)),
onChanged: (value) {
setState(() {
_password = value;
});
},
)
: Text(_password),
),
Divider(),
SizedBox(
height: 20,
width: 60,
),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges(); // 儲存變更
_saveChanges();
} else {
_toggleEdit(); // 進入編輯模式
_toggleEdit();
}
},
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(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
SizedBox(
height: 50, // 增加底部間距,確保按鈕不被遮蓋
ElevatedButton(
onPressed: _loginOut,
child: Text('登出'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
],
),
@@ -339,3 +234,294 @@ class _PersonalInfoState extends State<PersonalInfo> {
);
}
}
// 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
? TextField(
controller: _realnameController,
)
: Text(widget.realname),
),
Divider(),
ListTile(
title: Text('手機'),
subtitle: _isEditing
? TextField(
controller: _phoneController,
)
: Text(widget.phone),
),
Divider(),
ListTile(
title: Text('性別'),
subtitle: _isEditing
? TextField(
controller: _genderController,
)
: Text(widget.gender),
),
Divider(),
ListTile(
title: Text('地址'),
subtitle: _isEditing
? TextField(
controller: _addressController,
)
: Text(widget.address),
),
SizedBox(height: 20),
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('電子信箱'),
subtitle: _isEditing
? TextField(
controller: _emailController,
onChanged: (value) {
setState(() {
widget.email = value;
});
},
)
: Text(widget.email),
),
Divider(),
ListTile(
title: Text('密碼'),
subtitle: _isEditing
? TextField(
controller: _passwordController,
obscureText: _passwordNotVisible,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(_passwordNotVisible ? Icons.visibility : Icons.visibility_off),
onPressed: () {
setState(() {
_passwordNotVisible = !_passwordNotVisible;
});
},
),
),
onChanged: (value) {
setState(() {
widget.password = value;
});
},
)
: Text(widget.password),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges();
}
else {
_toggleEdit();
}
},
child: Text(_isEditing ? '儲存變更' : '修改資料'),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
],
),
);
}
}