Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
142 lines
3.9 KiB
Dart
142 lines
3.9 KiB
Dart
import 'package:cutdex/l10n/l10n.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:cutdex_ui/theme.dart';
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
|
|
Future<String?> askName(
|
|
BuildContext context, {
|
|
required String title,
|
|
required String label,
|
|
String? description,
|
|
String initialValue = '',
|
|
String? submitLabel,
|
|
Future<String?> Function(String name)? validateName,
|
|
}) => showAppDialog<String>(
|
|
context: context,
|
|
builder: (_) => _NameDialog(
|
|
title: title,
|
|
label: label,
|
|
description: description,
|
|
initialValue: initialValue,
|
|
submitLabel: submitLabel,
|
|
validateName: validateName,
|
|
),
|
|
);
|
|
|
|
class _NameDialog extends StatefulWidget {
|
|
const _NameDialog({
|
|
required this.title,
|
|
required this.label,
|
|
this.description,
|
|
required this.initialValue,
|
|
this.submitLabel,
|
|
this.validateName,
|
|
});
|
|
final String title;
|
|
final String label;
|
|
final String? description;
|
|
final String initialValue;
|
|
final String? submitLabel;
|
|
final Future<String?> Function(String name)? validateName;
|
|
@override
|
|
State<_NameDialog> createState() => _NameDialogState();
|
|
}
|
|
|
|
class _NameDialogState extends State<_NameDialog> {
|
|
late final _controller = TextEditingController(text: widget.initialValue)
|
|
..selection = TextSelection(
|
|
baseOffset: 0,
|
|
extentOffset: widget.initialValue.length,
|
|
);
|
|
final _form = GlobalKey<FormState>();
|
|
bool _validating = false;
|
|
String? _validationError;
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (_validating) return;
|
|
_validationError = null;
|
|
if (!_form.currentState!.validate()) return;
|
|
final name = _controller.text.trim();
|
|
if (widget.validateName != null) {
|
|
setState(() => _validating = true);
|
|
String? error;
|
|
try {
|
|
error = await widget.validateName!(name);
|
|
} catch (_) {
|
|
if (!mounted) return;
|
|
error = context.l10n.operationFailed;
|
|
}
|
|
if (!mounted || ModalRoute.of(context)?.isCurrent != true) return;
|
|
setState(() {
|
|
_validating = false;
|
|
_validationError = error;
|
|
});
|
|
if (error != null) {
|
|
_form.currentState!.validate();
|
|
return;
|
|
}
|
|
}
|
|
if (mounted) Navigator.pop(context, name);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => AppDialog(
|
|
title: widget.title,
|
|
description: widget.description,
|
|
width: 420,
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: Text(context.l10n.cancel),
|
|
),
|
|
FilledButton(
|
|
onPressed: _validating ? null : _submit,
|
|
child: Text(widget.submitLabel ?? context.l10n.create),
|
|
),
|
|
],
|
|
child: Builder(
|
|
builder: (dialogContext) => Form(
|
|
key: _form,
|
|
child: TextFormField(
|
|
controller: _controller,
|
|
readOnly: _validating,
|
|
style: AppTheme.inputTextStyle(dialogContext),
|
|
cursorHeight: AppTheme.inputCursorHeight(dialogContext),
|
|
textAlignVertical: TextAlignVertical.center,
|
|
autofocus: true,
|
|
maxLength: 80,
|
|
buildCounter: (
|
|
_, {
|
|
required currentLength,
|
|
required isFocused,
|
|
maxLength,
|
|
}) => null,
|
|
decoration: InputDecoration(
|
|
hintText: widget.label,
|
|
hintStyle: AppTheme.inputHintStyle(dialogContext),
|
|
errorMaxLines: 3,
|
|
),
|
|
validator: (value) => value == null || value.trim().isEmpty
|
|
? context.l10n.enterName
|
|
: RegExp(r'[/\\\x00-\x1F]').hasMatch(value) ||
|
|
['.', '..'].contains(value.trim())
|
|
? context.l10n.invalidName
|
|
: _validationError,
|
|
onChanged: (_) {
|
|
if (_validationError == null) return;
|
|
_validationError = null;
|
|
_form.currentState!.validate();
|
|
},
|
|
onFieldSubmitted: (_) => _submit(),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|