#54 new: grouped snippet & tab snippet

This commit is contained in:
lollipopkit
2023-05-30 18:05:46 +08:00
parent 96438313a1
commit a1e80fd806
13 changed files with 192 additions and 57 deletions

View File

@@ -43,7 +43,7 @@ class MyApp extends StatelessWidget {
brightness: Brightness.dark, brightness: Brightness.dark,
colorSchemeSeed: primaryColor, colorSchemeSeed: primaryColor,
), ),
home: const MyHomePage(), home: const HomePage(),
); );
}, },
); );

View File

@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:toolbox/core/extension/navigator.dart'; import 'package:toolbox/core/extension/navigator.dart';
import 'package:toolbox/data/model/app/tab.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import '../../data/model/server/snippet.dart'; import '../../data/model/server/snippet.dart';
@@ -99,17 +100,15 @@ void setTransparentNavigationBar(BuildContext context) {
} }
} }
String tabTitleName(BuildContext context, int i) { String tabTitleName(BuildContext context, AppTab tab) {
final s = S.of(context)!; final s = S.of(context)!;
switch (i) { switch (tab) {
case 0: case AppTab.servers:
return s.server; return s.server;
case 1: case AppTab.snippet:
return s.convert; return s.convert;
case 2: case AppTab.ping:
return 'Ping'; return 'Ping';
default:
return '';
} }
} }

View File

@@ -1 +1 @@
enum AppTab { servers, encode, ping } enum AppTab { servers, snippet, ping }

View File

@@ -8,16 +8,20 @@ class Snippet {
late String name; late String name;
@HiveField(1) @HiveField(1)
late String script; late String script;
Snippet(this.name, this.script); @HiveField(2)
List<String>? tags;
Snippet(this.name, this.script, {this.tags});
Snippet.fromJson(Map<String, dynamic> json) { Snippet.fromJson(Map<String, dynamic> json) {
name = json['name'].toString(); name = json['name'].toString();
script = json['script'].toString(); script = json['script'].toString();
tags = json['tags'].cast<String>();
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = <String, dynamic>{}; final data = <String, dynamic>{};
data['name'] = name; data['name'] = name;
data['script'] = script; data['script'] = script;
data['tags'] = tags;
return data; return data;
} }
} }

View File

@@ -19,17 +19,20 @@ class SnippetAdapter extends TypeAdapter<Snippet> {
return Snippet( return Snippet(
fields[0] as String, fields[0] as String,
fields[1] as String, fields[1] as String,
tags: (fields[2] as List?)?.cast<String>(),
); );
} }
@override @override
void write(BinaryWriter writer, Snippet obj) { void write(BinaryWriter writer, Snippet obj) {
writer writer
..writeByte(2) ..writeByte(3)
..writeByte(0) ..writeByte(0)
..write(obj.name) ..write(obj.name)
..writeByte(1) ..writeByte(1)
..write(obj.script); ..write(obj.script)
..writeByte(2)
..write(obj.tags);
} }
@override @override

View File

@@ -15,31 +15,22 @@ class SnippetProvider extends BusyProvider {
} }
void add(Snippet snippet) { void add(Snippet snippet) {
if (have(snippet)) return;
_snippets.add(snippet); _snippets.add(snippet);
_store.put(snippet); _store.put(snippet);
notifyListeners(); notifyListeners();
} }
void del(Snippet snippet) { void del(Snippet snippet) {
if (!have(snippet)) return; _snippets.remove(snippet);
_snippets.removeAt(index(snippet));
_store.delete(snippet); _store.delete(snippet);
notifyListeners(); notifyListeners();
} }
int index(Snippet snippet) {
return _snippets.indexWhere((e) => e.name == snippet.name);
}
bool have(Snippet snippet) {
return index(snippet) != -1;
}
void update(Snippet old, Snippet newOne) { void update(Snippet old, Snippet newOne) {
if (!have(old)) return; _store.delete(old);
_snippets[index(old)] = newOne;
_store.put(newOne); _store.put(newOne);
_snippets.remove(old);
_snippets.add(newOne);
notifyListeners(); notifyListeners();
} }

