Skip to content

Commit

Permalink
refactor: getx -> go_router
Browse files Browse the repository at this point in the history
feat: UUID生成
  • Loading branch information
bymoye committed Dec 22, 2024
1 parent bcfc662 commit b19fdd9
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 286 deletions.
248 changes: 125 additions & 123 deletions lib/base_page.dart
Original file line number Diff line number Diff line change
@@ -1,154 +1,156 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:refreshed/refreshed.dart';
import 'package:go_router/go_router.dart';
import 'package:simple_icons/simple_icons.dart';
import 'package:tools/func_enum.dart';
import 'package:tools/global_variable.dart';
import 'package:web/web.dart' as web;

// class Man

class BasePage extends StatelessWidget {
final Future<Widget> Function() child;
final String title;
final Rxn<Widget> body = Rxn<Widget>();
BasePage({super.key, required this.title, required this.child});
// final Rxn<Widget> body = Rxn<Widget>();
const BasePage({super.key, required this.title, required this.child});

// bool get isDark => Theme.of(Get.context!).brightness == Brightness.dark;

@override
Widget build(BuildContext context) {
if (body.value == null) child().then(body.call);

bool isDark = Theme.of(context).brightness == Brightness.dark;

return Scaffold(
appBar: AppBar(
title: Text(title),
centerTitle: true,
elevation: 0,
shadowColor: Colors.transparent,
backgroundColor: Colors.transparent,
scrolledUnderElevation: 0,
),
drawer: Drawer(
child: Column(
children: [
DrawerHeader(
decoration: const BoxDecoration(
color: Color.fromARGB(255, 44, 78, 105),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
const Text(
'功能列表',
style: TextStyle(color: Colors.white, fontSize: 24),
),
appBar: AppBar(
title: Text(title),
centerTitle: true,
elevation: 0,
shadowColor: Colors.transparent,
backgroundColor: Colors.transparent,
scrolledUnderElevation: 0,
),
drawer: Drawer(
child: Column(
children: [
DrawerHeader(
decoration: const BoxDecoration(
color: Color.fromARGB(255, 44, 78, 105),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
const Text(
'功能列表',
style: TextStyle(color: Colors.white, fontSize: 24),
),

/// 搜索框
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(
decoration: BoxDecoration(
color: Colors.white.withAlpha(100),
borderRadius: BorderRadius.circular(20),
border:
Border.all(color: Colors.white.withAlpha(100)),
),
child: TextField(
onChanged: GlobalVariable.searchRx.call,
controller: TextEditingController(
text: GlobalVariable.searchRx.value),
decoration: const InputDecoration(
hintText: "搜索工具",
prefixIcon: Icon(Icons.search),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 16, vertical: 14),
),
/// 搜索框
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(
decoration: BoxDecoration(
color: Colors.white.withAlpha(100),
borderRadius: BorderRadius.circular(20),
border:
Border.all(color: Colors.white.withAlpha(100)),
),
child: TextField(
onChanged: (value) {
GlobalVariable.searchNotifier.value = value;
},
controller: TextEditingController(
text: GlobalVariable.searchNotifier.value),
decoration: const InputDecoration(
hintText: "搜索工具",
prefixIcon: Icon(Icons.search),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 16, vertical: 14),
),
),
),
)
],
),
),
)
],
),
Expanded(
child: ListView(
/// 内容居中
physics: const BouncingScrollPhysics(),
padding: EdgeInsets.zero,
children: [
ValueListenableBuilder(
valueListenable: GlobalVariable.searchRx,
builder: (context, value, child) {
List<FunctionEnum> list = FunctionEnum.values
.where((element) => (GlobalVariable
.searchRx.value.isNotEmpty
? element.name
.contains(GlobalVariable.searchRx.value)
: true))
.toList();
if (list.isEmpty) {
return const ListTile(
title: Text("没有找到相关工具"),
);
}

return Column(
children: (list)
.map((e) => ListTile(
title: Text(e.name),
tileColor: e.route == Get.currentRoute
? Colors.blue.withAlpha(100)
: null,
onTap: e.onTap))
.toList(),
),
Expanded(
child: ListView(
/// 内容居中
physics: const BouncingScrollPhysics(),
padding: EdgeInsets.zero,
children: [
ValueListenableBuilder(
valueListenable: GlobalVariable.searchNotifier,
builder: (context, value, child) {
List<FunctionEnum> list = FunctionEnum.values
.where((element) =>
(GlobalVariable.searchNotifier.value.isNotEmpty
? element.name.contains(
GlobalVariable.searchNotifier.value)
: true))
.toList();
if (list.isEmpty) {
return const ListTile(
title: Text("没有找到相关工具"),
);
}),
],
),
}

return Column(
children: (list)
.map((e) => ListTile(
title: Text(e.name),
tileColor: e.route ==
GoRouter.of(context).state?.path
? Colors.blue.withAlpha(100)
: null,
onTap: e.onTap))
.toList(),
);
}),
],
),
const Divider(),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
TextButton(
),
const Divider(),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
TextButton(
onPressed: () {
web.window.open("https://github.com/bymoye/tools");
},
child: const Row(
children: [
Icon(SimpleIcons.github, size: 20),
SizedBox(width: 8),
Text("bymoye/tools")
],
),
),
const Spacer(),
IconButton(
onPressed: () {
web.window.open("https://github.com/bymoye/tools");
GlobalVariable.themeModeNotifier.value =
isDark ? ThemeMode.light : ThemeMode.dark;
},
child: const Row(
children: [
Icon(SimpleIcons.github, size: 20),
SizedBox(width: 8),
Text("bymoye/tools")
],
),
),
const Spacer(),
IconButton(
onPressed: () {
Get.changeThemeMode(
isDark ? ThemeMode.light : ThemeMode.dark);
},
icon:
Icon(isDark ? Icons.light_mode : Icons.dark_mode))
],
))
],
),
icon: Icon(isDark ? Icons.light_mode : Icons.dark_mode))
],
))
],
),
body: Obx(
() =>
body.value ??
const Center(
child: CircularProgressIndicator(),
),
));
),
body: FutureBuilder(
future: child(),
builder: (context, snapshot) {
return snapshot.hasData
? snapshot.data!
: const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
4 changes: 2 additions & 2 deletions lib/func_enum.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:refreshed/refreshed.dart';
import 'package:tools/router.dart';
import 'package:web/web.dart' as web;

/// 首页
Expand Down Expand Up @@ -91,7 +91,7 @@ enum FunctionEnum {
web.window.open("https://squoosh.app/", "squoosh");
break;
default:
Get.offAllNamed(route);
router.go(route);
}
}
}
6 changes: 4 additions & 2 deletions lib/global_variable.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:refreshed/refreshed.dart';
import 'package:flutter/material.dart';

