2025-03-12 17:15:31 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/models/models.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/state.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
|
|
typedef OnSelected = void Function(int index);
|
|
|
|
|
|
|
|
|
|
class HomePage extends StatelessWidget {
|
|
|
|
|
const HomePage({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-03-12 17:15:31 +08:00
|
|
|
return HomeBackScope(
|
2025-02-09 18:39:38 +08:00
|
|
|
child: Consumer(
|
|
|
|
|
builder: (_, ref, child) {
|
|
|
|
|
final state = ref.watch(homeStateProvider);
|
2024-09-18 10:27:53 +08:00
|
|
|
final viewMode = state.viewMode;
|
|
|
|
|
final navigationItems = state.navigationItems;
|
2025-02-09 18:39:38 +08:00
|
|
|
final pageLabel = state.pageLabel;
|
2024-09-18 10:27:53 +08:00
|
|
|
final index = navigationItems.lastIndexWhere(
|
2025-03-05 16:08:50 +08:00
|
|
|
(element) => element.label == pageLabel,
|
2024-09-18 10:27:53 +08:00
|
|
|
);
|
|
|
|
|
final currentIndex = index == -1 ? 0 : index;
|
2024-12-09 01:40:39 +08:00
|
|
|
final navigationBar = CommonNavigationBar(
|
2024-09-18 10:27:53 +08:00
|
|
|
viewMode: viewMode,
|
|
|
|
|
navigationItems: navigationItems,
|
|
|
|
|
currentIndex: currentIndex,
|
|
|
|
|
);
|
|
|
|
|
final bottomNavigationBar =
|
2025-03-05 16:08:50 +08:00
|
|
|
viewMode == ViewMode.mobile ? navigationBar : null;
|
2024-09-18 10:27:53 +08:00
|
|
|
final sideNavigationBar =
|
2025-03-05 16:08:50 +08:00
|
|
|
viewMode != ViewMode.mobile ? navigationBar : null;
|
2024-09-18 10:27:53 +08:00
|
|
|
return CommonScaffold(
|
|
|
|
|
key: globalState.homeScaffoldKey,
|
|
|
|
|
title: Intl.message(
|
2025-02-09 18:39:38 +08:00
|
|
|
pageLabel.name,
|
2024-09-18 10:27:53 +08:00
|
|
|
),
|
|
|
|
|
sideNavigationBar: sideNavigationBar,
|
|
|
|
|
body: child!,
|
|
|
|
|
bottomNavigationBar: bottomNavigationBar,
|
2024-07-24 01:27:49 +08:00
|
|
|
);
|
|
|
|
|
},
|
2025-02-09 18:39:38 +08:00
|
|
|
child: _HomePageView(),
|
2024-07-24 01:27:49 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-09 01:40:39 +08:00
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class _HomePageView extends ConsumerStatefulWidget {
|
|
|
|
|
const _HomePageView();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState createState() => _HomePageViewState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _HomePageViewState extends ConsumerState<_HomePageView> {
|
2025-03-12 17:15:31 +08:00
|
|
|
late PageController _pageController;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_pageController = PageController(
|
|
|
|
|
initialPage: _pageIndex,
|
|
|
|
|
keepPage: true,
|
2025-02-09 18:39:38 +08:00
|
|
|
);
|
2025-03-12 17:15:31 +08:00
|
|
|
ref.listenManual(currentPageLabelProvider, (prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
_toPage(next);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ref.listenManual(currentNavigationsStateProvider, (prev, next) {
|
|
|
|
|
if (prev?.value.length != next.value.length) {
|
|
|
|
|
_updatePageController();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get _pageIndex {
|
|
|
|
|
final navigationItems = ref.read(currentNavigationsStateProvider).value;
|
|
|
|
|
return navigationItems.indexWhere(
|
|
|
|
|
(item) => item.label == globalState.appState.pageLabel,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_toPage(PageLabel pageLabel, [bool ignoreAnimateTo = false]) async {
|
|
|
|
|
if (!mounted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final navigationItems = ref.read(currentNavigationsStateProvider).value;
|
|
|
|
|
final index = navigationItems.indexWhere((item) => item.label == pageLabel);
|
|
|
|
|
if (index == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final isAnimateToPage = ref.read(appSettingProvider).isAnimateToPage;
|
|
|
|
|
final isMobile = ref.read(isMobileViewProvider);
|
|
|
|
|
if (isAnimateToPage && isMobile && !ignoreAnimateTo) {
|
|
|
|
|
await _pageController.animateToPage(
|
|
|
|
|
index,
|
|
|
|
|
duration: kTabScrollDuration,
|
|
|
|
|
curve: Curves.easeOut,
|
2025-02-09 18:39:38 +08:00
|
|
|
);
|
2025-03-12 17:15:31 +08:00
|
|
|
} else {
|
|
|
|
|
_pageController.jumpToPage(index);
|
2025-02-09 18:39:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 17:15:31 +08:00
|
|
|
_updatePageController() {
|
|
|
|
|
final pageLabel = globalState.appState.pageLabel;
|
|
|
|
|
_toPage(pageLabel, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_pageController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
2025-02-09 18:39:38 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final navigationItems = ref.watch(currentNavigationsStateProvider).value;
|
|
|
|
|
return PageView.builder(
|
2025-03-12 17:15:31 +08:00
|
|
|
controller: _pageController,
|
2025-02-09 18:39:38 +08:00
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
|
itemCount: navigationItems.length,
|
2025-04-18 17:50:46 +08:00
|
|
|
// onPageChanged: (index) {
|
|
|
|
|
// debouncer.call(DebounceTag.pageChange, () {
|
|
|
|
|
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
// if (_pageIndex != index) {
|
|
|
|
|
// final pageLabel = navigationItems[index].label;
|
|
|
|
|
// _toPage(pageLabel, true);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
// },
|
2025-02-09 18:39:38 +08:00
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
final navigationItem = navigationItems[index];
|
|
|
|
|
return KeepScope(
|
|
|
|
|
keep: navigationItem.keep,
|
|
|
|
|
key: Key(navigationItem.label.name),
|
|
|
|
|
child: navigationItem.fragment,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CommonNavigationBar extends ConsumerWidget {
|
2024-12-09 01:40:39 +08:00
|
|
|
final ViewMode viewMode;
|
|
|
|
|
final List<NavigationItem> navigationItems;
|
|
|
|
|
final int currentIndex;
|
|
|
|
|
|
|
|
|
|
const CommonNavigationBar({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.viewMode,
|
|
|
|
|
required this.navigationItems,
|
|
|
|
|
required this.currentIndex,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
2025-02-09 18:39:38 +08:00
|
|
|
Widget build(BuildContext context, ref) {
|
2024-12-09 01:40:39 +08:00
|
|
|
if (viewMode == ViewMode.mobile) {
|
2025-03-05 16:08:50 +08:00
|
|
|
return NavigationBarTheme(
|
|
|
|
|
data: _NavigationBarDefaultsM3(context),
|
|
|
|
|
child: NavigationBar(
|
|
|
|
|
destinations: navigationItems
|
|
|
|
|
.map(
|
|
|
|
|
(e) => NavigationDestination(
|
|
|
|
|
icon: e.icon,
|
|
|
|
|
label: Intl.message(e.label.name),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.toList(),
|
2025-03-12 17:15:31 +08:00
|
|
|
onDestinationSelected: (index) {
|
|
|
|
|
globalState.appController.toPage(navigationItems[index].label);
|
|
|
|
|
},
|
2025-03-05 16:08:50 +08:00
|
|
|
selectedIndex: currentIndex,
|
|
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
);
|
|
|
|
|
}
|
2025-02-09 18:39:38 +08:00
|
|
|
final showLabel = ref.watch(appSettingProvider).showLabel;
|
2024-12-09 01:40:39 +08:00
|
|
|
return Material(
|
|
|
|
|
color: context.colorScheme.surfaceContainer,
|
2025-02-03 23:32:00 +08:00
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
2025-04-18 17:50:46 +08:00
|
|
|
child: ScrollConfiguration(
|
|
|
|
|
behavior: HiddenBarScrollBehavior(),
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
child: IntrinsicHeight(
|
|
|
|
|
child: NavigationRail(
|
|
|
|
|
backgroundColor: context.colorScheme.surfaceContainer,
|
|
|
|
|
selectedIconTheme: IconThemeData(
|
|
|
|
|
color: context.colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
|
|
|
|
unselectedIconTheme: IconThemeData(
|
|
|
|
|
color: context.colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
|
|
|
|
selectedLabelTextStyle:
|
|
|
|
|
context.textTheme.labelLarge!.copyWith(
|
|
|
|
|
color: context.colorScheme.onSurface,
|
|
|
|
|
),
|
|
|
|
|
unselectedLabelTextStyle:
|
|
|
|
|
context.textTheme.labelLarge!.copyWith(
|
|
|
|
|
color: context.colorScheme.onSurface,
|
|
|
|
|
),
|
|
|
|
|
destinations: navigationItems
|
|
|
|
|
.map(
|
|
|
|
|
(e) => NavigationRailDestination(
|
|
|
|
|
icon: e.icon,
|
|
|
|
|
label: Text(
|
|
|
|
|
Intl.message(e.label.name),
|
|
|
|
|
),
|
2025-03-05 16:08:50 +08:00
|
|
|
),
|
2025-04-18 17:50:46 +08:00
|
|
|
)
|
|
|
|
|
.toList(),
|
|
|
|
|
onDestinationSelected: (index) {
|
|
|
|
|
globalState.appController
|
|
|
|
|
.toPage(navigationItems[index].label);
|
|
|
|
|
},
|
|
|
|
|
extended: false,
|
|
|
|
|
selectedIndex: currentIndex,
|
|
|
|
|
labelType: showLabel
|
|
|
|
|
? NavigationRailLabelType.all
|
|
|
|
|
: NavigationRailLabelType.none,
|
|
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-02-03 23:32:00 +08:00
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 16,
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: () {
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.read(appSettingProvider.notifier).updateState(
|
|
|
|
|
(state) => state.copyWith(
|
2025-03-05 16:08:50 +08:00
|
|
|
showLabel: !state.showLabel,
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-02-03 23:32:00 +08:00
|
|
|
},
|
|
|
|
|
icon: const Icon(Icons.menu),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 16,
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-05 16:08:50 +08:00
|
|
|
|
|
|
|
|
class _NavigationBarDefaultsM3 extends NavigationBarThemeData {
|
|
|
|
|
_NavigationBarDefaultsM3(this.context)
|
|
|
|
|
: super(
|
|
|
|
|
height: 80.0,
|
|
|
|
|
elevation: 3.0,
|
|
|
|
|
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final BuildContext context;
|
|
|
|
|
late final ColorScheme _colors = Theme.of(context).colorScheme;
|
|
|
|
|
late final TextTheme _textTheme = Theme.of(context).textTheme;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Color? get backgroundColor => _colors.surfaceContainer;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Color? get shadowColor => Colors.transparent;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Color? get surfaceTintColor => Colors.transparent;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
WidgetStateProperty<IconThemeData?>? get iconTheme {
|
|
|
|
|
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
|
|
|
|
|
return IconThemeData(
|
|
|
|
|
size: 24.0,
|
|
|
|
|
color: states.contains(WidgetState.disabled)
|
2025-03-12 17:15:31 +08:00
|
|
|
? _colors.onSurfaceVariant.opacity38
|
2025-03-05 16:08:50 +08:00
|
|
|
: states.contains(WidgetState.selected)
|
|
|
|
|
? _colors.onSecondaryContainer
|
|
|
|
|
: _colors.onSurfaceVariant,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Color? get indicatorColor => _colors.secondaryContainer;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ShapeBorder? get indicatorShape => const StadiumBorder();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
WidgetStateProperty<TextStyle?>? get labelTextStyle {
|
|
|
|
|
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
|
|
|
|
|
final TextStyle style = _textTheme.labelMedium!;
|
|
|
|
|
return style.apply(
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
color: states.contains(WidgetState.disabled)
|
2025-03-12 17:15:31 +08:00
|
|
|
? _colors.onSurfaceVariant.opacity38
|
2025-03-05 16:08:50 +08:00
|
|
|
: states.contains(WidgetState.selected)
|
|
|
|
|
? _colors.onSurface
|
|
|
|
|
: _colors.onSurfaceVariant);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 17:15:31 +08:00
|
|
|
|
|
|
|
|
class HomeBackScope extends StatelessWidget {
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
|
|
const HomeBackScope({super.key, required this.child});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
return CommonPopScope(
|
|
|
|
|
onPop: () async {
|
|
|
|
|
final canPop = Navigator.canPop(context);
|
|
|
|
|
if (canPop) {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
} else {
|
|
|
|
|
await globalState.appController.handleBackOrExit();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
}
|