View File

@@ -44,6 +44,9 @@ class _ConvertPageState extends State<ConvertPage>
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context); super.build(context);
return Scaffold( return Scaffold(
appBar: AppBar(
title: Text(_s.convert),
),
body: SingleChildScrollView( body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 7), padding: const EdgeInsets.symmetric(horizontal: 7),
controller: ScrollController(), controller: ScrollController(),

View File

@@ -29,14 +29,14 @@ import 'setting.dart';
import 'sftp/downloaded.dart'; import 'sftp/downloaded.dart';
import 'snippet/list.dart'; import 'snippet/list.dart';
class MyHomePage extends StatefulWidget { class HomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key); const HomePage({Key? key}) : super(key: key);
@override @override
State<MyHomePage> createState() => _MyHomePageState(); State<HomePage> createState() => _HomePageState();
} }
class _MyHomePageState extends State<MyHomePage> class _HomePageState extends State<HomePage>
with with
AutomaticKeepAliveClientMixin, AutomaticKeepAliveClientMixin,
AfterLayoutMixin, AfterLayoutMixin,
@@ -122,7 +122,7 @@ class _MyHomePageState extends State<MyHomePage>
FocusScope.of(context).requestFocus(FocusNode()); FocusScope.of(context).requestFocus(FocusNode());
}); });
}, },
children: const [ServerPage(), ConvertPage(), PingPage()], children: const [ServerPage(), SnippetListPage(), PingPage()],
), ),
bottomNavigationBar: _buildBottomBar(context), bottomNavigationBar: _buildBottomBar(context),
); );
@@ -151,12 +151,14 @@ class _MyHomePageState extends State<MyHomePage>
selectedIcon: const Icon(Icons.cloud), selectedIcon: const Icon(Icons.cloud),
), ),
NavigationDestination( NavigationDestination(
icon: const Icon(Icons.code), icon: const Icon(Icons.snippet_folder_outlined),
label: _s.convert, label: _s.snippet,
selectedIcon: const Icon(Icons.snippet_folder),
), ),
const NavigationDestination( const NavigationDestination(
icon: Icon(Icons.leak_add), icon: Icon(Icons.network_check_outlined),
label: 'Ping', label: 'Ping',
selectedIcon: Icon(Icons.network_check),
), ),
], ],
); );
@@ -221,11 +223,11 @@ class _MyHomePageState extends State<MyHomePage>
).go(context), ).go(context),
), ),
ListTile( ListTile(
leading: const Icon(Icons.snippet_folder), leading: const Icon(Icons.code),
title: Text(_s.snippet), title: Text(_s.convert),
onTap: () => AppRoute( onTap: () => AppRoute(
const SnippetListPage(), const ConvertPage(),
'snippet list', 'convert page',
).go(context), ).go(context),
), ),
ListTile( ListTile(

View File

@@ -291,7 +291,7 @@ class _SettingPageState extends State<SettingPage> {
.map( .map(
(e) => PopupMenuItem( (e) => PopupMenuItem(
value: e.index, value: e.index,
child: Text(tabTitleName(context, e.index)), child: Text(tabTitleName(context, e)),
), ),
) )
.toList(); .toList();
@@ -316,7 +316,7 @@ class _SettingPageState extends State<SettingPage> {
child: ConstrainedBox( child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: _media.size.width * 0.35), constraints: BoxConstraints(maxWidth: _media.size.width * 0.35),
child: Text( child: Text(
tabTitleName(context, _launchPageIdx), tabTitleName(context, AppTab.values[_launchPageIdx]),
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: textSize15, style: textSize15,
), ),

View File

@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:toolbox/core/extension/navigator.dart'; import 'package:toolbox/core/extension/navigator.dart';
import 'package:toolbox/view/widget/input_field.dart'; import 'package:toolbox/view/widget/input_field.dart';
import 'package:toolbox/view/widget/tag.dart';
import '../../../core/utils/ui.dart'; import '../../../core/utils/ui.dart';
import '../../../data/model/server/snippet.dart'; import '../../../data/model/server/snippet.dart';
@@ -28,6 +29,8 @@ class _SnippetEditPageState extends State<SnippetEditPage>
late SnippetProvider _provider; late SnippetProvider _provider;
late S _s; late S _s;
var _tags = <String>[];
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@@ -53,7 +56,8 @@ class _SnippetEditPageState extends State<SnippetEditPage>
context.pop(); context.pop();
}, },
tooltip: _s.delete, tooltip: _s.delete,
icon: const Icon(Icons.delete)) icon: const Icon(Icons.delete),
)
: placeholder : placeholder
], ],
), ),
@@ -72,7 +76,7 @@ class _SnippetEditPageState extends State<SnippetEditPage>
showSnackBar(context, Text(_s.fieldMustNotEmpty)); showSnackBar(context, Text(_s.fieldMustNotEmpty));
return; return;
} }
final snippet = Snippet(name, script); final snippet = Snippet(name, script, tags: _tags);
if (widget.snippet != null) { if (widget.snippet != null) {
_provider.update(widget.snippet!, snippet); _provider.update(widget.snippet!, snippet);
} else { } else {
@@ -103,6 +107,12 @@ class _SnippetEditPageState extends State<SnippetEditPage>
label: _s.snippet, label: _s.snippet,
icon: Icons.code, icon: Icons.code,
), ),
TagEditor(
tags: widget.snippet?.tags ?? [],
onChanged: (p0) => setState(() {
_tags = p0;
}),
)
], ],
); );
} }
@@ -112,6 +122,7 @@ class _SnippetEditPageState extends State<SnippetEditPage>
if (widget.snippet != null) { if (widget.snippet != null) {
_nameController.text = widget.snippet!.name; _nameController.text = widget.snippet!.name;
_scriptController.text = widget.snippet!.script; _scriptController.text = widget.snippet!.script;
_tags = widget.snippet!.tags ?? [];
} }
} }
} }

