2024-07-19 02:14:58 +08:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/providers.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
2024-11-09 20:17:57 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'widgets/start_button.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class DashboardFragment extends ConsumerStatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
const DashboardFragment({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
2025-02-09 18:39:38 +08:00
|
|
|
ConsumerState<DashboardFragment> createState() => _DashboardFragmentState();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class _DashboardFragmentState extends ConsumerState<DashboardFragment>
|
|
|
|
|
with PageMixin {
|
2024-12-09 01:40:39 +08:00
|
|
|
final key = GlobalKey<SuperGridState>();
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
@override
|
|
|
|
|
initState() {
|
|
|
|
|
ref.listenManual(
|
|
|
|
|
isCurrentPageProvider(PageLabel.dashboard),
|
|
|
|
|
(prev, next) {
|
|
|
|
|
if (prev != next && next == true) {
|
|
|
|
|
initPageState();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fireImmediately: true,
|
|
|
|
|
);
|
|
|
|
|
return super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget? get floatingActionButton => const StartButton();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
List<Widget> get actions => [
|
2024-12-09 01:40:39 +08:00
|
|
|
ValueListenableBuilder(
|
|
|
|
|
valueListenable: key.currentState!.addedChildrenNotifier,
|
|
|
|
|
builder: (_, addedChildren, child) {
|
|
|
|
|
return ValueListenableBuilder(
|
|
|
|
|
valueListenable: key.currentState!.isEditNotifier,
|
|
|
|
|
builder: (_, isEdit, child) {
|
|
|
|
|
if (!isEdit || addedChildren.isEmpty) {
|
|
|
|
|
return Container();
|
|
|
|
|
}
|
|
|
|
|
return child!;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
key.currentState!.showAddModal();
|
|
|
|
|
},
|
|
|
|
|
icon: Icon(
|
|
|
|
|
Icons.add_circle,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: ValueListenableBuilder(
|
|
|
|
|
valueListenable: key.currentState!.isEditNotifier,
|
|
|
|
|
builder: (_, isEdit, ___) {
|
|
|
|
|
return isEdit
|
|
|
|
|
? Icon(Icons.save)
|
|
|
|
|
: Icon(
|
|
|
|
|
Icons.edit,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
key.currentState!.isEditNotifier.value =
|
|
|
|
|
!key.currentState!.isEditNotifier.value;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
];
|
2025-02-09 18:39:38 +08:00
|
|
|
|
|
|
|
|
_handleSave(List<GridItem> girdItems, WidgetRef ref) {
|
|
|
|
|
final dashboardWidgets = girdItems
|
|
|
|
|
.map(
|
|
|
|
|
(item) => DashboardWidget.getDashboardWidget(item),
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
|
|
|
|
ref.read(appSettingProvider.notifier).updateState(
|
|
|
|
|
(state) => state.copyWith(dashboardWidgets: dashboardWidgets),
|
|
|
|
|
);
|
2024-10-27 16:59:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-02-09 18:39:38 +08:00
|
|
|
final dashboardState = ref.watch(dashboardStateProvider);
|
|
|
|
|
final columns = max(4 * ((dashboardState.viewWidth / 350).ceil()), 8);
|
|
|
|
|
return Align(
|
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
padding: const EdgeInsets.all(16).copyWith(
|
|
|
|
|
bottom: 88,
|
|
|
|
|
),
|
|
|
|
|
child: SuperGrid(
|
|
|
|
|
key: key,
|
|
|
|
|
crossAxisCount: columns,
|
|
|
|
|
crossAxisSpacing: 16,
|
|
|
|
|
mainAxisSpacing: 16,
|
|
|
|
|
children: [
|
|
|
|
|
...dashboardState.dashboardWidgets
|
|
|
|
|
.where(
|
|
|
|
|
(item) => item.platforms.contains(
|
|
|
|
|
SupportPlatform.currentPlatform,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.map(
|
|
|
|
|
(item) => item.widget,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
onSave: (girdItems) {
|
|
|
|
|
_handleSave(girdItems, ref);
|
|
|
|
|
},
|
|
|
|
|
addedItemsBuilder: (girdItems) {
|
|
|
|
|
return DashboardWidget.values
|
|
|
|
|
.where(
|
|
|
|
|
(item) =>
|
|
|
|
|
!girdItems.contains(item.widget) &&
|
|
|
|
|
item.platforms.contains(
|
|
|
|
|
SupportPlatform.currentPlatform,
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
)
|
|
|
|
|
.map((item) => item.widget)
|
|
|
|
|
.toList();
|
|
|
|
|
},
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
2024-05-20 15:15:09 +08:00
|
|
|
),
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|