new album: removed name init, validate form to create
This commit is contained in:
parent
0e42568d85
commit
a56ed27d0e
1 changed files with 16 additions and 21 deletions
|
@ -15,6 +15,7 @@ class CreateAlbumDialog extends StatefulWidget {
|
||||||
class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
final TextEditingController _nameController = TextEditingController();
|
final TextEditingController _nameController = TextEditingController();
|
||||||
final ValueNotifier<bool> _existsNotifier = ValueNotifier(false);
|
final ValueNotifier<bool> _existsNotifier = ValueNotifier(false);
|
||||||
|
final ValueNotifier<bool> _isValidNotifier = ValueNotifier(false);
|
||||||
Set<StorageVolume> _allVolumes;
|
Set<StorageVolume> _allVolumes;
|
||||||
StorageVolume _primaryVolume, _selectedVolume;
|
StorageVolume _primaryVolume, _selectedVolume;
|
||||||
|
|
||||||
|
@ -24,7 +25,6 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
_allVolumes = androidFileUtils.storageVolumes;
|
_allVolumes = androidFileUtils.storageVolumes;
|
||||||
_primaryVolume = _allVolumes.firstWhere((volume) => volume.isPrimary, orElse: () => _allVolumes.first);
|
_primaryVolume = _allVolumes.firstWhere((volume) => volume.isPrimary, orElse: () => _allVolumes.first);
|
||||||
_selectedVolume = _primaryVolume;
|
_selectedVolume = _primaryVolume;
|
||||||
_initAlbumName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -48,7 +48,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
groupValue: _selectedVolume,
|
groupValue: _selectedVolume,
|
||||||
onChanged: (volume) {
|
onChanged: (volume) {
|
||||||
_selectedVolume = volume;
|
_selectedVolume = volume;
|
||||||
_checkAlbumExists();
|
_validate();
|
||||||
setState(() {});
|
setState(() {});
|
||||||
},
|
},
|
||||||
title: Text(
|
title: Text(
|
||||||
|
@ -67,7 +67,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
SizedBox(height: 8),
|
SizedBox(height: 8),
|
||||||
],
|
],
|
||||||
Padding(
|
Padding(
|
||||||
padding: AvesDialog.contentHorizontalPadding,
|
padding: AvesDialog.contentHorizontalPadding + EdgeInsets.only(bottom: 8),
|
||||||
child: ValueListenableBuilder<bool>(
|
child: ValueListenableBuilder<bool>(
|
||||||
valueListenable: _existsNotifier,
|
valueListenable: _existsNotifier,
|
||||||
builder: (context, exists, child) {
|
builder: (context, exists, child) {
|
||||||
|
@ -75,8 +75,9 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
helperText: exists ? 'Album already exists' : '',
|
helperText: exists ? 'Album already exists' : '',
|
||||||
|
hintText: 'Album name',
|
||||||
),
|
),
|
||||||
onChanged: (_) => _checkAlbumExists(),
|
onChanged: (_) => _validate(),
|
||||||
onSubmitted: (_) => _submit(context),
|
onSubmitted: (_) => _submit(context),
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
|
@ -87,10 +88,15 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: () => Navigator.pop(context),
|
||||||
child: Text('Cancel'.toUpperCase()),
|
child: Text('Cancel'.toUpperCase()),
|
||||||
),
|
),
|
||||||
FlatButton(
|
ValueListenableBuilder<bool>(
|
||||||
onPressed: () => _submit(context),
|
valueListenable: _isValidNotifier,
|
||||||
child: Text('Create'.toUpperCase()),
|
builder: (context, isValid, child) {
|
||||||
),
|
return FlatButton(
|
||||||
|
onPressed: isValid ? () => _submit(context) : null,
|
||||||
|
child: Text('Create'.toUpperCase()),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -100,21 +106,10 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
return join(_selectedVolume.path, 'Pictures', name);
|
return join(_selectedVolume.path, 'Pictures', name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initAlbumName() async {
|
Future<void> _validate() async {
|
||||||
var count = 1;
|
|
||||||
while (true) {
|
|
||||||
var name = 'Album $count';
|
|
||||||
if (!await Directory(_buildAlbumPath(name)).exists()) {
|
|
||||||
_nameController.text = name;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _checkAlbumExists() async {
|
|
||||||
final path = _buildAlbumPath(_nameController.text);
|
final path = _buildAlbumPath(_nameController.text);
|
||||||
_existsNotifier.value = path.isEmpty ? false : await Directory(path).exists();
|
_existsNotifier.value = path.isEmpty ? false : await Directory(path).exists();
|
||||||
|
_isValidNotifier.value = (_nameController.text ?? '').isNotEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _submit(BuildContext context) => Navigator.pop(context, _buildAlbumPath(_nameController.text));
|
void _submit(BuildContext context) => Navigator.pop(context, _buildAlbumPath(_nameController.text));
|
||||||
|
|
Loading…
Reference in a new issue