View File

@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../../../data/res/ui.dart';
import '/core/route.dart'; import '/core/route.dart';
import '/data/provider/snippet.dart'; import '/data/provider/snippet.dart';
import 'edit.dart'; import 'edit.dart';
@@ -27,9 +26,6 @@ class _SnippetListPageState extends State<SnippetListPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(
title: Text(_s.snippet, style: textSize18),
),
body: _buildBody(), body: _buildBody(),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add), child: const Icon(Icons.add),
@@ -41,8 +37,8 @@ class _SnippetListPageState extends State<SnippetListPage> {
Widget _buildBody() { Widget _buildBody() {
return Consumer<SnippetProvider>( return Consumer<SnippetProvider>(
builder: (_, key, __) { builder: (_, provider, __) {
if (key.snippets.isEmpty) { if (provider.snippets.isEmpty) {
return Center( return Center(
child: Text(_s.noSavedSnippet), child: Text(_s.noSavedSnippet),
); );
@@ -50,19 +46,22 @@ class _SnippetListPageState extends State<SnippetListPage> {
return ListView.builder( return ListView.builder(
padding: const EdgeInsets.all(13), padding: const EdgeInsets.all(13),
itemCount: key.snippets.length, itemCount: provider.snippets.length,
itemBuilder: (context, idx) { itemBuilder: (context, idx) {
return RoundRectCard( return RoundRectCard(
ListTile( ListTile(
contentPadding: const EdgeInsets.only(left: 23, right: 17),
title: Text( title: Text(
key.snippets[idx].name, provider.snippets[idx].name,
overflow: TextOverflow.ellipsis,
maxLines: 1,
), ),
trailing: TextButton( trailing: IconButton(
onPressed: () => AppRoute( onPressed: () => AppRoute(
SnippetEditPage(snippet: key.snippets[idx]), SnippetEditPage(snippet: provider.snippets[idx]),
'snippet edit page') 'snippet edit page')
.go(context), .go(context),
child: Text(_s.edit), icon: const Icon(Icons.edit),
), ),
), ),
); );

View File

@@ -9,12 +9,15 @@ class Input extends StatelessWidget {
final String? hint; final String? hint;
final String? label; final String? label;
final void Function(String)? onSubmitted; final void Function(String)? onSubmitted;
final void Function(String)? onChanged;
final bool obscureText; final bool obscureText;
final IconData? icon; final IconData? icon;
final TextInputType? type; final TextInputType? type;
final FocusNode? node; final FocusNode? node;
final bool autoCorrect; final bool autoCorrect;
final bool suggestiion; final bool suggestiion;
final String? errorText;
final Widget? prefix;
const Input({ const Input({
super.key, super.key,
@@ -24,12 +27,15 @@ class Input extends StatelessWidget {
this.hint, this.hint,
this.label, this.label,
this.onSubmitted, this.onSubmitted,
this.onChanged,
this.obscureText = false, this.obscureText = false,
this.icon, this.icon,
this.type, this.type,
this.node, this.node,
this.autoCorrect = false, this.autoCorrect = false,
this.suggestiion = false, this.suggestiion = false,
this.errorText,
this.prefix,
}); });
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -40,16 +46,18 @@ class Input extends StatelessWidget {
maxLines: maxLines, maxLines: maxLines,
minLines: minLines, minLines: minLines,
onSubmitted: onSubmitted, onSubmitted: onSubmitted,
onChanged: onChanged,
keyboardType: type, keyboardType: type,
focusNode: node, focusNode: node,
autocorrect: autoCorrect, autocorrect: autoCorrect,
enableSuggestions: suggestiion, enableSuggestions: suggestiion,
decoration: InputDecoration( decoration: InputDecoration(
label: label != null ? Text(label!) : null, label: label != null ? Text(label!) : null,
hintText: hint, hintText: hint,
icon: icon != null ? Icon(icon) : null, icon: icon != null ? Icon(icon) : null,
border: InputBorder.none, border: InputBorder.none,
), errorText: errorText,
prefix: prefix),
controller: controller, controller: controller,
obscureText: obscureText, obscureText: obscureText,
), ),

115
lib/view/widget/tag.dart Normal file
View File

@@ -0,0 +1,115 @@
import 'package:flutter/material.dart';
import 'package:toolbox/view/widget/input_field.dart';
import 'package:toolbox/view/widget/round_rect_card.dart';
import '../../data/res/color.dart';
class TagEditor extends StatelessWidget {
final List<String> tags;
final void Function(List<String>)? onChanged;
const TagEditor({super.key, required this.tags, this.onChanged});
@override
Widget build(BuildContext context) {
return RoundRectCard(ListTile(
leading: const Icon(Icons.tag),
title: _buildTags(
tags,
_onTapDelete,
),
trailing: InkWell(
child: const Icon(Icons.add),
onTap: () {
_showTagDialog(context, tags, onChanged);
},
),
));
}
void _onTapDelete(String tag) {
tags.remove(tag);
onChanged?.call(tags);
}
Widget _buildTags(
List<String> tags,
Function(String) onTagDelete,
) {
if (tags.isEmpty) return Text('Tags');
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: tags.map((e) => _buildTagItem(e, onTagDelete)).toList(),
),
);
}
Widget _buildTagItem(String tag, Function(String) onTagDelete) {
return Padding(
padding: EdgeInsets.only(right: 7),
child: Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(
Radius.circular(20.0),
),
color: primaryColor,
),
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 3),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'#$tag',
style: const TextStyle(color: Colors.white),
),
const SizedBox(width: 4.0),
InkWell(
child: const Icon(
Icons.cancel,
size: 14.0,
color: Colors.white,
),
onTap: () {
onTagDelete(tag);
},
)
],
),
),
);
}
void _showTagDialog(
BuildContext context,
List<String> tags,
void Function(List<String>)? onChanged,
) {
final _textEditingController = TextEditingController();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Add Tag'),
content: Input(
controller: _textEditingController,
hint: 'Tag',
),
actions: [
TextButton(
onPressed: () {
final tag = _textEditingController.text;
tags.add(tag.trim());
onChanged?.call(tags);
Navigator.pop(context);
},
child: const Text('Add'),
),
],
);
},
);
}
}