class GlobalVariable {
static RxString searchRx = RxString("");
static ValueNotifier<ThemeMode> themeModeNotifier =
ValueNotifier(ThemeMode.system);
static ValueNotifier<String> searchNotifier = ValueNotifier("");
}
47 changes: 28 additions & 19 deletions lib/json_tools/json_utils.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:refreshed/refreshed.dart';

class JsonUtilsPage extends StatelessWidget {
JsonUtilsPage({super.key});
class JsonUtilsPage extends StatefulWidget {
const JsonUtilsPage({super.key});

final TextEditingController _controller = TextEditingController();
@override
State<JsonUtilsPage> createState() => _JsonUtilsPageState();
}

/// 输出框
final RxString _output = RxString("");
class _JsonUtilsPageState extends State<JsonUtilsPage> {
final TextEditingController _controller = TextEditingController();
final TextEditingController _outputController = TextEditingController();

/// 状态提示
final RxString _status = RxString("");
String _status = "";

@override
Widget build(BuildContext context) {
Expand All @@ -38,10 +39,14 @@ class JsonUtilsPage extends StatelessWidget {
/// 将输入框的内容格式化, 缩进2个空格
try {
var json = jsonDecode(_controller.text);
_output.value =
_outputController.text =
const JsonEncoder.withIndent(" ").convert(json);
_status = "";
setState(() {});
} catch (e) {
_output.value = "JSON格式错误";
setState(() {
_status = "JSON格式错误";
});
}
},
child: const Text("格式化"),
Expand All @@ -50,25 +55,29 @@ class JsonUtilsPage extends StatelessWidget {
onPressed: () {
try {
var json = jsonDecode(_controller.text);
_output.value = jsonEncode(json);
_outputController.text = jsonEncode(json);
_status = "";
setState(() {});
} catch (e) {
_output.value = "JSON格式错误";
setState(() {
_status = "JSON格式错误";
});
}
},
child: const Text("压缩"),
),
],
),
Obx(() => Text(_status.value)),
Text(_status),
const SizedBox(height: 30),
const Text("结果", style: TextStyle(fontSize: 15)),
Obx(() => TextField(
controller: TextEditingController(text: _output.value),
readOnly: true,
maxLines: 10,
TextField(
controller: _outputController,
readOnly: true,
maxLines: 10,

/// 高度可被拉伸
))
/// 高度可被拉伸
)
],
),
);
Expand Down
Loading

0 comments on commit b19fdd9

Please sign in to comment.