Compare commits

...

4 Commits

Author SHA1 Message Date
JingChiang
cf3abfc12b style: format code 2024-09-08 20:28:52 +08:00
JingChiang
994701e2ff feat: 新增基礎驗證、錯誤處理
1. 為註冊資料新增基礎驗證及驗證碼驗證實作
 2. 錯誤欄位游標自動移到該欄
2024-09-08 20:23:18 +08:00
JingChiang
7e6b0df97f feat: textField 新增 focusNode 設置
新增 focusNode 用以控制游標目標位置
2024-09-08 20:21:01 +08:00
JingChiang
7c33bd99d9 更改資料庫連接,testdb->care(RegisterPage) 2024-09-08 20:19:12 +08:00
7 changed files with 285 additions and 225 deletions

View File

@@ -1,11 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:topic/BottomNavBar.dart';
import 'package:topic/main.dart';
import 'package:topic/HomePage.dart';
import 'package:topic/PersonalInfo.dart';
import 'package:topic/KnowledgePage.dart';
import 'package:topic/MessagePage.dart';
import 'package:mysql_client/mysql_client.dart';
class HistoricalRecord extends StatefulWidget {
@@ -42,7 +35,8 @@ class _HistoricalRecordState extends State<HistoricalRecord> {
await conn.connect();
try {
var result = await conn.execute('SELECT HomeElderFall.*, HomeElder.heName FROM HomeElderFall JOIN HomeElder ON HomeElderFall.heId = HomeElder.heId;');
var result = await conn.execute(
'SELECT HomeElderFall.*, HomeElder.heName FROM HomeElderFall JOIN HomeElder ON HomeElderFall.heId = HomeElder.heId;');
print('Result: ${result.length} rows found.');
if (result.rows.isEmpty) {
@@ -51,12 +45,12 @@ class _HistoricalRecordState extends State<HistoricalRecord> {
setState(() {
_results = result.rows
.map((row) => {
'長者ID': row.colAt(0),//去裝資料庫的行數
'姓名': row.colAt(8),
'跌倒時間': row.colAt(1),
'跌倒原因': row.colAt(2),
'跌倒地點': row.colAt(7),
})
'長者ID': row.colAt(0), //去裝資料庫的行數
'姓名': row.colAt(8),
'跌倒時間': row.colAt(1),
'跌倒原因': row.colAt(2),
'跌倒地點': row.colAt(7),
})
.toList();
});
}

View File

@@ -1,7 +1,4 @@
// import 'dart:html';
import 'package:flutter/material.dart';
import 'package:topic/TryPage.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'BottomNavBar.dart';
@@ -61,7 +58,6 @@ class _HomePageState extends State<HomePage> {
height: 240,
width: double.infinity,
// TODO: 如果用http需要分別設定ios, android權限
// TODO: 替換video_player to flutter_webView
child: WebViewWidget(
controller: WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)

View File

@@ -9,6 +9,7 @@ import 'package:topic/HomePage.dart';
class KnowledgePage extends StatelessWidget {
final String email; // 接收來自上個頁面的 email
KnowledgePage({required this.email});
@override
Widget build(BuildContext context) {
return Scaffold(

View File

@@ -58,7 +58,6 @@ class _MessagePageState extends State<MessagePage> {
height: 240,
width: double.infinity,
// TODO: 如果用http需要分別設定ios, android權限
// TODO: 替換video_player to flutter_webView
child: WebViewWidget(
controller: WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)

View File

@@ -4,21 +4,22 @@ 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 = '';
String _name = '',
_phone = '',
_gender = '',
_address = '',
_email = '',
_password = '';
bool _isEditing = false; //是否為編輯狀態
final TextEditingController _nameController = TextEditingController();
final TextEditingController _phoneController = TextEditingController();
@@ -53,19 +54,20 @@ class _PersonalInfoState extends State<PersonalInfo> {
try {
print('ok');
print('狀態:$_isEditing');
var result = await conn.execute('SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // 傳入參數 email
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)??'';
_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) {
@@ -104,16 +106,16 @@ class _PersonalInfoState extends State<PersonalInfo> {
try {
await conn.execute(
'UPDATE HomeLogin SET homeName = :homeName, homePhone = :homePhone, homeGender = :homeGender, homeAddress = :homeAddress, homeEmail = :new_email, homePassword = :homePassword WHERE homeEmail = :old_email',
{
'homeName':_nameController.text,
'homePhone': _phoneController.text,
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'new_email': _emailController.text,
'old_email': widget.email, // 用戶的舊 email 作為查找條件
'homePassword': _passwordController.text,
},
'UPDATE HomeLogin SET homeName = :homeName, homePhone = :homePhone, homeGender = :homeGender, homeAddress = :homeAddress, homeEmail = :new_email, homePassword = :homePassword WHERE homeEmail = :old_email',
{
'homeName': _nameController.text,
'homePhone': _phoneController.text,
'homeGender': _genderController.text,
'homeAddress': _addressController.text,
'new_email': _emailController.text,
'old_email': widget.email, // 用戶的舊 email 作為查找條件
'homePassword': _passwordController.text,
},
);
setState(() {
_name = _nameController.text;
@@ -171,7 +173,8 @@ class _PersonalInfoState extends State<PersonalInfo> {
children: [
Container(
height: 100,
color: Color(0xFFF5E3C3),//背景底色
color: Color(0xFFF5E3C3),
//背景底色
width: double.infinity,
padding: EdgeInsets.all(10.0),
child: Center(
@@ -187,7 +190,10 @@ class _PersonalInfoState extends State<PersonalInfo> {
children: [
ListTile(
title: Text('姓名',style: TextStyle(fontSize: 20),),
title: Text(
'姓名',
style: TextStyle(fontSize: 20),
),
subtitle: Text(_name),
),
Divider(),
@@ -195,13 +201,13 @@ class _PersonalInfoState extends State<PersonalInfo> {
title: Text('手機'),
subtitle: _isEditing
? TextField(
controller: _phoneController,
onChanged: (value) {
setState(() {
_phone = value;
});
},
)
controller: _phoneController,
onChanged: (value) {
setState(() {
_phone = value;
});
},
)
: Text(_phone),
),
Divider(),
@@ -209,13 +215,13 @@ class _PersonalInfoState extends State<PersonalInfo> {
title: Text('性別'),
subtitle: _isEditing
? TextField(
controller: _genderController,
onChanged: (value) {
setState(() {
_gender = value;
});
},
)
controller: _genderController,
onChanged: (value) {
setState(() {
_gender = value;
});
},
)
: Text(_gender),
),
Divider(),
@@ -223,13 +229,13 @@ class _PersonalInfoState extends State<PersonalInfo> {
title: Text('地址'),
subtitle: _isEditing
? TextField(
controller: _addressController,
onChanged: (value) {
setState(() {
_address = value;
});
},
)
controller: _addressController,
onChanged: (value) {
setState(() {
_address = value;
});
},
)
: Text(_address),
),
Divider(),
@@ -237,13 +243,13 @@ class _PersonalInfoState extends State<PersonalInfo> {
title: Text('電子信箱'),
subtitle: _isEditing
? TextField(
controller: _emailController,
onChanged: (value) {
setState(() {
_email = value;
});
},
)
controller: _emailController,
onChanged: (value) {
setState(() {
_email = value;
});
},
)
: Text(_email),
),
Divider(),
@@ -251,23 +257,26 @@ class _PersonalInfoState extends State<PersonalInfo> {
title: Text('密碼'),
subtitle: _isEditing
? TextField(
controller: _passwordController,
onChanged: (value) {
setState(() {
_password = value;
});
},
)
controller: _passwordController,
onChanged: (value) {
setState(() {
_password = value;
});
},
)
: Text(_password),
),
Divider(),
SizedBox(height: 20,width: 60,),
SizedBox(
height: 20,
width: 60,
),
ElevatedButton(
onPressed: () {
if (_isEditing) {
_saveChanges(); // 儲存變更
_saveChanges(); // 儲存變更
} else {
_toggleEdit(); // 進入編輯模式
_toggleEdit(); // 進入編輯模式
}
},
child: Text(_isEditing ? '儲存變更' : '修改資料'),
@@ -277,7 +286,10 @@ class _PersonalInfoState extends State<PersonalInfo> {
shadowColor: Colors.transparent, // 去除陰影
),
),
SizedBox(height: 10,width: 60,),
SizedBox(
height: 10,
width: 60,
),
ElevatedButton(
onPressed: () {
_loginOut();

View File

@@ -18,6 +18,7 @@ class RegisterPage extends StatefulWidget {
}
class _RegisterPageState extends State<RegisterPage> {
late FocusNode _nameFocusNode, _emailFocusNode, _passwordFocusNode, _codeFocusNode;
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@@ -33,6 +34,10 @@ class _RegisterPageState extends State<RegisterPage> {
void initState() {
super.initState();
_fetchData();
_nameFocusNode = FocusNode();
_emailFocusNode = FocusNode();
_passwordFocusNode = FocusNode();
_codeFocusNode = FocusNode();
}
void _fetchData() async {
@@ -106,6 +111,7 @@ class _RegisterPageState extends State<RegisterPage> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('請輸入正確的電子信箱')),
);
_emailFocusNode.requestFocus();
return;
}
@@ -114,24 +120,63 @@ class _RegisterPageState extends State<RegisterPage> {
_startTimer();
}
bool _isDataCorrect() {
String name = _nameController.text;
String email = _emailController.text;
String password = _passwordController.text;
String code = _codeController.text;
if (name == '') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('用戶姓名不可為空')),
);
_nameFocusNode.requestFocus();
return false;
}
if (email == '') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('電子信箱不可為空')),
);
_emailFocusNode.requestFocus();
return false;
}
if (password == '') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('密碼不可為空')),
);
_passwordFocusNode.requestFocus();
return false;
}
if (code != _generatedCode) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('驗證碼錯誤')),
);
_codeFocusNode.requestFocus();
return false;
}
return true;
}
void registerBtn() async {
// TODO: 驗證碼未實作驗證
if (!_isDataCorrect()) return;
final conn = await MySQLConnection.createConnection(
host: '10.0.2.2',
port: 3306,
host: '203.64.84.154',
port: 33061,
userName: 'root',
password: '0000',
databaseName: 'testdb',
password: 'Topic@2024',
databaseName: 'care',
);
await conn.connect();
try {
String name = _nameController.text;
String email = _emailController.text;
String password = _passwordController.text;
var result = await conn.execute(
'SELECT * FROM users WHERE email = :email AND password = :password',
'SELECT * FROM HomeLogin WHERE homeEmail = :email AND homePassword = :password',
{'email': email, 'password': password},
);
@@ -141,7 +186,7 @@ class _RegisterPageState extends State<RegisterPage> {
);
} else {
await conn.execute(
'INSERT INTO users (name, email, password) VALUES (:name, :email, :password)',
'INSERT INTO HomeLogin (homeName, homeEmail, homePassword) VALUES (:name, :email, :password)',
{'name': name, 'email': email, 'password': password},
);
@@ -163,7 +208,7 @@ class _RegisterPageState extends State<RegisterPage> {
} catch (e) {
print('資料庫錯誤: $e');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('登入失敗:系統錯誤')),
SnackBar(content: Text('註冊失敗:系統錯誤')),
);
} finally {
await conn.close();
@@ -172,8 +217,13 @@ class _RegisterPageState extends State<RegisterPage> {
@override
void dispose() {
_timer?.cancel();
super.dispose();
_timer?.cancel();
_nameFocusNode.dispose();
_emailFocusNode.dispose();
_passwordFocusNode.dispose();
_codeFocusNode.dispose();
}
@override
@@ -207,6 +257,7 @@ class _RegisterPageState extends State<RegisterPage> {
SizedBox(height: 20),
TextField(
controller: _nameController,
focusNode: _nameFocusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.person_outlined),
@@ -216,6 +267,7 @@ class _RegisterPageState extends State<RegisterPage> {
SizedBox(height: 20),
TextField(
controller: _emailController,
focusNode: _emailFocusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.email_outlined),
@@ -226,6 +278,7 @@ class _RegisterPageState extends State<RegisterPage> {
SizedBox(height: 20),
TextField(
controller: _passwordController,
focusNode: _passwordFocusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.lock_outlined),
@@ -235,7 +288,7 @@ class _RegisterPageState extends State<RegisterPage> {
: Icons.visibility_off),
onPressed: () {
setState(
() {
() {
_passwordNotVisible = !_passwordNotVisible;
},
);
@@ -251,6 +304,7 @@ class _RegisterPageState extends State<RegisterPage> {
Expanded(
child: TextField(
controller: _codeController,
focusNode: _codeFocusNode,
decoration: InputDecoration(
labelText: '驗證碼',
hintText: '填寫驗證碼',

View File

@@ -5,13 +5,14 @@ import 'package:topic/HomePage.dart';
import 'package:mysql_client/mysql_client.dart';
import 'package:topic/NoSwipeBackRoute.dart';
import 'package:topic/RegisterPage.dart';
void main() {
runApp(MaterialApp(
home: LoginPage(),
));
}
class LoginPage extends StatefulWidget {//ful會改變
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
@@ -22,10 +23,11 @@ class _LoginPageState extends State<LoginPage> {
bool _passwordNotVisible = true;
@override
void initState() {//初始化
void initState() {
//初始化
super.initState();
_fetchData();//連資料庫
_CheckPreLoginInfo();//確定有先前有無登入
_fetchData(); //連資料庫
_CheckPreLoginInfo(); //確定有先前有無登入
}
void _fetchData() async {
@@ -44,12 +46,12 @@ class _LoginPageState extends State<LoginPage> {
await conn.connect();
}
void _CheckPreLoginInfo() async{
void _CheckPreLoginInfo() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//get user data from share preference else set empty to check not login before
String loginUserEmail = prefs.getString("email") ?? "";
//replace screen to HomePage if there are previous data in share preference
if(loginUserEmail != ""){
if (loginUserEmail != "") {
Navigator.pushReplacement(
context,
NoSwipeBackRoute(
@@ -61,134 +63,12 @@ class _LoginPageState extends State<LoginPage> {
}
}
void _setLoginInfo(String email) async{
void _setLoginInfo(String email) async {
//save user email into share preference to let app can auto login next open
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("email", email);
}
@override
Widget build(BuildContext context) {
debugPaintSizeEnabled=false;
//by use PopScope and disable can Pop avoid android user pop back by back button
return PopScope(
canPop: false,
child: Scaffold(
//appBar: AppBar(
//title: Text('Demo'),
//backgroundColor: Color(0xFF81D4FA),
//),
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.only(bottom: 40), // 添加間距
height: 100, // 設置logo高度
child: Icon(
Icons.account_circle,
size: 100, // 設置圖標大小
color: Color(0xFF4FC3F7), // 設置圖標颜色
),
),
Text(
'全方位照護守護者',
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),//全方位照護守護者
SizedBox(height: 20),
TextField(
controller: _emailController, // 绑定電子信箱输入框的控制器
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.email_outlined),//https://www.fluttericon.cn/v
labelText: '電子信箱',
),
),//電子信箱
SizedBox(height: 20),
TextField(
controller: _ageController,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(_passwordNotVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
_passwordNotVisible = !_passwordNotVisible;
},
);
},
),
labelText: '密碼',
),
obscureText: _passwordNotVisible,
),//密碼
SizedBox(height: 20),
ElevatedButton(
onPressed: loginBtn,/*() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},*/
child: Text(' 登入'),
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF4FC3F7),
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 15),
textStyle: TextStyle(fontSize: 18),
),
),//登入
SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RegisterPage()),
);
},
child: Text('立即註冊'),
style: TextButton.styleFrom(
backgroundColor: Colors.transparent, // 無背景颜色
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, // 去除陰影
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage(
email: _emailController.text,
)),
);
},
child: Text('忘記密碼'),
style: TextButton.styleFrom(
backgroundColor: Colors.transparent, // 無背景颜色
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, // 去除陰影
),
),
],
),
),
),
),
),
),
);
}
void loginBtn() async {
final conn = await MySQLConnection.createConnection(
host: '203.64.84.154',
@@ -247,4 +127,128 @@ class _LoginPageState extends State<LoginPage> {
await conn.close();
}
}
}
@override
Widget build(BuildContext context) {
debugPaintSizeEnabled = false;
//by use PopScope and disable can Pop avoid android user pop back by back button
return PopScope(
canPop: false,
child: Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.only(bottom: 40), // 添加間距
height: 100, // 設置logo高度
child: Icon(
Icons.account_circle,
size: 100, // 設置圖標大小
color: Color(0xFF4FC3F7), // 設置圖標颜色
),
),
Text(
'全方位照護守護者',
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.black,
),
), //全方位照護守護者
SizedBox(height: 20),
TextField(
controller: _emailController, // 绑定電子信箱输入框的控制器
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.email_outlined),
//https://www.fluttericon.cn/v
labelText: '電子信箱',
),
), //電子信箱
SizedBox(height: 20),
TextField(
controller: _ageController,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(_passwordNotVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
_passwordNotVisible = !_passwordNotVisible;
},
);
},
),
labelText: '密碼',
),
obscureText: _passwordNotVisible,
), //密碼
SizedBox(height: 20),
ElevatedButton(
onPressed: loginBtn,
/*() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},*/
child: Text(' 登入'),
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF4FC3F7),
padding:
EdgeInsets.symmetric(horizontal: 50, vertical: 15),
textStyle: TextStyle(fontSize: 18),
),
), //登入
SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RegisterPage()),
);
},
child: Text('立即註冊'),
style: TextButton.styleFrom(
backgroundColor: Colors.transparent, // 無背景颜色
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, // 去除陰影
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(
email: _emailController.text,
)),
);
},
child: Text('忘記密碼'),
style: TextButton.styleFrom(
backgroundColor: Colors.transparent, // 無背景颜色
textStyle: TextStyle(fontSize: 18),
shadowColor: Colors.transparent, // 去除陰影
),
),
],
),
),
),
),
),
),
);
}
}