Skip to content

Commit

Permalink
Merge pull request #33 from yihong1120/feature/dashboard-display
Browse files Browse the repository at this point in the history
Feature/dashboard display
  • Loading branch information
yihong1120 authored Dec 31, 2023
2 parents ee5ac78 + f9b4f14 commit 4041a45
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/components/media_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MediaPreview extends StatelessWidget {
final List<XFile> mediaFiles;
final Function(XFile) onRemove;

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) {
Expand All @@ -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),
),
],
Expand Down
5 changes: 3 additions & 2 deletions lib/models/traffic_violation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TrafficViolation {
String? location;
String? officer;

static const List<String> VIOLATIONS = [
static const List<String> violations = [
'紅線停車',
'黃線臨車',
'行駛人行道',
Expand All @@ -26,7 +26,8 @@ class TrafficViolation {
'其他',
];

static const List<String> STATUS = [
// 將靜態常量列表重命名為 statusOptions
static const List<String> statusOptions = [
'Pending',
'Approved',
'Rejected',
Expand Down
30 changes: 15 additions & 15 deletions lib/screens/accounts/account_delete_confirm_page.dart
Original file line number Diff line number Diff line change
@@ -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(
SnackBar(content: Text('Failed to delete account')),
);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -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')),
);
}
}
}
14 changes: 10 additions & 4 deletions lib/screens/accounts/account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ class _AccountPageState extends State<AccountPage> {
}

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
Expand Down
14 changes: 9 additions & 5 deletions lib/screens/accounts/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ class _LoginPageState extends State<LoginPage> {
_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')),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/accounts/password_change_done_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class _PasswordChangeDonePageState extends State<PasswordChangeDonePage> {
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(
Expand Down
20 changes: 12 additions & 8 deletions lib/screens/accounts/password_change_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ class _PasswordChangePageState extends State<PasswordChangePage> {
_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(
SnackBar(content: Text('Password changed successfully')),
scaffoldMessenger.showSnackBar(
const SnackBar(content: Text('Password changed successfully')),
);
Navigator.of(context).pop();
navigator.pop();
} else {
// 如果密碼更改失敗,顯示錯誤消息
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to change password')),
scaffoldMessenger.showSnackBar(
const SnackBar(content: Text('Failed to change password')),
);
}

Expand Down
16 changes: 9 additions & 7 deletions lib/screens/accounts/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ class _RegisterPageState extends State<RegisterPage> {

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')),
);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/screens/accounts/verify_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ class _VerifyPageState extends State<VerifyPage> {
_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')),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/map/home_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _HomeMapScreenState extends State<HomeMapScreen> {
late GoogleMapController mapController;
final LatLng _center = const LatLng(23.6978, 120.9605);
String _searchKeyword = '';
Set<Marker> _markers = {};
final Set<Marker> _markers = {};

@override
void initState() {
Expand Down
19 changes: 11 additions & 8 deletions lib/screens/reports/create_report_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CreateReportPage extends StatefulWidget {
class _CreateReportPageState extends State<CreateReportPage> {
final _formKey = GlobalKey<FormState>();
final _picker = ImagePicker();
List<XFile> _mediaFiles = [];
final List<XFile> _mediaFiles = [];
final TrafficViolation _violation = TrafficViolation(
date: DateTime.now(),
time: TimeOfDay.now(),
Expand Down Expand Up @@ -142,7 +142,7 @@ class _CreateReportPageState extends State<CreateReportPage> {
DropdownButtonFormField<String>(
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),
Expand Down Expand Up @@ -190,12 +190,10 @@ class _CreateReportPageState extends State<CreateReportPage> {
}

void _pickMedia() async {
final List<XFile>? pickedFiles = await _picker.pickMultiImage();
if (pickedFiles != null) {
setState(() {
_mediaFiles.addAll(pickedFiles);
});
}
final List<XFile> pickedFiles = await _picker.pickMultiImage();
setState(() {
_mediaFiles.addAll(pickedFiles);
});
}

Widget _buildMediaPreview() {
Expand Down Expand Up @@ -224,8 +222,13 @@ class _CreateReportPageState extends State<CreateReportPage> {
}

void _submitReport() async {
// 获取 context 依赖的信息
final reportService = Provider.of<ReportService>(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);
Expand Down
4 changes: 2 additions & 2 deletions lib/screens/reports/edit_report_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class EditReportPage extends StatefulWidget {
class _EditReportPageState extends State<EditReportPage> {
late TrafficViolation _violation;
final ImagePicker _picker = ImagePicker();
List<XFile> _mediaFiles = [];
bool _isLoading = true;
final List<XFile> _mediaFiles = [];
final bool _isLoading = true;

@override
void initState() {
Expand Down

0 comments on commit 4041a45

Please sign in to comment.