103 lines
3.1 KiB
Dart
103 lines
3.1 KiB
Dart
import 'package:flisp_app/layout/app_body.dart';
|
|
import 'package:flisp_app/layout/app_drawer.dart';
|
|
import 'package:flisp_app/layout/app_floating_button.dart';
|
|
import 'package:flisp_app/layout/app_navbar.dart';
|
|
import 'package:flisp_app/models/calendar.dart';
|
|
import 'package:flisp_app/models/todo.dart';
|
|
import 'package:flisp_app/provider/app_provider.dart';
|
|
import 'package:flisp_app/service/calendar_service.dart';
|
|
import 'package:flisp_app/service/todo_service.dart';
|
|
import 'package:flisp_app/utils/notify_utils.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_common/utils/date_utils.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class MainScreen extends StatefulWidget {
|
|
const MainScreen({super.key});
|
|
|
|
@override
|
|
State<MainScreen> createState() => _MainScreenState();
|
|
}
|
|
|
|
class _MainScreenState extends State<MainScreen> {
|
|
final GlobalKey<AppBodyState> _appBodyKey = GlobalKey();
|
|
final NotifyService notifyService = NotifyService();
|
|
|
|
void _onPressedFloatingButton(int index) {
|
|
_appBodyKey.currentState?.showPageAddDialog(index);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
refreshScheduled();
|
|
}
|
|
|
|
/// 重新刷新提醒 防止每次重启设备后提醒丢失的问题
|
|
void refreshScheduled() async {
|
|
await notifyService.cancelAllNotifications();
|
|
|
|
TodoService todoService = TodoService();
|
|
final List<Todo> todoResult = todoService.getAllTodos(
|
|
TodoSortMode.priority,
|
|
);
|
|
final List<Todo> todayTodos =
|
|
todoResult
|
|
.where(
|
|
(todo) => !todo.isCompleted && isAfterToday(todo.scheduledTime),
|
|
)
|
|
.toList();
|
|
|
|
for (Todo todo in todayTodos) {
|
|
await notifyService.scheduleNotification(
|
|
id: todo.id,
|
|
title: '待办提醒',
|
|
body: todo.title,
|
|
scheduledTime: todo.scheduledTime!,
|
|
);
|
|
}
|
|
|
|
CalendarService calendarService = CalendarService();
|
|
final List<Calendar> calendarResult = calendarService.getAllCalendars();
|
|
final List<Calendar> todayCalendars =
|
|
calendarResult
|
|
.where((item) => isAfterToday(item.scheduledTime))
|
|
.toList();
|
|
|
|
for (Calendar calendar in todayCalendars) {
|
|
await notifyService.scheduleNotification(
|
|
id: calendar.id,
|
|
title: '日程提醒',
|
|
body: calendar.title,
|
|
scheduledTime: calendar.scheduledTime!,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
|
|
return Scaffold(
|
|
drawer: const AppDrawer(),
|
|
appBar: AppBar(
|
|
title: Text(provider.currentAppBarTitle),
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
foregroundColor: Theme.of(context).colorScheme.onPrimary,
|
|
elevation: 0,
|
|
),
|
|
body: Padding(
|
|
padding: EdgeInsets.all(10),
|
|
child: AppBody(key: _appBodyKey),
|
|
),
|
|
bottomNavigationBar: AppNavbar(),
|
|
floatingActionButton:
|
|
provider.currentTab == 3
|
|
? null
|
|
: AppFloatingButton(
|
|
onPressed: () => _onPressedFloatingButton(provider.currentTab),
|
|
),
|
|
);
|
|
}
|
|
}
|