topicApp/lib/PersonalInfo.dart
2024-09-22 17:37:01 +08:00

528 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mysql_client/mysql_client.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:topic/NoSwipeBackRoute.dart';
import 'package:topic/main.dart';
class PersonalInfo extends StatefulWidget {
final String email; // 接收來自上個頁面的 email
PersonalInfo({required this.email});
@override
_PersonalInfoState createState() => _PersonalInfoState();
}
class _PersonalInfoState extends State<PersonalInfo> {
String _username = '', _realname = '', _phone = '', _gender = '', _address = '', _email = '', _password = '';
bool _isEditing = false; //是否為編輯狀態
bool _passwordNotVisible = true;
final TextEditingController _realnameController = TextEditingController();
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _genderController = TextEditingController();
final TextEditingController _addressController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
void initState() {
super.initState();
_fetchData();
}
void _fetchData() async {
print('傳遞過來的 email: ${widget.email}');
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
port: 33061,
userName: 'root',
password: 'Topic@2024',
databaseName: 'care',
);
await conn.connect();
try {
var result = await conn.execute(
'SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email},
);
if (result.rows.isNotEmpty) {
var row = result.rows.first;
setState(() {
_username = row.colAt(0) ?? '';
_realname = row.colAt(1) ?? '';
_phone = row.colAt(2) ?? '';
_gender = row.colAt(3) ?? '';
_address = row.colAt(4) ?? '';
_email = row.colAt(5) ?? '';
_password = row.colAt(6) ?? '';
});
}
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
void _toggleEdit() {
setState(() {
if (!_isEditing) {
_realnameController.text = _realname;
_phoneController.text = _phone;
_genderController.text = _gender;
_addressController.text = _address;
_emailController.text = _email;
_passwordController.text = _password;
}
_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, homeEmail = :new_email, homePassword = :homePassword WHERE homeEmail = :old_email',
{
'homeRealName': _realnameController.text,
'homePhone': _phoneController.text,
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'new_email': _emailController.text,
'old_email': widget.email,
'homePassword': _passwordController.text,
},
);
setState(() {
_realname = _realnameController.text;
_phone = _phoneController.text;
_gender = _genderController.text;
_address = _addressController.text;
_email = _emailController.text;
_password = _passwordController.text;
_isEditing = false;
});
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
void _loginOut() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
pushReplacementWithoutNavBar(
context,
NoSwipeBackRoute(
builder: (context) => LoginPage(),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
height: 120,
color: Color(0xFFF5E3C3),
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),
children: [
ListTile(
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(
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(
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('successsssssssssssssss');
} 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,
),
),
],
),
);
}
}