init commit
This commit is contained in:
165
lib/HistoricalRecord.dart
Normal file
165
lib/HistoricalRecord.dart
Normal file
@@ -0,0 +1,165 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.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 {
|
||||
final String email; // 接收來自上個頁面的 email
|
||||
HistoricalRecord({required this.email});
|
||||
|
||||
@override
|
||||
_HistoricalRecordState createState() => _HistoricalRecordState();
|
||||
}
|
||||
|
||||
class _HistoricalRecordState extends State<HistoricalRecord> {
|
||||
List<Map<String, dynamic>> _results = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fetchData(); // 連資料庫
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
print('connect');
|
||||
|
||||
final conn = await MySQLConnection.createConnection(
|
||||
//host: '203.64.84.154',
|
||||
host:'10.0.2.2',
|
||||
//127.0.0.1 10.0.2.2
|
||||
port: 3306,
|
||||
userName: 'root',
|
||||
//password: 'Topic@2024',
|
||||
password: '0000',
|
||||
//databaseName: 'care', //testdb
|
||||
databaseName: 'testdb',
|
||||
);
|
||||
await conn.connect();
|
||||
|
||||
try {
|
||||
var result = await conn.execute('SELECT * FROM users');
|
||||
print('Result: ${result.length} rows found.');
|
||||
|
||||
if (result.rows.isEmpty) {
|
||||
print('No data found in users table.');
|
||||
} else {
|
||||
setState(() {
|
||||
_results = result.rows.map((row) => {
|
||||
'id': row.colAt(0),
|
||||
'name': row.colAt(1),
|
||||
'age':row.colAt(2),
|
||||
'gender':row.colAt(3),
|
||||
'phone':row.colAt(4),
|
||||
'email':row.colAt(5),
|
||||
'password':row.colAt(2)
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
} 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.all(10.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'跌倒紀錄',
|
||||
style: TextStyle(fontSize: 24, height: 5),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.symmetric(vertical: 5), // 调整列表视图的 padding
|
||||
|
||||
itemCount: _results.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(_results[index]['name']),
|
||||
subtitle: Text(
|
||||
'age: ${_results[index]['age']}, '
|
||||
'gender: ${_results[index]['gender']}, '
|
||||
'phone: ${_results[index]['phone']}, '
|
||||
'email: ${_results[index]['email']}',
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: [
|
||||
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.history_edu), label: '跌倒紀錄'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.lightbulb_outline), label: '知識補充'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.monitor_outlined), label: '切換畫面'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.person_sharp), label: '個人資料'),
|
||||
],
|
||||
currentIndex: 1,
|
||||
selectedItemColor: Colors.orange,
|
||||
unselectedItemColor: Colors.grey,
|
||||
selectedLabelStyle: TextStyle(color: Colors.orange),
|
||||
unselectedLabelStyle: TextStyle(color: Colors.grey),
|
||||
onTap: (index) {
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => HomePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => HistoricalRecord(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 2) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => KnowledgePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 3) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MessagePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 4) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PersonalInfo(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
175
lib/HomePage.dart
Normal file
175
lib/HomePage.dart
Normal file
@@ -0,0 +1,175 @@
|
||||
// import 'dart:html';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:topic/HistoricalRecord.dart';
|
||||
import 'package:topic/PersonalInfo.dart';
|
||||
import 'package:topic/KnowledgePage.dart';
|
||||
import 'package:topic/MessagePage.dart';
|
||||
import 'package:topic/TryPage.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
final String email;
|
||||
|
||||
HomePage({required this.email});
|
||||
|
||||
@override
|
||||
_HomePageState createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
late WebViewController _webViewController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
height: 100,
|
||||
// APPBar height
|
||||
color: Color(0xFFF5E3C3),
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(10.0),
|
||||
child: Stack(children: [
|
||||
Container(
|
||||
padding: EdgeInsets.only(
|
||||
left: MediaQuery.of(context).size.width,
|
||||
top: MediaQuery.of(context).size.height / 2,
|
||||
),
|
||||
child: Icon(Icons.settings, size: 48, color: Colors.orange),
|
||||
),
|
||||
Center(
|
||||
child: Text(
|
||||
'全方位照護守護者',
|
||||
style: TextStyle(fontSize: 24, height: 5),
|
||||
),
|
||||
),
|
||||
|
||||
])),
|
||||
Container(
|
||||
color: Color(0xFFFFF0E0),
|
||||
margin: EdgeInsets.only(top: 5),
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
height: 240,
|
||||
width: double.infinity,
|
||||
// TODO: 如果用http需要分別設定ios, android權限
|
||||
// TODO: 替換video_player to flutter_webView
|
||||
child: WebViewWidget(
|
||||
controller: WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onProgress: (int progress) {
|
||||
// Update loading bar.
|
||||
},
|
||||
onPageStarted: (String url) {},
|
||||
onPageFinished: (String url) {},
|
||||
onHttpError: (HttpResponseError error) {
|
||||
print("httpError");
|
||||
},
|
||||
onWebResourceError: (WebResourceError error) {
|
||||
print("httpResourceError");
|
||||
print(error.description);
|
||||
},
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
if (request.url.startsWith(
|
||||
'http://203.64.84.154:8088/video_feed')) {
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse('http://203.64.84.154:8088')),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'即時畫面',
|
||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: GridView.count(
|
||||
crossAxisCount: 2,
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
padding: EdgeInsets.all(16),
|
||||
children: [
|
||||
_buildGridItem(Icons.history_edu, '跌倒紀錄', context),
|
||||
_buildGridItem(Icons.lightbulb_outline, '知識補充', context),
|
||||
_buildGridItem(Icons.monitor_outlined, '切換畫面', context),
|
||||
_buildGridItem(Icons.person_sharp, '個人資料', context),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGridItem(IconData icon, String label, BuildContext context) {
|
||||
return Card(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
color: Colors.white,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
if (label == '跌倒紀錄') {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => HistoricalRecord(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (label == '知識補充') {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => KnowledgePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
} else if (label == '123') {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => TryPage()),
|
||||
);
|
||||
} else if (label == '個人資料') {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PersonalInfo(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 48, color: Colors.orange),
|
||||
SizedBox(height: 8),
|
||||
Text(label, style: TextStyle(fontSize: 16)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
139
lib/KnowledgePage.dart
Normal file
139
lib/KnowledgePage.dart
Normal file
@@ -0,0 +1,139 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:topic/main.dart';
|
||||
import 'package:topic/HomePage.dart';
|
||||
import 'package:topic/HistoricalRecord.dart';
|
||||
import 'package:topic/PersonalInfo.dart';
|
||||
import 'package:topic/MessagePage.dart';
|
||||
/*void main() {
|
||||
runApp(MaterialApp(
|
||||
home: KnowledgePage(),
|
||||
));
|
||||
}*/
|
||||
|
||||
class KnowledgePage extends StatelessWidget {
|
||||
final String email; // 接收來自上個頁面的 email
|
||||
KnowledgePage({required this.email});
|
||||
@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.builder(
|
||||
padding: EdgeInsets.symmetric(vertical: 5), // 调整列表视图的 padding
|
||||
itemCount: 3,
|
||||
itemBuilder: (context, idx) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HomePage(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
},
|
||||
child: Card(
|
||||
//margin: EdgeInsets.all(10),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Image.asset(
|
||||
'lib/images/456.jpg',
|
||||
height: 180,
|
||||
width: double.infinity,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.all(10.0),
|
||||
child: Text(
|
||||
'文章標題 $idx',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 10.0),
|
||||
child: Text(
|
||||
'這是文章的簡短描述...',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: [
|
||||
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.history_edu), label: '跌倒紀錄'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.lightbulb_outline), label: '知識補充'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.monitor_outlined), label: '切換畫面'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.person_sharp), label: '個人資料'),
|
||||
],
|
||||
currentIndex: 2,
|
||||
selectedItemColor: Colors.orange,
|
||||
unselectedItemColor: Colors.grey,
|
||||
onTap: (index) {
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HomePage(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HistoricalRecord(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 2) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => KnowledgePage(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 3) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => MessagePage(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
} else if (index == 4) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => PersonalInfo(
|
||||
email: email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
136
lib/MessagePage.dart
Normal file
136
lib/MessagePage.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:topic/HomePage.dart';
|
||||
import 'package:topic/HistoricalRecord.dart';
|
||||
import 'package:topic/PersonalInfo.dart';
|
||||
import 'package:topic/KnowledgePage.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
|
||||
class MessagePage extends StatefulWidget {
|
||||
final String email; // 接收來自上個頁面的 email
|
||||
|
||||
MessagePage({required this.email});
|
||||
|
||||
@override
|
||||
_MessagePageState createState() => _MessagePageState();
|
||||
}
|
||||
|
||||
class _MessagePageState extends State<MessagePage> {
|
||||
List<Map<String, dynamic>> _results = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fetchData(); // 連接資料庫並抓取資料
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
print('connect');
|
||||
|
||||
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 eName, eGender FROM Elder');
|
||||
print('Result: ${result.length} rows found.');
|
||||
|
||||
if (result.rows.isEmpty) {
|
||||
print('No data found in users table.');
|
||||
} else {
|
||||
setState(() {
|
||||
_results = result.rows.map((row) => {
|
||||
'name': row.colAt(0),
|
||||
'gender': row.colAt(1),
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
/*appBar: AppBar(
|
||||
title: Text('MySQL Data'), // 顯示標題
|
||||
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.builder(
|
||||
padding: EdgeInsets.symmetric(vertical: 5), // 调整列表视图的 padding
|
||||
itemCount: _results.length, // 列表項目數量
|
||||
itemBuilder: (context, index) {
|
||||
final user = _results[index];
|
||||
return ListTile(
|
||||
title: Text(user['name']), // 顯示名稱
|
||||
subtitle: Text('gender: ${user['gender']}'), // 顯示性別
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: [
|
||||
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.history_edu), label: '跌倒紀錄'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.lightbulb_outline), label: '知識補充'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.monitor_outlined), label: '切換畫面'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.person_sharp), label: '個人資料'),
|
||||
],
|
||||
currentIndex: 3, // 當前選中的索引
|
||||
selectedItemColor: Colors.orange,
|
||||
unselectedItemColor: Colors.grey,
|
||||
onTap: (index) {
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HomePage(email: widget.email)),
|
||||
);
|
||||
} else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HistoricalRecord(email: widget.email)),
|
||||
);
|
||||
} else if (index == 2) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => KnowledgePage(email: widget.email)),
|
||||
);
|
||||
} else if (index == 3) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => MessagePage(email: widget.email)),
|
||||
);
|
||||
} else if (index == 4) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => PersonalInfo(email: widget.email)),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
274
lib/PersonalInfo.dart
Normal file
274
lib/PersonalInfo.dart
Normal file
@@ -0,0 +1,274 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:topic/main.dart';
|
||||
import 'package:topic/HomePage.dart';
|
||||
import 'package:topic/HistoricalRecord.dart';
|
||||
import 'package:topic/KnowledgePage.dart';
|
||||
import 'package:topic/MessagePage.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
/*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 = '', _age = '',_gender = '',_email = '';
|
||||
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: 3306,
|
||||
userName: 'root',
|
||||
//password: 'Topic@2024',
|
||||
password: '0000',
|
||||
//databaseName: 'care', //testdb
|
||||
databaseName: 'testdb',
|
||||
);
|
||||
|
||||
await conn.connect();
|
||||
|
||||
try {
|
||||
print('ok');
|
||||
var result = await conn.execute('SELECT name, phone, age, gender, email FROM users WHERE email = :email',
|
||||
{'email': widget.email}, // 傳入參數 email
|
||||
);
|
||||
if (result.rows.isNotEmpty) {//有資料
|
||||
var row = result.rows.first;
|
||||
setState(() {
|
||||
_name = row.colAt(0)??'';//如果沒有資料就是空直
|
||||
_phone = row.colAt(1)??'';
|
||||
_age = row.colAt(2)??'';
|
||||
_gender = row.colAt(3)??'';
|
||||
_email = row.colAt(4)??'';
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleEdit() {
|
||||
setState(() {
|
||||
_isEditing = !_isEditing;
|
||||
});
|
||||
}
|
||||
|
||||
void _saveChanges() async {
|
||||
final conn = await MySQLConnection.createConnection(
|
||||
host: '10.0.2.2',
|
||||
port: 3306,
|
||||
userName: 'root',
|
||||
password: '0000',
|
||||
databaseName: 'testdb',
|
||||
);
|
||||
|
||||
await conn.connect();
|
||||
|
||||
try {
|
||||
await conn.execute(
|
||||
'UPDATE users SET email = :email WHERE email = :old_email',
|
||||
{'email': _emailController.text, 'old_email': widget.email},
|
||||
);
|
||||
setState(() {
|
||||
_email = _emailController.text;
|
||||
_isEditing = false; // 退出編輯
|
||||
});
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//頁面
|
||||
@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(_age),
|
||||
//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('EMAIL'),
|
||||
subtitle: //Text(_email),
|
||||
_isEditing
|
||||
? TextField(
|
||||
controller: _emailController,
|
||||
autofocus: true,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_email = value;
|
||||
});
|
||||
},
|
||||
)
|
||||
: Text(_email),
|
||||
//trailing: Icon(Icons.arrow_forward_ios),
|
||||
|
||||
onTap: () {
|
||||
_toggleEdit(); // 切換到編輯狀態
|
||||
},
|
||||
),
|
||||
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: () {
|
||||
},
|
||||
child: Text('登出'),
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: Color(0xFFF5E3C3),
|
||||
textStyle: TextStyle(fontSize: 18),
|
||||
shadowColor: Colors.transparent,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: [
|
||||
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.history_edu), label: '跌倒紀錄'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.lightbulb_outline), label: '知識補充'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.monitor_outlined), label: '切換畫面'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.person_sharp), label: '個人資料'),
|
||||
],
|
||||
currentIndex: 4,
|
||||
selectedItemColor: Colors.orange,
|
||||
unselectedItemColor: Colors.grey,
|
||||
onTap: (index) {
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HomePage(
|
||||
email: _email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => HistoricalRecord(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
else if (index == 2) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => KnowledgePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
else if (index == 3) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => MessagePage(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
else if (index == 4) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => PersonalInfo(
|
||||
email: widget.email,
|
||||
)),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
306
lib/RegisterPage.dart
Normal file
306
lib/RegisterPage.dart
Normal file
@@ -0,0 +1,306 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:mailer/mailer.dart';
|
||||
import 'package:mailer/smtp_server.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: RegisterPage(),
|
||||
));
|
||||
}
|
||||
|
||||
class RegisterPage extends StatefulWidget {
|
||||
@override
|
||||
_RegisterPageState createState() => _RegisterPageState();
|
||||
}
|
||||
|
||||
class _RegisterPageState extends State<RegisterPage> {
|
||||
final TextEditingController _nameController = TextEditingController();
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
final TextEditingController _codeController = TextEditingController();
|
||||
|
||||
bool _isButtonEnabled = true;
|
||||
int _seconds = 60;
|
||||
Timer? _timer;
|
||||
String _generatedCode = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fetchData();
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
final conn = await MySQLConnection.createConnection(
|
||||
host: '203.64.84.154',
|
||||
port: 33061,
|
||||
userName: 'root',
|
||||
password: 'Topic@2024',
|
||||
databaseName: 'care',
|
||||
);
|
||||
await conn.connect();
|
||||
}
|
||||
|
||||
void _startTimer() {
|
||||
setState(() {
|
||||
_isButtonEnabled = false;
|
||||
});
|
||||
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
||||
setState(() {
|
||||
if (_seconds > 0) {
|
||||
_seconds--;
|
||||
} else {
|
||||
_timer?.cancel();
|
||||
_isButtonEnabled = true;
|
||||
_seconds = 60;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
String _generateVerificationCode() {
|
||||
final random = Random();
|
||||
const availableChars = '0123456789';
|
||||
return List.generate(
|
||||
6, (index) => availableChars[random.nextInt(availableChars.length)])
|
||||
.join();
|
||||
}
|
||||
|
||||
Future<void> _sendEmail(String recipientEmail, String code) async {
|
||||
final smtpServer = SmtpServer(
|
||||
'smtp.gmail.com',
|
||||
port: 587,
|
||||
username: 'kueikuei8011@gmail.com',
|
||||
password: 'yqns onwf tydq obzl',
|
||||
ssl: false,
|
||||
allowInsecure: false,
|
||||
);
|
||||
|
||||
final message = Message()
|
||||
..from = Address('kueikuei8011@gmail.com', 'Kuei')
|
||||
..recipients.add(recipientEmail)
|
||||
..subject = '您的驗證碼'
|
||||
..text = '您的驗證碼是: $code';
|
||||
|
||||
try {
|
||||
await send(message, smtpServer);
|
||||
print('驗證碼發送成功');
|
||||
} catch (e) {
|
||||
print('驗證碼發送失敗: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _sendVerificationCode() {
|
||||
final email = _emailController.text;
|
||||
if (email.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('請輸入電子信箱地址')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
_generatedCode = _generateVerificationCode();
|
||||
_sendEmail(email, _generatedCode);
|
||||
_startTimer();
|
||||
}
|
||||
|
||||
void registerBtn() async {
|
||||
// TODO: 驗證碼未實作驗證
|
||||
final conn = await MySQLConnection.createConnection(
|
||||
host: '10.0.2.2',
|
||||
port: 3306,
|
||||
userName: 'root',
|
||||
password: '0000',
|
||||
databaseName: 'testdb',
|
||||
);
|
||||
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',
|
||||
{'email': email, 'password': password},
|
||||
);
|
||||
|
||||
if (result.rows.isNotEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('登入失敗:帳號或密碼錯誤')),
|
||||
);
|
||||
} else {
|
||||
await conn.execute(
|
||||
'INSERT INTO users (name, email, password) VALUES (:name, :email, :password)',
|
||||
{'name': name, 'email': email, 'password': password},
|
||||
);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('註冊成功!')),
|
||||
);
|
||||
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => VerifyPage(email: email),
|
||||
),
|
||||
);
|
||||
|
||||
_nameController.clear();
|
||||
_emailController.clear();
|
||||
_passwordController.clear();
|
||||
}
|
||||
} catch (e) {
|
||||
print('資料庫錯誤: $e');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('登入失敗:系統錯誤')),
|
||||
);
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return 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,
|
||||
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: _nameController,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.person_outlined),
|
||||
labelText: '用戶姓名',
|
||||
),
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
TextField(
|
||||
controller: _emailController,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.email_outlined),
|
||||
labelText: '電子信箱',
|
||||
),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
TextField(
|
||||
controller: _passwordController,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.lock_outlined),
|
||||
labelText: '密碼',
|
||||
),
|
||||
obscureText: true,
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _codeController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '驗證碼',
|
||||
hintText: '填寫驗證碼',
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 10),
|
||||
ElevatedButton(
|
||||
onPressed:
|
||||
_isButtonEnabled ? _sendVerificationCode : null,
|
||||
child: _isButtonEnabled
|
||||
? Text('發送驗證碼')
|
||||
: Text('重新發送(${_seconds})'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _isButtonEnabled
|
||||
? Color(0xFF4FC3F7)
|
||||
: Colors.grey,
|
||||
padding: EdgeInsets.symmetric(horizontal: 16),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: registerBtn,
|
||||
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.pop(context);
|
||||
},
|
||||
child: Text('取消註冊'),
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: Colors.transparent,
|
||||
textStyle: TextStyle(fontSize: 18),
|
||||
shadowColor: Colors.transparent,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class VerifyPage extends StatelessWidget {
|
||||
final String email;
|
||||
|
||||
VerifyPage({required this.email});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Verify Email'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text('Email: $email'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
148
lib/TryPage.dart
Normal file
148
lib/TryPage.dart
Normal file
@@ -0,0 +1,148 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:topic/HomePage.dart';
|
||||
void main() {
|
||||
runApp(TryPage());
|
||||
}
|
||||
|
||||
class TryPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('MySQL Data'),
|
||||
),
|
||||
body: MySqlData(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
class MySqlData extends StatefulWidget {
|
||||
@override
|
||||
_MySqlDataState createState() => _MySqlDataState();
|
||||
}
|
||||
|
||||
class _MySqlDataState extends State<MySqlData> {
|
||||
List<Map<String, dynamic>> _results = [];
|
||||
|
||||
@override
|
||||
void initState() {//初始化
|
||||
super.initState();
|
||||
_fetchData();//連資料庫
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
print('connect');
|
||||
|
||||
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 eName, eGender FROM Elder');
|
||||
print('Result: ${result.length} rows found.');
|
||||
|
||||
if (result.rows.isEmpty) {//null
|
||||
print('No data found in users table.');
|
||||
}
|
||||
else {
|
||||
setState(() {
|
||||
_results = result.rows.map((row) =>
|
||||
{
|
||||
'name': row.colAt(0),
|
||||
'gender': row.colAt(1)
|
||||
} //colat的參數是看select進來的參數
|
||||
).toList();
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
print('Error: $e');
|
||||
}
|
||||
finally{
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
//頁面
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: _results.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = _results[index];
|
||||
return ListTile(
|
||||
title: Text(user['name']),
|
||||
subtitle: Text('gender: ${user['gender']}'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//server=203.64.84.154;database=care;uid=root;password=Topic@2024;port = 33061";
|
||||
/*
|
||||
// 创建数据库连接对象
|
||||
final conn = await Connection.connect(settings);
|
||||
|
||||
// 执行SQL查询语句
|
||||
final result = await conn.query('SELECT * FROM users');
|
||||
|
||||
// 处理查询结果
|
||||
for (final row in result) {
|
||||
print('${row[0]} - ${row[1]} - ${row[2]}');
|
||||
}
|
||||
|
||||
// 执行SQL插入语句
|
||||
final insertResult = await conn.execute('INSERT INTO users (name, age) VALUES (?, ?)', ['John', 30]);
|
||||
|
||||
// 处理插入结果
|
||||
print('Inserted rows: ${insertResult.affectedRows}');
|
||||
|
||||
// 执行SQL更新语句
|
||||
final updateResult = await conn.execute('UPDATE users SET age = ? WHERE name = ?', [35, 'John']);
|
||||
|
||||
// 处理更新结果
|
||||
print('Updated rows: ${updateResult.affectedRows}');
|
||||
|
||||
// 执行SQL删除语句
|
||||
final deleteResult = await conn.execute('DELETE FROM users WHERE name = ?', ['John']);
|
||||
|
||||
// 处理删除结果
|
||||
print('Deleted rows: ${deleteResult.affectedRows}');
|
||||
|
||||
// 关闭数据库连接
|
||||
conn.close();
|
||||
}*/
|
||||
206
lib/main.dart
Normal file
206
lib/main.dart
Normal file
@@ -0,0 +1,206 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:topic/HomePage.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'package:topic/RegisterPage.dart';
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: LoginPage(),
|
||||
));
|
||||
}
|
||||
|
||||
class LoginPage extends StatefulWidget {//ful會改變
|
||||
@override
|
||||
_LoginPageState createState() => _LoginPageState();
|
||||
}
|
||||
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _ageController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {//初始化
|
||||
super.initState();
|
||||
_fetchData();//連資料庫
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
//MYSQL
|
||||
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();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
debugPaintSizeEnabled=false;
|
||||
return 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),
|
||||
labelText: '密碼',
|
||||
),
|
||||
obscureText: true,
|
||||
),//密碼
|
||||
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',
|
||||
// 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 {
|
||||
// 獲取用戶輸入的帳號和密碼
|
||||
String email = _emailController.text;
|
||||
String password = _ageController.text;
|
||||
|
||||
print('输入的信箱: $email');
|
||||
print('输入的密碼: $password');
|
||||
|
||||
// 查詢數據庫,检查是否有匹配的帳號
|
||||
var result = await conn.execute(
|
||||
'SELECT * FROM HomeLogin WHERE homeEmail = :email AND homePassword = :password',
|
||||
{'email': email, 'password': password},
|
||||
);
|
||||
|
||||
print('查詢結果行數: ${result.rows.length}');
|
||||
|
||||
if (result.rows.isNotEmpty) {
|
||||
_emailController.clear();
|
||||
_ageController.clear();
|
||||
// 如果找到匹配的帳號,登入成功,跳轉到主頁
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => HomePage(
|
||||
email: email,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// 没有找到匹配的帳號,提示登入失敗
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('登入失敗:帳號或密碼錯誤')),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print('資料庫錯誤: $e');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('登入失敗:系統錯誤')),
|
||||
);
|
||||
} finally {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
28
lib/testdb.sql
Normal file
28
lib/testdb.sql
Normal file
@@ -0,0 +1,28 @@
|
||||
-- 创建数据库
|
||||
CREATE DATABASE IF NOT EXISTS testdb;
|
||||
|
||||
-- 使用创建的数据库
|
||||
USE testdb;
|
||||
|
||||
-- 创建表
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100),
|
||||
age INT,
|
||||
gender INT,
|
||||
phone VARCHAR(10),
|
||||
email VARCHAR(100),
|
||||
password VARCHAR(100)
|
||||
);
|
||||
drop table IF EXISTS users;
|
||||
|
||||
-- 插入一些测试数据
|
||||
INSERT INTO users (name, age,gender,phone,email) VALUES ('Alice', 30,1,09123,'alice@gmail.com');
|
||||
INSERT INTO users (name, age,gender,phone,email) VALUES ('Bob', 25,2,09456,'bob@gmail.com');
|
||||
INSERT INTO users (name, age,gender,phone,email) VALUES ('Charlie', 35,1,09789,'charlie@gmail.com');
|
||||
INSERT INTO users (name, age, gender, phone, email, password) VALUES ('David', 28, 1, '0923456789', 'david@gmail.com', 'password4');
|
||||
INSERT INTO users (name, age, gender, phone, email, password) VALUES ('Eve', 22, 2, '0956789123', 'eve@gmail.com', 'password5');
|
||||
|
||||
SELECT * FROM testdb.users;
|
||||
SHOW GRANTS FOR 'root'@'localhost';
|
||||
table users;
|
||||
Reference in New Issue
Block a user