@@ -14,31 +14,67 @@ import 'package:phosphor_flutter/phosphor_flutter.dart';
1414import '../../bloc/document_bloc.dart' ;
1515import 'pack.dart' ;
1616
17- class AssetDialog extends StatelessWidget {
17+ class AssetDialog extends StatefulWidget {
1818 final PackAssetLocation ? value;
1919 final String initialName;
2020
2121 const AssetDialog ({super .key, this .value, this .initialName = '' });
2222
23+ @override
24+ State <AssetDialog > createState () => _AssetDialogState ();
25+ }
26+
27+ class _AssetDialogState extends State <AssetDialog > {
28+ late final ButterflyFileSystem _fileSystem;
29+ late final PackFileSystem _packSystem;
30+ late Future <List <FileSystemFile <NoteData >>> _packsFuture;
31+ late String _name;
32+ String ? _pack;
33+
34+ @override
35+ void initState () {
36+ super .initState ();
37+ _fileSystem = context.read <ButterflyFileSystem >();
38+ _packSystem = _fileSystem.buildDefaultPackSystem ();
39+ _packsFuture = _getPacks ();
40+ _pack = widget.value? .namespace;
41+ _name = widget.value? .key ?? widget.initialName;
42+ }
43+
44+ Future <List <FileSystemFile <NoteData >>> _getPacks () =>
45+ _packSystem.initialize ().then ((_) => _packSystem.getFiles ());
46+
47+ Future <void > _createPack () async {
48+ final pack = await showDialog <NoteData >(
49+ context: context,
50+ builder: (context) => const PackDialog (),
51+ );
52+ if (pack == null ) return ;
53+ final createdPath = await _fileSystem.createPack (
54+ pack,
55+ storage: _packSystem.storage,
56+ );
57+ final packs = await _packSystem.getFiles ();
58+ if (! mounted) return ;
59+ setState (() {
60+ _pack = createdPath;
61+ _packsFuture = Future .value (packs);
62+ });
63+ }
64+
2365 @override
2466 Widget build (BuildContext context) {
25- String ? pack = value? .namespace;
26- String name = value? .key ?? initialName;
27- final bloc = context.read <DocumentBloc >();
28- final packSystem = context
29- .read <ButterflyFileSystem >()
30- .buildDefaultPackSystem ();
3167 return FutureBuilder <List <FileSystemFile <NoteData >>>(
32- future: packSystem. initialize (). then ((_) => packSystem. getFiles ()) ,
68+ future: _packsFuture ,
3369 builder: (context, snapshot) => BlocBuilder <DocumentBloc , DocumentState >(
3470 buildWhen: (previous, current) => previous.data != current.data,
3571 builder: (context, state) {
3672 if (state is ! DocumentLoaded ) return const SizedBox ();
3773 final packs = snapshot.data ?? < FileSystemFile <NoteData >> [];
38- pack ?? = packs.firstOrNull? .path;
74+ _pack ?? = packs.firstOrNull? .path;
3975 return AlertDialog (
4076 title: Text (
41- value == null
77+ widget. value == null
4278 ? AppLocalizations .of (context).addAsset
4379 : AppLocalizations .of (context).editAsset,
4480 ),
@@ -62,24 +98,15 @@ class AssetDialog extends StatelessWidget {
6298 ),
6399 );
64100 }).toList (),
65- onSelected: (value) {
66- pack = value;
67- },
68- initialSelection: pack,
101+ onSelected: (value) => _pack = value,
102+ initialSelection: _pack,
69103 expandedInsets: const EdgeInsets .all (0 ),
70104 ),
71105 ),
72106 const SizedBox (width: 8 ),
73107 IconButton (
74108 icon: const PhosphorIcon (PhosphorIconsLight .plusCircle),
75- onPressed: () async {
76- final pack = await showDialog <NoteData >(
77- context: context,
78- builder: (context) => const PackDialog (),
79- );
80- if (pack == null ) return ;
81- bloc.add (PackAdded (pack));
82- },
109+ onPressed: _createPack,
83110 tooltip: AppLocalizations .of (context).createPack,
84111 ),
85112 ],
@@ -90,10 +117,8 @@ class AssetDialog extends StatelessWidget {
90117 labelText: LeapLocalizations .of (context).name,
91118 filled: true ,
92119 ),
93- initialValue: name,
94- onChanged: (value) {
95- name = value;
96- },
120+ initialValue: _name,
121+ onChanged: (value) => _name = value,
97122 ),
98123 ],
99124 ),
@@ -108,8 +133,8 @@ class AssetDialog extends StatelessWidget {
108133 ),
109134 ElevatedButton (
110135 onPressed: () {
111- if (pack == null ) return ;
112- Navigator .of (context).pop (PackAssetLocation (pack ! , name ));
136+ if (_pack == null ) return ;
137+ Navigator .of (context).pop (PackAssetLocation (_pack ! , _name ));
113138 },
114139 child: Text (MaterialLocalizations .of (context).okButtonLabel),
115140 ),
0 commit comments