From 915f9bf8063ce0cfdb613971bfaa04dc58fcab71 Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Sun, 31 Dec 2023 21:21:11 +0800 Subject: [PATCH 1/4] Add const to boost performance --- lib/components/media_preview.dart | 4 ++-- lib/screens/accounts/account_delete_confirm_page.dart | 2 +- lib/screens/accounts/password_change_done_page.dart | 2 +- lib/screens/accounts/password_change_page.dart | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/components/media_preview.dart b/lib/components/media_preview.dart index 9dd5fda..9428d52 100644 --- a/lib/components/media_preview.dart +++ b/lib/components/media_preview.dart @@ -5,7 +5,7 @@ class MediaPreview extends StatelessWidget { final List mediaFiles; final Function(XFile) onRemove; - MediaPreview({Key? key, required this.mediaFiles, required this.onRemove}) : super(key: key); + const MediaPreview({Key? key, required this.mediaFiles, required this.onRemove}) : super(key: key); @override Widget build(BuildContext context) { @@ -20,7 +20,7 @@ class MediaPreview extends StatelessWidget { // For example: // Image.file(File(file.path), width: 100, height: 100), IconButton( - icon: Icon(Icons.remove_circle), + icon: const Icon(Icons.remove_circle), onPressed: () => onRemove(file), ), ], diff --git a/lib/screens/accounts/account_delete_confirm_page.dart b/lib/screens/accounts/account_delete_confirm_page.dart index 8820df2..83260f7 100644 --- a/lib/screens/accounts/account_delete_confirm_page.dart +++ b/lib/screens/accounts/account_delete_confirm_page.dart @@ -13,7 +13,7 @@ class AccountDeleteConfirmPage extends StatelessWidget { } else { // 如果刪除失敗,顯示錯誤消息 ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Failed to delete account')), + const SnackBar(content: Text('Failed to delete account')), ); } } diff --git a/lib/screens/accounts/password_change_done_page.dart b/lib/screens/accounts/password_change_done_page.dart index 19959de..3f1c6b0 100644 --- a/lib/screens/accounts/password_change_done_page.dart +++ b/lib/screens/accounts/password_change_done_page.dart @@ -53,7 +53,7 @@ class _PasswordChangeDonePageState extends State { const SizedBox(height: 20), Text( 'You will be redirected to your account page in $_countdown seconds.', - style: TextStyle(fontSize: 16), + style: const TextStyle(fontSize: 16), textAlign: TextAlign.center, ), TextButton( diff --git a/lib/screens/accounts/password_change_page.dart b/lib/screens/accounts/password_change_page.dart index 16e5b26..c3da842 100644 --- a/lib/screens/accounts/password_change_page.dart +++ b/lib/screens/accounts/password_change_page.dart @@ -28,13 +28,13 @@ class _PasswordChangePageState extends State { if (passwordChanged) { // 如果密碼更改成功,顯示成功消息並導航回帳戶頁面 ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Password changed successfully')), + const SnackBar(content: Text('Password changed successfully')), ); Navigator.of(context).pop(); } else { // 如果密碼更改失敗,顯示錯誤消息 ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Failed to change password')), + const SnackBar(content: Text('Failed to change password')), ); } From 58611e7f81883f20d620d5d7cc909cb263b7efcc Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Mon, 1 Jan 2024 02:26:27 +0800 Subject: [PATCH 2/4] Add const and final to boost performance --- lib/components/media_preview.dart | 2 +- lib/screens/map/home_map.dart | 2 +- lib/screens/reports/edit_report_screen.dart | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/components/media_preview.dart b/lib/components/media_preview.dart index 9428d52..f88b9bb 100644 --- a/lib/components/media_preview.dart +++ b/lib/components/media_preview.dart @@ -5,7 +5,7 @@ class MediaPreview extends StatelessWidget { final List mediaFiles; final Function(XFile) onRemove; - const MediaPreview({Key? key, required this.mediaFiles, required this.onRemove}) : super(key: key); + const MediaPreview({super.key, required this.mediaFiles, required this.onRemove}); @override Widget build(BuildContext context) { diff --git a/lib/screens/map/home_map.dart b/lib/screens/map/home_map.dart index 526d447..d909c40 100644 --- a/lib/screens/map/home_map.dart +++ b/lib/screens/map/home_map.dart @@ -13,7 +13,7 @@ class _HomeMapScreenState extends State { late GoogleMapController mapController; final LatLng _center = const LatLng(23.6978, 120.9605); String _searchKeyword = ''; - Set _markers = {}; + final Set _markers = {}; @override void initState() { diff --git a/lib/screens/reports/edit_report_screen.dart b/lib/screens/reports/edit_report_screen.dart index da33dac..33bb973 100644 --- a/lib/screens/reports/edit_report_screen.dart +++ b/lib/screens/reports/edit_report_screen.dart @@ -18,8 +18,8 @@ class EditReportPage extends StatefulWidget { class _EditReportPageState extends State { late TrafficViolation _violation; final ImagePicker _picker = ImagePicker(); - List _mediaFiles = []; - bool _isLoading = true; + final List _mediaFiles = []; + final bool _isLoading = true; @override void initState() { From 47f27cd7dfb2ae5e29b923929627a5cbbd4f9a98 Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Mon, 1 Jan 2024 02:43:11 +0800 Subject: [PATCH 3/4] Rename the parametres --- lib/models/traffic_violation.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/models/traffic_violation.dart b/lib/models/traffic_violation.dart index 059d010..08fd4fd 100644 --- a/lib/models/traffic_violation.dart +++ b/lib/models/traffic_violation.dart @@ -11,7 +11,7 @@ class TrafficViolation { String? location; String? officer; - static const List VIOLATIONS = [ + static const List violations = [ '紅線停車', '黃線臨車', '行駛人行道', @@ -26,7 +26,8 @@ class TrafficViolation { '其他', ]; - static const List STATUS = [ + // 將靜態常量列表重命名為 statusOptions + static const List statusOptions = [ 'Pending', 'Approved', 'Rejected', From f9b4f142eff3a2bbff46e81ac43fd941c5113235 Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Mon, 1 Jan 2024 02:45:14 +0800 Subject: [PATCH 4/4] Refactor TrafficViolation class to resolve naming conflicts and improve JSON parsing --- .../accounts/account_delete_confirm_page.dart | 30 +++++++++---------- lib/screens/accounts/account_page.dart | 14 ++++++--- lib/screens/accounts/login.dart | 14 +++++---- .../accounts/password_change_page.dart | 16 ++++++---- lib/screens/accounts/register.dart | 16 +++++----- lib/screens/accounts/verify_page.dart | 14 +++++---- lib/screens/reports/create_report_screen.dart | 19 +++++++----- 7 files changed, 73 insertions(+), 50 deletions(-) diff --git a/lib/screens/accounts/account_delete_confirm_page.dart b/lib/screens/accounts/account_delete_confirm_page.dart index 83260f7..e789da9 100644 --- a/lib/screens/accounts/account_delete_confirm_page.dart +++ b/lib/screens/accounts/account_delete_confirm_page.dart @@ -1,23 +1,9 @@ import 'package:flutter/material.dart'; -import '../../services/auth_service.dart'; // 假設您有一個處理身份驗證的服務 +import '../../services/auth_service.dart'; class AccountDeleteConfirmPage extends StatelessWidget { const AccountDeleteConfirmPage({super.key}); - void _deleteAccount(BuildContext context) async { - // 假設 AuthService 有一個 deleteAccount 方法來處理帳戶刪除邏輯 - bool deleted = await AuthService.deleteAccount(); - if (deleted) { - // 如果刪除成功,導航到登入頁面或其他頁面 - Navigator.of(context).pushReplacementNamed('/login'); - } else { - // 如果刪除失敗,顯示錯誤消息 - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Failed to delete account')), - ); - } - } - @override Widget build(BuildContext context) { return Scaffold( @@ -78,4 +64,18 @@ class AccountDeleteConfirmPage extends StatelessWidget { }, ); } + + void _deleteAccount(BuildContext context) async { + final navigator = Navigator.of(context); + final scaffoldMessenger = ScaffoldMessenger.of(context); + + bool deleted = await AuthService.deleteAccount(); + if (deleted) { + navigator.pushReplacementNamed('/login'); + } else { + scaffoldMessenger.showSnackBar( + const SnackBar(content: Text('Failed to delete account')), + ); + } + } } \ No newline at end of file diff --git a/lib/screens/accounts/account_page.dart b/lib/screens/accounts/account_page.dart index 2e7d267..07c7143 100644 --- a/lib/screens/accounts/account_page.dart +++ b/lib/screens/accounts/account_page.dart @@ -18,13 +18,19 @@ class _AccountPageState extends State { } void _checkLoginStatus() async { - // 假設 AuthService 是您用來管理用戶登入狀態的服務 + // 获取 Navigator 状态 + final navigator = Navigator.of(context); + + // 假设 AuthService 是您用来管理用户登录状态的服务 _isLoggedIn = await AuthService.isLoggedIn(); if (!_isLoggedIn) { - // 如果用戶未登入,導航到登入頁面 - Navigator.of(context).pushReplacementNamed('/login'); + // 如果用户未登录,使用先前获取的 Navigator 状态导航到登录页面 + navigator.pushReplacementNamed('/login'); + } + // 通知 Flutter 需要重建 Widget + if (mounted) { + setState(() {}); } - setState(() {}); } @override diff --git a/lib/screens/accounts/login.dart b/lib/screens/accounts/login.dart index 8a6b5e6..27e39b9 100644 --- a/lib/screens/accounts/login.dart +++ b/lib/screens/accounts/login.dart @@ -22,14 +22,18 @@ class _LoginPageState extends State { _isLoading = true; }); - // 假設 AuthService 有一個 login 方法來處理登入邏輯 + // 在执行异步操作前获取 Navigator 和 ScaffoldMessenger 状态 + final navigator = Navigator.of(context); + final scaffoldMessenger = ScaffoldMessenger.of(context); + + // 执行异步操作 bool loggedIn = await AuthService.login(_username, _password); + + // 根据操作结果使用先前获取的状态 if (loggedIn) { - // 如果登入成功,導航到帳戶頁面 - Navigator.of(context).pushReplacementNamed('/account'); + navigator.pushReplacementNamed('/account'); } else { - // 如果登入失敗,顯示錯誤消息 - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( const SnackBar(content: Text('Invalid username or password')), ); } diff --git a/lib/screens/accounts/password_change_page.dart b/lib/screens/accounts/password_change_page.dart index c3da842..03d79d8 100644 --- a/lib/screens/accounts/password_change_page.dart +++ b/lib/screens/accounts/password_change_page.dart @@ -23,17 +23,21 @@ class _PasswordChangePageState extends State { _isLoading = true; }); - // 假設 AuthService 有一個 changePassword 方法來處理密碼更改邏輯 + // 在执行异步操作前获取 Navigator 和 ScaffoldMessenger 状态 + final navigator = Navigator.of(context); + final scaffoldMessenger = ScaffoldMessenger.of(context); + + // 执行异步操作 bool passwordChanged = await AuthService.changePassword(_oldPassword, _newPassword); + + // 根据操作结果使用先前获取的状态 if (passwordChanged) { - // 如果密碼更改成功,顯示成功消息並導航回帳戶頁面 - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( const SnackBar(content: Text('Password changed successfully')), ); - Navigator.of(context).pop(); + navigator.pop(); } else { - // 如果密碼更改失敗,顯示錯誤消息 - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( const SnackBar(content: Text('Failed to change password')), ); } diff --git a/lib/screens/accounts/register.dart b/lib/screens/accounts/register.dart index 86d2bfd..2b5d82b 100644 --- a/lib/screens/accounts/register.dart +++ b/lib/screens/accounts/register.dart @@ -18,20 +18,22 @@ class _RegisterPageState extends State { void _register() async { if (_formKey.currentState!.validate()) { - _formKey.currentState!.save(); - setState(() { _isLoading = true; }); - // 使用 _password 和 _confirmPassword 作为参数 + // 在执行异步操作前获取 Navigator 和 ScaffoldMessenger 状态 + final navigator = Navigator.of(context); + final scaffoldMessenger = ScaffoldMessenger.of(context); + + // 执行异步操作 bool registered = await AuthService.register(_username, _email, _password, _confirmPassword); + + // 根据操作结果使用先前获取的状态 if (registered) { - // 如果注册成功,导航到登录页面 - Navigator.of(context).pushReplacementNamed('/login'); + navigator.pushReplacementNamed('/login'); } else { - // 如果注册失败,显示错误消息 - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( const SnackBar(content: Text('Registration failed')), ); } diff --git a/lib/screens/accounts/verify_page.dart b/lib/screens/accounts/verify_page.dart index 757a755..a4634b0 100644 --- a/lib/screens/accounts/verify_page.dart +++ b/lib/screens/accounts/verify_page.dart @@ -21,14 +21,18 @@ class _VerifyPageState extends State { _isLoading = true; }); - // 假設 AuthService 有一個 verify 方法來處理帳戶驗證邏輯 + // 在异步操作开始前获取 Navigator 和 ScaffoldMessenger 状态 + final navigator = Navigator.of(context); + final scaffoldMessenger = ScaffoldMessenger.of(context); + + // 执行异步操作 bool verified = await AuthService.verify(_verificationCode); + + // 使用先前获取的状态 if (verified) { - // 如果驗證成功,導航到首頁 - Navigator.of(context).pushReplacementNamed('/home'); + navigator.pushReplacementNamed('/home'); } else { - // 如果驗證失敗,顯示錯誤消息 - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( const SnackBar(content: Text('Invalid verification code')), ); } diff --git a/lib/screens/reports/create_report_screen.dart b/lib/screens/reports/create_report_screen.dart index e508b63..0ef821c 100644 --- a/lib/screens/reports/create_report_screen.dart +++ b/lib/screens/reports/create_report_screen.dart @@ -16,7 +16,7 @@ class CreateReportPage extends StatefulWidget { class _CreateReportPageState extends State { final _formKey = GlobalKey(); final _picker = ImagePicker(); - List _mediaFiles = []; + final List _mediaFiles = []; final TrafficViolation _violation = TrafficViolation( date: DateTime.now(), time: TimeOfDay.now(), @@ -142,7 +142,7 @@ class _CreateReportPageState extends State { DropdownButtonFormField( value: _violation.status, decoration: const InputDecoration(labelText: 'Status'), - items: TrafficViolation.STATUS.map((status) { + items: TrafficViolation.statusOptions.map((status) { return DropdownMenuItem( value: status, child: Text(status), @@ -190,12 +190,10 @@ class _CreateReportPageState extends State { } void _pickMedia() async { - final List? pickedFiles = await _picker.pickMultiImage(); - if (pickedFiles != null) { - setState(() { - _mediaFiles.addAll(pickedFiles); - }); - } + final List pickedFiles = await _picker.pickMultiImage(); + setState(() { + _mediaFiles.addAll(pickedFiles); + }); } Widget _buildMediaPreview() { @@ -224,8 +222,13 @@ class _CreateReportPageState extends State { } void _submitReport() async { + // 获取 context 依赖的信息 final reportService = Provider.of(context, listen: false); + bool success = await reportService.createReport(_violation, _mediaFiles); + + if (!mounted) return; // 检查组件是否仍然挂载 + if (success) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Report submitted successfully'))); Navigator.pop(context);