mirror of
https://github.com/localsend/localsend.git
synced 2026-06-23 04:10:07 +00:00
fix: react on wifi change
This commit is contained in:
+20
-11
@@ -5,10 +5,12 @@ import 'package:localsend_app/gen/strings.g.dart';
|
||||
import 'package:localsend_app/init.dart';
|
||||
import 'package:localsend_app/pages/home_page.dart';
|
||||
import 'package:localsend_app/provider/device_info_provider.dart';
|
||||
import 'package:localsend_app/provider/network_info_provider.dart';
|
||||
import 'package:localsend_app/provider/persistence_provider.dart';
|
||||
import 'package:localsend_app/provider/settings_provider.dart';
|
||||
import 'package:localsend_app/theme.dart';
|
||||
import 'package:localsend_app/util/device_info_helper.dart';
|
||||
import 'package:localsend_app/widget/life_cycle_watcher.dart';
|
||||
import 'package:routerino/routerino.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
@@ -30,17 +32,24 @@ class LocalSendApp extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final themeMode = ref.watch(settingsProvider.select((settings) => settings.theme));
|
||||
return MaterialApp(
|
||||
title: t.appName,
|
||||
locale: TranslationProvider.of(context).flutterLocale,
|
||||
supportedLocales: LocaleSettings.supportedLocales,
|
||||
localizationsDelegates: GlobalMaterialLocalizations.delegates,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: getTheme(Brightness.light),
|
||||
darkTheme: getTheme(Brightness.dark),
|
||||
themeMode: themeMode,
|
||||
navigatorKey: Routerino.navigatorKey,
|
||||
home: const HomePage(),
|
||||
return LifeCycleWatcher(
|
||||
onChangedState: (AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
ref.read(networkInfoProvider.notifier).init();
|
||||
}
|
||||
},
|
||||
child: MaterialApp(
|
||||
title: t.appName,
|
||||
locale: TranslationProvider.of(context).flutterLocale,
|
||||
supportedLocales: LocaleSettings.supportedLocales,
|
||||
localizationsDelegates: GlobalMaterialLocalizations.delegates,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: getTheme(Brightness.light),
|
||||
darkTheme: getTheme(Brightness.dark),
|
||||
themeMode: themeMode,
|
||||
navigatorKey: Routerino.navigatorKey,
|
||||
home: const HomePage(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:localsend_app/model/network_info.dart';
|
||||
import 'package:network_info_plus/network_info_plus.dart' as plugin;
|
||||
|
||||
final networkInfoProvider = StateNotifierProvider<NetworkInfoNotifier, NetworkInfo?>((ref) => NetworkInfoNotifier());
|
||||
|
||||
StreamSubscription<ConnectivityResult>? _subscription;
|
||||
|
||||
class NetworkInfoNotifier extends StateNotifier<NetworkInfo?> {
|
||||
NetworkInfoNotifier() : super(null) {
|
||||
_init();
|
||||
init();
|
||||
}
|
||||
|
||||
Future<void> _init() async {
|
||||
Future<void> init() async {
|
||||
if (!kIsWeb) {
|
||||
_subscription?.cancel();
|
||||
_subscription = Connectivity().onConnectivityChanged.listen((_) async {
|
||||
state = await _getInfo();
|
||||
});
|
||||
}
|
||||
state = await _getInfo();
|
||||
}
|
||||
|
||||
Future<NetworkInfo> _getInfo() async {
|
||||
final info = plugin.NetworkInfo();
|
||||
String? ip;
|
||||
String? mask;
|
||||
@@ -19,7 +35,10 @@ class NetworkInfoNotifier extends StateNotifier<NetworkInfo?> {
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
state = NetworkInfo(
|
||||
|
||||
print('New network state: $ip ($mask)');
|
||||
|
||||
return NetworkInfo(
|
||||
localIp: ip,
|
||||
netMask: mask,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LifeCycleWatcher extends StatefulWidget {
|
||||
final Widget child;
|
||||
final void Function(AppLifecycleState state) onChangedState;
|
||||
|
||||
const LifeCycleWatcher({required this.child, required this.onChangedState, super.key});
|
||||
|
||||
@override
|
||||
State<LifeCycleWatcher> createState() => _LifeCycleWatcherState();
|
||||
}
|
||||
|
||||
class _LifeCycleWatcherState extends State<LifeCycleWatcher> with WidgetsBindingObserver {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return widget.child;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
widget.onChangedState(state);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
import FlutterMacOS
|
||||
import Foundation
|
||||
|
||||
import connectivity_plus
|
||||
import desktop_drop
|
||||
import device_info_plus
|
||||
import network_info_plus
|
||||
@@ -17,6 +18,7 @@ import url_launcher_macos
|
||||
import window_manager
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
|
||||
DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin"))
|
||||
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
|
||||
NetworkInfoPlusPlugin.register(with: registry.registrar(forPlugin: "NetworkInfoPlusPlugin"))
|
||||
|
||||
@@ -148,6 +148,20 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.0"
|
||||
connectivity_plus:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: connectivity_plus
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.2"
|
||||
connectivity_plus_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: connectivity_plus_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.3"
|
||||
console:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -11,6 +11,7 @@ environment:
|
||||
dependencies:
|
||||
basic_utils: 5.4.2
|
||||
collection: 1.16.0
|
||||
connectivity_plus: 3.0.2
|
||||
desktop_drop: 0.4.0
|
||||
device_info_plus: 8.0.0
|
||||
dio: 4.0.6
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
|
||||
#include <desktop_drop/desktop_drop_plugin.h>
|
||||
#include <network_info_plus/network_info_plus_windows_plugin.h>
|
||||
#include <permission_handler_windows/permission_handler_windows_plugin.h>
|
||||
@@ -14,6 +15,8 @@
|
||||
#include <window_manager/window_manager_plugin.h>
|
||||
|
||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
|
||||
DesktopDropPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("DesktopDropPlugin"));
|
||||
NetworkInfoPlusWindowsPluginRegisterWithRegistrar(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#
|
||||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
connectivity_plus
|
||||
desktop_drop
|
||||
network_info_plus
|
||||
permission_handler_windows
|
||||
|
||||
Reference in New Issue
Block a user