Files
topicApp/lib/PersonalInfo.dart
2024-12-05 00:29:11 +08:00

660 lines
21 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.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/HomePage.dart';
import 'package:topic/NoSwipeBackRoute.dart';
import 'package:topic/main.dart';
import 'package:url_launcher/url_launcher.dart';
import 'generated/l10n.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: 'comprehensive-guardian.systems',
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: 'comprehensive-guardian.systems',
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;
print('有道這裡1');
});
} 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(),
),
);
}
void _setLanguage(Locale locale) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("language", locale.languageCode);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
height: 100,
color: Color(0xFFF5E3C3),
padding: EdgeInsets.only(top: 50, left: 20, right: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
S.of(context).welcome_message(_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(S.of(context).basic_information_setting, 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(S.of(context).account_setting, 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(S.of(context).language_setting, style: TextStyle(fontSize: 18)),
onTap: () {
if(S.current.login_button == 'Login'){
S.load(Locale('zh_Hant'));
_setLanguage(Locale('zh_Hant'));
}else{
S.load(Locale('en', 'US'));
_setLanguage(Locale('en', 'US'));
}
showDialog(
context: context,
builder: (BuildContext context) {
if (Platform.isAndroid) {
// Android-specific code
return AlertDialog(
title: Text(S.of(context).language_setting_alert_title),
content: Text(S.of(context).language_alert_android),
actions: <Widget>[
TextButton(
onPressed: () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
},
child: Text(S.of(context).confirm),
),
],
);
} else {
// iOS-specific code
return AlertDialog(
title: Text(S.of(context).language_setting_alert_title),
content: Text(S.of(context).language_alert),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {});
},
child: Text(S.of(context).cancel),
),
TextButton(
onPressed: () {
// SystemChannels.platform.invokeMethod('SystemNavigator.pop');
pushReplacementWithoutNavBar(context, MaterialPageRoute(builder:(context) => HomePage(
email: widget.email)),
);
},
child: Text(S.of(context).confirm),
),
],
);
}
},
);
},
),
Divider(),
ListTile(
leading: Icon(Icons.local_phone_rounded),
title: Text(S.of(context).call_phone, style: TextStyle(fontSize: 18)),
onTap: () {
_showDialerDialog(context); // 顯示對話框,確認是否撥打電話
},
),
Divider(),
ElevatedButton(
onPressed: _loginOut,
child: Text(S.of(context).logout_button),
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: 'comprehensive-guardian.systems',
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(
body: Column(
children: [
Container(
height: 100,
color: Color(0xFFF5E3C3),
width: double.infinity,
padding: EdgeInsets.only(left: 0.0, top: 50.0), // 調整左右和底部的間距
child:
ListTile(
contentPadding: EdgeInsets.zero, // 移除 ListTile 內的預設 padding
leading: IconButton(
icon: Icon(Icons.arrow_back_outlined, size: 28),
onPressed: () {
Navigator.of(context).pop();
},
),title: Text(S.of(context).basic_information_setting, style: TextStyle(fontSize: 28)),
onTap: () {
},
),),
Expanded(
child: ListView(
padding: EdgeInsets.all(16),
children: [
ListTile(
title: Text(S.of(context).name),
subtitle: _isEditing
? TextField(
controller: _realnameController,
)
: Text(_realnameController.text),//Controller.text才會跟著修正更新
),
Divider(),
ListTile(
title: Text(S.of(context).phone),
subtitle: _isEditing
? TextField(
controller: _phoneController,
)
: Text(_phoneController.text),
),
Divider(),
ListTile(
title: Text(S.of(context).gender),
subtitle: _isEditing
? TextField(
controller: _genderController,
)
: Text(_genderController.text),
),
Divider(),
ListTile(
title: Text(S.of(context).address),
subtitle: _isEditing
? TextField(
controller: _addressController,
)
: Text(_addressController.text),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges();
} else {
_toggleEdit();
}
},
child: Text(_isEditing ? S.of(context).save : S.of(context).edit),
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: 'comprehensive-guardian.systems',
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(
body: Column(
children: [
Container(
height: 100,
color: Color(0xFFF5E3C3),
width: double.infinity,
padding: EdgeInsets.only(left: 0.0, top: 50.0), // 調整左右和底部的間距
child:
ListTile(
contentPadding: EdgeInsets.zero, // 移除 ListTile 內的預設 padding
leading: IconButton(
icon: Icon(Icons.arrow_back_outlined, size: 28),
onPressed: () {
Navigator.of(context).pop();
},
),
title: Text(S.of(context).account_setting, style: TextStyle(fontSize: 28)),
onTap: () {
},
),
),
Expanded(
child: ListView(
padding: EdgeInsets.all(16),
children: [
ListTile(
title: Text(S.of(context).email),
subtitle: _isEditing
? TextField(
controller: _emailController,
)
: Text(_emailController.text),
),
Divider(),
ListTile(
title: Text(S.of(context).password_label),
subtitle: _isEditing
? TextField(
controller: _passwordController,
obscureText: _passwordNotVisible,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(_passwordNotVisible ? Icons.visibility : Icons.visibility_off),
onPressed: () {
setState(() {
_passwordNotVisible = !_passwordNotVisible;
});
},
),
),
)
: Text(_passwordController.text),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges();
}
else {
_toggleEdit();
}
},
child: Text(_isEditing ? S.of(context).save : S.of(context).edit),
style: TextButton.styleFrom(
backgroundColor: Color(0xFFF5E3C3),
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent,
),
),
],
),
),
],
),
);
}
}
void _launchDialer(BuildContext context, String phoneNumber) async {
final Uri launchUri = Uri(
scheme: 'tel',
path: phoneNumber,
);
if (await canLaunch(launchUri.toString())) {
await launch(launchUri.toString());
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('無法撥打電話至 $phoneNumber')),
);
}
}
// 顯示確認撥打電話的對話框
void _showDialerDialog(BuildContext context) {
String phoneNumber = "123456789";
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('撥打電話'),
content: Text('你確定要撥打電話至 $phoneNumber 嗎?'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('關閉'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
_launchDialer(context, phoneNumber);//跳至撥打電話
},
child: Text('撥打電話'),
),
],
);
},
);
}