2025-06-07 01:48:34 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
2025-12-16 11:23:09 +08:00
|
|
|
import 'package:fl_clash/controller.dart';
|
2025-06-07 01:48:34 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
|
|
|
|
import 'package:fl_clash/manager/window_manager.dart';
|
|
|
|
|
import 'package:fl_clash/providers/providers.dart';
|
|
|
|
|
import 'package:fl_clash/state.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
|
|
class AppStateManager extends ConsumerStatefulWidget {
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
const AppStateManager({super.key, required this.child});
|
2025-06-07 01:48:34 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState<AppStateManager> createState() => _AppStateManagerState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AppStateManagerState extends ConsumerState<AppStateManager>
|
|
|
|
|
with WidgetsBindingObserver {
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2025-07-31 17:09:18 +08:00
|
|
|
ref.listenManual(checkIpProvider, (prev, next) {
|
2025-12-16 11:23:09 +08:00
|
|
|
if (prev != next && next.a && next.c) {
|
|
|
|
|
ref.read(networkDetectionProvider.notifier).startCheck();
|
2025-07-31 17:09:18 +08:00
|
|
|
}
|
2025-12-16 11:23:09 +08:00
|
|
|
});
|
|
|
|
|
ref.listenManual(configProvider, (prev, next) {
|
2025-06-07 01:48:34 +08:00
|
|
|
if (prev != next) {
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.savePreferencesDebounce();
|
2025-06-07 01:48:34 +08:00
|
|
|
}
|
|
|
|
|
});
|
2025-07-31 17:09:18 +08:00
|
|
|
ref.listenManual(needUpdateGroupsProvider, (prev, next) {
|
|
|
|
|
if (prev != next) {
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateGroupsDebounce();
|
2025-07-31 17:09:18 +08:00
|
|
|
}
|
|
|
|
|
});
|
2025-06-07 01:48:34 +08:00
|
|
|
if (window == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-31 17:09:18 +08:00
|
|
|
ref.listenManual(autoSetSystemDnsStateProvider, (prev, next) async {
|
|
|
|
|
if (prev == next) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (next.a == true && next.b == true) {
|
|
|
|
|
macOS?.updateDns(false);
|
|
|
|
|
} else {
|
|
|
|
|
macOS?.updateDns(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-06-07 01:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
|
|
|
|
|
commonPrint.log('$state');
|
2025-09-27 23:39:20 +08:00
|
|
|
if (state == AppLifecycleState.resumed) {
|
2025-06-07 01:48:34 +08:00
|
|
|
render?.resume();
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.tryCheckIp();
|
|
|
|
|
if (system.isAndroid) {
|
|
|
|
|
appController.tryStartCore();
|
|
|
|
|
}
|
2025-06-07 01:48:34 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangePlatformBrightness() {
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateBrightness();
|
2025-06-07 01:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Listener(
|
|
|
|
|
onPointerHover: (_) {
|
|
|
|
|
render?.resume();
|
|
|
|
|
},
|
|
|
|
|
child: widget.child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AppEnvManager extends StatelessWidget {
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
const AppEnvManager({super.key, required this.child});
|
2025-06-07 01:48:34 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
if (globalState.isPre) {
|
|
|
|
|
return Banner(
|
|
|
|
|
message: 'DEBUG',
|
|
|
|
|
location: BannerLocation.topEnd,
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (globalState.isPre) {
|
|
|
|
|
return Banner(
|
|
|
|
|
message: 'PRE',
|
|
|
|
|
location: BannerLocation.topEnd,
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AppSidebarContainer extends ConsumerWidget {
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
const AppSidebarContainer({super.key, required this.child});
|
2025-06-07 01:48:34 +08:00
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
// Widget _buildLoading() {
|
|
|
|
|
// return Consumer(
|
|
|
|
|
// builder: (_, ref, _) {
|
|
|
|
|
// final loading = ref.watch(loadingProvider);
|
|
|
|
|
// final isMobileView = ref.watch(isMobileViewProvider);
|
|
|
|
|
// return loading && !isMobileView
|
|
|
|
|
// ? RotatedBox(
|
|
|
|
|
// quarterTurns: 1,
|
|
|
|
|
// child: const LinearProgressIndicator(),
|
|
|
|
|
// )
|
|
|
|
|
// : Container();
|
|
|
|
|
// },
|
|
|
|
|
// );
|
|
|
|
|
// }
|
2025-06-07 01:48:34 +08:00
|
|
|
|
|
|
|
|
Widget _buildBackground({
|
|
|
|
|
required BuildContext context,
|
|
|
|
|
required Widget child,
|
|
|
|
|
}) {
|
2025-07-31 17:09:18 +08:00
|
|
|
return Material(color: context.colorScheme.surfaceContainer, child: child);
|
|
|
|
|
// if (!system.isMacOS) {
|
|
|
|
|
// return Material(
|
|
|
|
|
// color: context.colorScheme.surfaceContainer,
|
|
|
|
|
// child: child,
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// return child;
|
|
|
|
|
// return TransparentMacOSSidebar(
|
|
|
|
|
// child: Material(color: Colors.transparent, child: child),
|
|
|
|
|
// );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _updateSideBarWidth(WidgetRef ref, double contentWidth) {
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
ref.read(sideWidthProvider.notifier).value =
|
|
|
|
|
ref.read(viewSizeProvider.select((state) => state.width)) -
|
|
|
|
|
contentWidth;
|
|
|
|
|
});
|
2025-06-07 01:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
final navigationState = ref.watch(navigationStateProvider);
|
|
|
|
|
final navigationItems = navigationState.navigationItems;
|
|
|
|
|
final isMobileView = navigationState.viewMode == ViewMode.mobile;
|
|
|
|
|
if (isMobileView) {
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
final currentIndex = navigationState.currentIndex;
|
|
|
|
|
final showLabel = ref.watch(appSettingProvider).showLabel;
|
|
|
|
|
return Row(
|
|
|
|
|
children: [
|
2025-07-31 17:09:18 +08:00
|
|
|
_buildBackground(
|
|
|
|
|
context: context,
|
|
|
|
|
child: SafeArea(
|
2025-12-16 11:23:09 +08:00
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2025-07-31 17:09:18 +08:00
|
|
|
children: [
|
2025-12-16 11:23:09 +08:00
|
|
|
if (system.isMacOS) SizedBox(height: 22),
|
|
|
|
|
SizedBox(height: 10),
|
|
|
|
|
if (!system.isMacOS) ...[
|
|
|
|
|
ClipRect(child: AppIcon()),
|
|
|
|
|
SizedBox(height: 12),
|
|
|
|
|
],
|
|
|
|
|
Expanded(
|
|
|
|
|
child: ScrollConfiguration(
|
|
|
|
|
behavior: HiddenBarScrollBehavior(),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: NavigationRail(
|
|
|
|
|
scrollable: true,
|
|
|
|
|
minExtendedWidth: 200,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
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)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.toList(),
|
|
|
|
|
onDestinationSelected: (index) {
|
|
|
|
|
appController.toPage(
|
|
|
|
|
navigationItems[index].label,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
extended: false,
|
|
|
|
|
selectedIndex: currentIndex,
|
|
|
|
|
labelType: showLabel
|
|
|
|
|
? NavigationRailLabelType.all
|
|
|
|
|
: NavigationRailLabelType.none,
|
|
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
),
|
2025-12-16 11:23:09 +08:00
|
|
|
],
|
2025-06-07 01:48:34 +08:00
|
|
|
),
|
2025-12-16 11:23:09 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
ref
|
|
|
|
|
.read(appSettingProvider.notifier)
|
|
|
|
|
.update(
|
|
|
|
|
(state) =>
|
|
|
|
|
state.copyWith(showLabel: !state.showLabel),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
icon: Icon(
|
|
|
|
|
Icons.menu,
|
|
|
|
|
color: context.colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2025-12-16 11:23:09 +08:00
|
|
|
const SizedBox(height: 16),
|
2025-07-31 17:09:18 +08:00
|
|
|
],
|
2025-06-07 01:48:34 +08:00
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
),
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 1,
|
|
|
|
|
child: ClipRect(
|
2025-07-31 17:09:18 +08:00
|
|
|
child: LayoutBuilder(
|
|
|
|
|
builder: (_, constraints) {
|
|
|
|
|
_updateSideBarWidth(ref, constraints.maxWidth);
|
|
|
|
|
return child;
|
|
|
|
|
},
|
|
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|