Files
naivefly/lib/main.dart
2025-03-28 09:38:24 +08:00

163 lines
4.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:process_run/shell.dart';
import 'dart:io';
void main() {
runApp(NaiveProxyApp());
}
class NaiveProxyApp extends StatelessWidget {
const NaiveProxyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NaiveProxy启动器',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: NaiveProxyHomePage(),
);
}
}
class NaiveProxyHomePage extends StatefulWidget {
const NaiveProxyHomePage({super.key});
@override
_NaiveProxyHomePageState createState() => _NaiveProxyHomePageState();
}
class _NaiveProxyHomePageState extends State<NaiveProxyHomePage> {
String? _configFilePath;
String _status = '未选择配置文件';
Future<void> _pickConfigFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['json'],
);
if (result != null) {
setState(() {
_configFilePath = result.files.single.path;
_status = '已选择文件: ${_configFilePath!.split('/').last}';
});
}
}
Future<void> _launchNaiveProxy() async {
if (_configFilePath == null) {
_showErrorDialog('请先导入配置文件');
return;
}
try {
// 检查naiveproxy是否已安装
var shell = Shell();
var whichResult = await shell.run('which naive');
if (whichResult.isEmpty) {
_showErrorDialog('NaiveProxy未安装');
return;
}
// 尝试执行naive
var result = await shell.run('naive $_configFilePath');
// 显示执行结果
_showResultDialog('NaiveProxy已启动', result.outText);
} catch (e) {
_showErrorDialog('启动失败:${e.toString()}');
}
}
void _showErrorDialog(String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('错误'),
content: Text(message),
actions: <Widget>[
TextButton(
child: Text('确定'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
void _showResultDialog(String title, String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: Text(message),
),
actions: <Widget>[
TextButton(
child: Text('确定'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('NaiveProxy启动器'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 300,
child: Text(
_status,
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
),
SizedBox(height: 20),
ElevatedButton.icon(
icon: Icon(Icons.file_upload),
label: Text('导入配置文件'),
onPressed: _pickConfigFile,
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
),
SizedBox(height: 20),
ElevatedButton.icon(
icon: Icon(Icons.rocket_launch),
label: Text('启动NaiveProxy'),
onPressed: _launchNaiveProxy,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
),
],
),
),
);
}
}