mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
初始化服务器状态页
This commit is contained in:
174
lib/view/page/convert.dart
Normal file
174
lib/view/page/convert.dart
Normal file
@@ -0,0 +1,174 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
|
||||
|
||||
class ConvertPage extends StatefulWidget {
|
||||
const ConvertPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_ConvertPageState createState() => _ConvertPageState();
|
||||
}
|
||||
|
||||
class _ConvertPageState extends State<ConvertPage>
|
||||
with AutomaticKeepAliveClientMixin {
|
||||
late TextEditingController _textEditingController;
|
||||
late TextEditingController _textEditingControllerResult;
|
||||
late MediaQueryData _media;
|
||||
late ThemeData _theme;
|
||||
|
||||
static const List<String> _typeOption = [
|
||||
'base64 decode',
|
||||
'base64 encode',
|
||||
'URL encode',
|
||||
'URL decode'
|
||||
];
|
||||
int _typeOptionIndex = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_textEditingController = TextEditingController();
|
||||
_textEditingControllerResult = TextEditingController();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_media = MediaQuery.of(context);
|
||||
_theme = Theme.of(context);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return Scaffold(
|
||||
body: GestureDetector(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7),
|
||||
child: AnimationLimiter(
|
||||
child: Column(
|
||||
children: AnimationConfiguration.toStaggeredList(
|
||||
duration: const Duration(milliseconds: 377),
|
||||
childAnimationBuilder: (widget) => SlideAnimation(
|
||||
verticalOffset: 50.0,
|
||||
child: FadeInAnimation(
|
||||
child: widget,
|
||||
),
|
||||
),
|
||||
children: [
|
||||
const SizedBox(height: 13),
|
||||
_buildInputTop(),
|
||||
_buildTypeOption(),
|
||||
_buildResult(),
|
||||
],
|
||||
))),
|
||||
),
|
||||
onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
_textEditingControllerResult.text = doConvert();
|
||||
},
|
||||
tooltip: 'convert',
|
||||
child: const Icon(Icons.send),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String doConvert() {
|
||||
final text = _textEditingController.text;
|
||||
switch (_typeOptionIndex) {
|
||||
case 0:
|
||||
return utf8.decode(base64.decode(text));
|
||||
case 1:
|
||||
return base64.encode(utf8.encode(text));
|
||||
case 2:
|
||||
return Uri.encodeFull(text);
|
||||
case 3:
|
||||
return Uri.decodeFull(text);
|
||||
default:
|
||||
return '未知编码';
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildInputTop() {
|
||||
return SizedBox(
|
||||
height: _media.size.height * 0.33,
|
||||
child: _buildInput(_textEditingController),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTypeOption() {
|
||||
return Card(
|
||||
child: ExpansionTile(
|
||||
leading: TextButton(
|
||||
child: SizedBox(
|
||||
width: _media.size.width * 0.2,
|
||||
child: Row(
|
||||
children: const [Icon(Icons.change_circle), Text('上下交换')],
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
final temp = _textEditingController.text;
|
||||
_textEditingController.text = _textEditingControllerResult.text;
|
||||
_textEditingControllerResult.text = temp;
|
||||
},
|
||||
),
|
||||
title: SizedBox(
|
||||
width: _media.size.width * 0.4,
|
||||
child: Text(
|
||||
_typeOption[_typeOptionIndex],
|
||||
style: const TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
children: _typeOption
|
||||
.map((e) => ListTile(
|
||||
title: Text(
|
||||
e,
|
||||
style: TextStyle(
|
||||
color:
|
||||
_theme.textTheme.bodyText2!.color!.withAlpha(177)),
|
||||
),
|
||||
trailing: _buildRadio(_typeOption.indexOf(e)),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildResult() {
|
||||
return SizedBox(
|
||||
height: _media.size.height * 0.33,
|
||||
child: _buildInput(_textEditingControllerResult),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInput(TextEditingController controller) {
|
||||
return Card(
|
||||
child: TextField(
|
||||
maxLines: 20,
|
||||
decoration: InputDecoration(
|
||||
fillColor: Theme.of(context).cardColor,
|
||||
filled: true,
|
||||
border: InputBorder.none),
|
||||
controller: controller,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Radio _buildRadio(int index) {
|
||||
return Radio<int>(
|
||||
value: index,
|
||||
groupValue: _typeOptionIndex,
|
||||
onChanged: (int? value) {
|
||||
setState(() {
|
||||
_typeOptionIndex = value!;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
45
lib/view/page/debug.dart
Normal file
45
lib/view/page/debug.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:toolbox/data/provider/debug.dart';
|
||||
|
||||
class DebugPage extends StatefulWidget {
|
||||
const DebugPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_DebugPageState createState() => _DebugPageState();
|
||||
}
|
||||
|
||||
class _DebugPageState extends State<DebugPage> {
|
||||
DebugProvider get debug => Provider.of<DebugProvider>(context);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar:
|
||||
AppBar(title: const Text('Terminal'), backgroundColor: Colors.black),
|
||||
body: _buildTerminal(context),
|
||||
backgroundColor: Colors.black,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTerminal(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
color: Colors.black,
|
||||
child: DefaultTextStyle(
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: debug.widgets,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
96
lib/view/page/home.dart
Normal file
96
lib/view/page/home.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:toolbox/core/route.dart';
|
||||
import 'package:toolbox/data/res/build_data.dart';
|
||||
import 'package:toolbox/view/page/convert.dart';
|
||||
import 'package:toolbox/view/page/debug.dart';
|
||||
import 'package:toolbox/view/page/server.dart';
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({Key? key, required this.title}) : super(key: key);
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage>
|
||||
with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
|
||||
final List<String> _tabs = ['服务器', '编/解码', '1', '2', '3'];
|
||||
late final TabController _tabController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: _tabs.length, vsync: this);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: GestureDetector(
|
||||
onLongPress: () =>
|
||||
AppRoute(const DebugPage(), 'Debug Page').go(context),
|
||||
child: Text(widget.title),
|
||||
),
|
||||
bottom: TabBar(
|
||||
tabs: _tabs.map((e) => Tab(text: e)).toList(),
|
||||
controller: _tabController,
|
||||
),
|
||||
),
|
||||
drawer: _buildDrawer(),
|
||||
body: TabBarView(controller: _tabController, children: const [
|
||||
ServerPage(),
|
||||
ConvertPage(),
|
||||
ConvertPage(),
|
||||
ConvertPage(),
|
||||
ConvertPage()
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDrawer() {
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: [
|
||||
UserAccountsDrawerHeader(
|
||||
accountName: const Text('ToolBox'),
|
||||
accountEmail: Text(_buildVersionStr()),
|
||||
currentAccountPicture: _buildIcon(const Color(0x00083963)),
|
||||
),
|
||||
const ListTile(
|
||||
leading: Icon(Icons.settings),
|
||||
title: Text('设置'),
|
||||
),
|
||||
AboutListTile(
|
||||
icon: const Icon(Icons.text_snippet),
|
||||
child: const Text('开源证书'),
|
||||
applicationName: BuildData.name,
|
||||
applicationVersion: _buildVersionStr(),
|
||||
applicationIcon: _buildIcon(Colors.transparent),
|
||||
aboutBoxChildren: const [
|
||||
Text('''\nMade with Love.
|
||||
\nAll rights reserved.'''),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIcon(Color c) {
|
||||
return CircleAvatar(
|
||||
child: Image.asset('assets/app_icon.jpg'),
|
||||
backgroundColor: c,
|
||||
);
|
||||
}
|
||||
|
||||
String _buildVersionStr() {
|
||||
return 'Ver: 1.0.${BuildData.build}';
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
175
lib/view/page/server.dart
Normal file
175
lib/view/page/server.dart
Normal file
@@ -0,0 +1,175 @@
|
||||
import 'package:charts_flutter/flutter.dart' as chart;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
|
||||
import 'package:ssh2/ssh2.dart';
|
||||
import 'package:toolbox/core/utils.dart';
|
||||
import 'package:toolbox/view/widget/circle_pie.dart';
|
||||
|
||||
class ServerPage extends StatefulWidget {
|
||||
const ServerPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_ServerPageState createState() => _ServerPageState();
|
||||
}
|
||||
|
||||
class _ServerPageState extends State<ServerPage>
|
||||
with AutomaticKeepAliveClientMixin {
|
||||
late MediaQueryData _media;
|
||||
late ThemeData _theme;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_media = MediaQuery.of(context);
|
||||
_theme = Theme.of(context);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return Scaffold(
|
||||
body: GestureDetector(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7),
|
||||
child: AnimationLimiter(
|
||||
child: Column(
|
||||
children: AnimationConfiguration.toStaggeredList(
|
||||
duration: const Duration(milliseconds: 377),
|
||||
childAnimationBuilder: (widget) => SlideAnimation(
|
||||
verticalOffset: 50.0,
|
||||
child: FadeInAnimation(
|
||||
child: widget,
|
||||
),
|
||||
),
|
||||
children: [const SizedBox(height: 13), ..._buildServerCards()],
|
||||
))),
|
||||
),
|
||||
onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
showSnackBar(context, const Text(''));
|
||||
},
|
||||
tooltip: 'add a server',
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<String>>? _getData() async {
|
||||
final client = SSHClient(
|
||||
host: '',
|
||||
port: 0,
|
||||
username: '',
|
||||
passwordOrKey: '',
|
||||
);
|
||||
await client.connect();
|
||||
final cpu = await client.execute(
|
||||
"top -bn1 | grep load | awk '{printf \"%.2f\", \$(NF-2)}'") ??
|
||||
'failed';
|
||||
final mem = await client
|
||||
.execute("free -m | awk 'NR==2{printf \"%s/%sMB\", \$3,\$2}'") ??
|
||||
'failed';
|
||||
return [cpu.trim(), mem.trim()];
|
||||
}
|
||||
|
||||
Widget _buildEachServerCard() {
|
||||
return FutureBuilder<List<String>>(
|
||||
future: _getData(),
|
||||
builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
if (snapshot.hasError) {
|
||||
return Text("Error: ${snapshot.error}");
|
||||
} else {
|
||||
return _buildEachCardContent(snapshot);
|
||||
}
|
||||
} else {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEachCardContent(AsyncSnapshot snapshot) {
|
||||
final cpuPercent = double.parse(snapshot.data![0]) * 100;
|
||||
final memSplit = snapshot.data![1].replaceFirst('MB', '').split('/');
|
||||
final memPercent = int.parse(memSplit[0]) / int.parse(memSplit[1]) * 100;
|
||||
final cpuData = [
|
||||
IndexPercent(0, cpuPercent.toInt()),
|
||||
];
|
||||
final memData = [
|
||||
IndexPercent(0, memPercent.toInt()),
|
||||
];
|
||||
return Card(
|
||||
child: Padding(padding:const EdgeInsets.all(13) , child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(' Jilin', style: TextStyle(fontWeight: FontWeight.bold),),
|
||||
const SizedBox(height: 7,),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
_buildPercentCircle(cpuPercent, 'CPU', [
|
||||
chart.Series<IndexPercent, int>(
|
||||
id: 'CPU',
|
||||
domainFn: (IndexPercent cpu, _) => cpu.id,
|
||||
measureFn: (IndexPercent cpu, _) => cpu.percent,
|
||||
data: cpuData,
|
||||
)
|
||||
]),
|
||||
_buildPercentCircle(memPercent, 'MEM', [
|
||||
chart.Series<IndexPercent, int>(
|
||||
id: 'MEM',
|
||||
domainFn: (IndexPercent sales, _) => sales.id,
|
||||
measureFn: (IndexPercent sales, _) => sales.percent,
|
||||
data: memData,
|
||||
)
|
||||
])
|
||||
],
|
||||
)
|
||||
],
|
||||
),),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPercentCircle(
|
||||
double percent, String title, List<chart.Series<IndexPercent, int>> series) {
|
||||
return SizedBox(
|
||||
width: _media.size.width * 0.2,
|
||||
height: _media.size.height * 0.1,
|
||||
child: Stack(
|
||||
children: [
|
||||
DonutPieChart.withRandomData(),
|
||||
Positioned(
|
||||
child: Text(
|
||||
'${percent.toStringAsFixed(1)}%',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
left: 0,
|
||||
right: 0,
|
||||
top: 0,
|
||||
bottom: 0
|
||||
),
|
||||
Positioned(
|
||||
child: Text(title, textAlign: TextAlign.center),
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildServerCards() {
|
||||
return [_buildEachServerCard()];
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
Reference in New Issue
Block a user