2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/clash/clash.dart';
|
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
2024-07-13 16:36:08 +08:00
|
|
|
import 'package:fl_clash/plugins/proxy.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-07-26 08:05:22 +08:00
|
|
|
import 'package:provider/provider.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-07-26 08:05:22 +08:00
|
|
|
class ClashContainer extends StatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
final Widget child;
|
|
|
|
|
|
2024-07-26 08:05:22 +08:00
|
|
|
const ClashContainer({
|
2024-04-30 23:38:49 +08:00
|
|
|
super.key,
|
|
|
|
|
required this.child,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
2024-07-26 08:05:22 +08:00
|
|
|
State<ClashContainer> createState() => _ClashContainerState();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-26 08:05:22 +08:00
|
|
|
class _ClashContainerState extends State<ClashContainer>
|
2024-07-13 16:36:08 +08:00
|
|
|
with AppMessageListener {
|
2024-07-26 08:05:22 +08:00
|
|
|
Widget _updateCoreState(Widget child) {
|
|
|
|
|
return Selector2<Config, ClashConfig, CoreState>(
|
|
|
|
|
selector: (_, config, clashConfig) => CoreState(
|
|
|
|
|
accessControl: config.isAccessControl ? config.accessControl : null,
|
|
|
|
|
allowBypass: config.allowBypass,
|
|
|
|
|
systemProxy: config.systemProxy,
|
|
|
|
|
mixedPort: clashConfig.mixedPort,
|
|
|
|
|
onlyProxy: config.onlyProxy,
|
|
|
|
|
),
|
|
|
|
|
builder: (__, state, child) {
|
|
|
|
|
clashCore.setState(state);
|
|
|
|
|
return child!;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-26 08:05:22 +08:00
|
|
|
return _updateCoreState(widget.child);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
clashMessage.addListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> dispose() async {
|
|
|
|
|
clashMessage.removeListener(this);
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onDelay(Delay delay) {
|
2024-05-11 17:02:34 +08:00
|
|
|
final appController = globalState.appController;
|
2024-05-06 19:03:49 +08:00
|
|
|
appController.setDelay(delay);
|
2024-04-30 23:38:49 +08:00
|
|
|
super.onDelay(delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onLog(Log log) {
|
2024-05-11 17:02:34 +08:00
|
|
|
globalState.appController.appState.addLog(log);
|
2024-04-30 23:38:49 +08:00
|
|
|
super.onLog(log);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 23:43:42 +08:00
|
|
|
@override
|
|
|
|
|
void onRequest(Connection connection) async {
|
|
|
|
|
globalState.appController.appState.addRequest(connection);
|
|
|
|
|
super.onRequest(connection);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-07-02 08:08:31 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onLoaded(String groupName) {
|
|
|
|
|
final appController = globalState.appController;
|
|
|
|
|
final currentSelectedMap = appController.config.currentSelectedMap;
|
|
|
|
|
final proxyName = currentSelectedMap[groupName];
|
|
|
|
|
if (proxyName == null) return;
|
2024-07-26 08:05:22 +08:00
|
|
|
appController.changeProxy(
|
2024-07-24 01:27:49 +08:00
|
|
|
groupName: groupName,
|
|
|
|
|
proxyName: proxyName,
|
2024-07-02 08:08:31 +08:00
|
|
|
);
|
|
|
|
|
super.onLoaded(proxyName);
|
|
|
|
|
}
|
2024-07-13 16:36:08 +08:00
|
|
|
|
|
|
|
|
@override
|
2024-07-26 08:05:22 +08:00
|
|
|
Future<void> onStarted(String runTime) async {
|
2024-07-13 16:36:08 +08:00
|
|
|
super.onStarted(runTime);
|
|
|
|
|
proxy?.updateStartTime();
|
|
|
|
|
final appController = globalState.appController;
|
2024-07-26 08:05:22 +08:00
|
|
|
await appController.applyProfile(isPrue: true);
|
|
|
|
|
appController.addCheckIpNumDebounce();
|
2024-07-13 16:36:08 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|