Files
topicApp/lib/PersonalInfo.dart

246 lines
7.2 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';
/*void main() {
runApp(MaterialApp(
home: PersonalInfo(),
));
}*/
class PersonalInfo extends StatefulWidget {
final String email; // 接收來自上個頁面的 email
PersonalInfo({required this.email});
@override
_PersonalInfoState createState() => _PersonalInfoState();
}
class _PersonalInfoState extends State<PersonalInfo> {
String _name = '', _phone = '', _gender = '', _address='', _email = '', _password = '';
bool _isEditing = false; //是否為編輯狀態
final TextEditingController _emailController =
TextEditingController(); //email輸入text
@override
void initState() {
//初始化
super.initState();
_fetchData();
}
void _fetchData() async {
//MYSQL
print('傳遞過來的 email: ${widget.email}'); // 打印 email 來確認它是否正確傳遞
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
//host: '10.0.2.2',
//127.0.0.1 10.0.2.2
port: 33061,
userName: 'root',
password: 'Topic@2024',
//password: '0000',
databaseName: 'care', //testdb
//databaseName: 'testdb',
);
await conn.connect();
try {
print('ok');
var result = await conn.execute(
'SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // 傳入參數 email
);
if (result.rows.isNotEmpty) {
//有資料
var row = result.rows.first;
setState(() {
_name = row.colAt(0)??'';//如果沒有資料就是空直
_phone = row.colAt(1)??'';
_gender = row.colAt(2)??'';
_address = row.colAt(3)??'';
_email = row.colAt(4)??'';
_password = row.colAt(5)??'';
});
}
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
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 homeemail = :email WHERE homeemail = :old_email',
{'email': _emailController.text, 'old_email': widget.email},
);
setState(() {
_email = _emailController.text;
_isEditing = false; // 退出編輯
});
} catch (e) {
print('Error: $e');
} finally {
await conn.close();
}
}
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(),
));
}
//頁面
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: Text('個人資料'),
// backgroundColor: Color(0xFFF5E3C3),
// ),
body: Column(
children: [
Container(
height: 100,
color: Color(0xFFF5E3C3), //背景底色
width: double.infinity,
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
'個人資料',
style: TextStyle(fontSize: 24, height: 5),
),
),
),
Expanded(
child: ListView(
padding: EdgeInsets.symmetric(vertical: 5), // 调整列表视图的 padding
children: [
ListTile(
title: Text(
'姓名',
style: TextStyle(fontSize: 20),
),
subtitle: Text(_name),
//trailing: Icon(Icons.arrow_forward_ios),//>
onTap: () {
//點了之後
},
),
Divider(),
ListTile(
title: Text('手機'),
subtitle: Text(_phone),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {},
),
Divider(),
ListTile(
title: Text('性別'),
subtitle: Text(_gender),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {},
),
Divider(),
ListTile(
title: Text('地址'),
subtitle: Text(_address),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {},
),
Divider(),
ListTile(
title: Text('電子信箱'),
subtitle: _isEditing
? TextField(
controller: _emailController,
autofocus: true,
onChanged: (value) {
setState(() {
_email = value;
});
},
)
: Text(_email),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
_toggleEdit(); // 切換到編輯狀態
},
),
Divider(),
ListTile(
title: Text('密碼'),
subtitle: Text(_password),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
},
),
Divider(),
SizedBox(
height: 20,
width: 60,
),
ElevatedButton(
onPressed: () {},
child: Text('修改資料'),
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,
),
),
],
),
),
],
),
);
}
}