Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
202 lines
6.1 KiB
Dart
202 lines
6.1 KiB
Dart
import 'package:cutdex/l10n/l10n.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../shared/state/operation_scope.dart';
|
|
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
import 'package:cutdex_ui/theme.dart';
|
|
|
|
class PropertyNumberInput extends StatefulWidget {
|
|
const PropertyNumberInput({
|
|
super.key,
|
|
required this.label,
|
|
required this.value,
|
|
required this.enabled,
|
|
required this.readOnly,
|
|
required this.submit,
|
|
this.unit,
|
|
this.inlineLabel = false,
|
|
this.prefix,
|
|
this.prefixIcon,
|
|
this.prefixQuarterTurns = 0,
|
|
this.valueText,
|
|
this.labelActions = const [],
|
|
this.decimalPlaces = 2,
|
|
this.onScrubStart,
|
|
required this.onScrubUpdate,
|
|
required this.onScrubEnd,
|
|
required this.onScrubCancel,
|
|
required this.scrubbing,
|
|
required this.editContext,
|
|
}) : assert(decimalPlaces >= 0 && decimalPlaces <= 20);
|
|
final String? label;
|
|
final String? unit;
|
|
final bool inlineLabel;
|
|
final String? prefix;
|
|
final IconData? prefixIcon;
|
|
final int prefixQuarterTurns;
|
|
final String? valueText;
|
|
final List<Widget> labelActions;
|
|
final int decimalPlaces;
|
|
final bool Function()? onScrubStart;
|
|
final ValueChanged<double> onScrubUpdate;
|
|
final VoidCallback onScrubEnd;
|
|
final VoidCallback onScrubCancel;
|
|
final bool scrubbing;
|
|
final Object editContext;
|
|
final double value;
|
|
final bool enabled;
|
|
final bool readOnly;
|
|
final Future<bool> Function(double) submit;
|
|
@override
|
|
State<PropertyNumberInput> createState() => _PropertyNumberInputState();
|
|
}
|
|
|
|
class _PropertyNumberInputState extends State<PropertyNumberInput> {
|
|
late final _text = TextEditingController(text: _displayValue);
|
|
final _focus = FocusNode();
|
|
bool _dirty = false;
|
|
late final _operations = OperationScope<Object>(widget.editContext);
|
|
String? _error;
|
|
String get _displayValue => widget.valueText ?? _format(widget.value);
|
|
String _format(double value) {
|
|
var text = value.toStringAsFixed(widget.decimalPlaces);
|
|
if (text.contains('.')) text = text.replaceFirst(RegExp(r'\.?0+$'), '');
|
|
return text == '-0' ? '0' : text;
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_focus.addListener(_blur);
|
|
}
|
|
|
|
void _blur() {
|
|
if (!_focus.hasFocus) _submit();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_dirty || !widget.enabled || widget.readOnly) return;
|
|
final value = double.tryParse(_text.text);
|
|
if (value == null || !value.isFinite) {
|
|
setState(() => _error = context.l10n.invalidNumber);
|
|
return;
|
|
}
|
|
_dirty = false;
|
|
final isCurrent = _operations.begin();
|
|
final accepted = await widget.submit(value);
|
|
if (!mounted || !isCurrent()) return;
|
|
setState(() {
|
|
_error = accepted ? null : context.l10n.notApplied;
|
|
_text.text = _displayValue;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant PropertyNumberInput oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
_operations.bind(widget.editContext);
|
|
if ((oldWidget.enabled && !widget.enabled) ||
|
|
oldWidget.editContext != widget.editContext ||
|
|
(!oldWidget.readOnly && widget.readOnly)) {
|
|
_dirty = false;
|
|
_error = null;
|
|
}
|
|
final text = _displayValue;
|
|
// A no-op assignment still resets selection and asks EditableText to
|
|
// reveal its caret, competing with the user's scroll on preview updates.
|
|
if (!_dirty && _text.text != text) _text.text = text;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_operations.dispose();
|
|
_focus.removeListener(_blur);
|
|
_focus.dispose();
|
|
_text.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final compact = AppTheme.isCompactInspector(context);
|
|
final labelAbove = compact && !widget.inlineLabel;
|
|
final label = widget.onScrubStart == null
|
|
? Text(widget.label ?? '', style: AppTheme.propertyLabelStyle(context))
|
|
: AppScrubLabel(
|
|
label: widget.prefix ?? widget.label!,
|
|
icon: widget.prefixIcon,
|
|
style: labelAbove
|
|
? AppTheme.propertySectionTitleStyle(context)
|
|
: null,
|
|
alignment: labelAbove ? Alignment.centerLeft : Alignment.center,
|
|
padding: labelAbove || compact
|
|
? EdgeInsets.zero
|
|
: const EdgeInsets.symmetric(horizontal: AppSpacing.small),
|
|
enabled: widget.enabled && (!widget.readOnly || widget.scrubbing),
|
|
onStart: () {
|
|
_operations.invalidate();
|
|
_dirty = false;
|
|
_error = null;
|
|
_text.text = _displayValue;
|
|
return widget.onScrubStart!();
|
|
},
|
|
onUpdate: widget.onScrubUpdate,
|
|
onEnd: widget.onScrubEnd,
|
|
onCancel: widget.onScrubCancel,
|
|
);
|
|
final input = AppTextField(
|
|
controller: _text,
|
|
focusNode: _focus,
|
|
enabled: widget.enabled,
|
|
readOnly: widget.readOnly,
|
|
canRequestFocus: widget.enabled && !widget.readOnly,
|
|
keyboardType: const TextInputType.numberWithOptions(
|
|
decimal: true,
|
|
signed: true,
|
|
),
|
|
label: widget.label,
|
|
labelAbove: labelAbove,
|
|
labelActions: widget.labelActions,
|
|
leading: widget.onScrubStart == null
|
|
? null
|
|
: compact && !labelAbove
|
|
? SizedBox(
|
|
width: AppInspectorMetrics.action,
|
|
child: RotatedBox(
|
|
quarterTurns: widget.prefixQuarterTurns,
|
|
child: label,
|
|
),
|
|
)
|
|
: label,
|
|
unit: widget.unit,
|
|
errorText: _error,
|
|
onChanged: (_) {
|
|
_operations.invalidate();
|
|
_dirty = true;
|
|
if (_error != null) setState(() => _error = null);
|
|
},
|
|
onEditingComplete: () {},
|
|
onSubmitted: (_) => _submit(),
|
|
);
|
|
return Focus(
|
|
onKeyEvent: (_, event) {
|
|
if (event is KeyDownEvent &&
|
|
event.logicalKey == LogicalKeyboardKey.escape) {
|
|
setState(() {
|
|
_operations.invalidate();
|
|
_dirty = false;
|
|
_error = null;
|
|
_text.text = _displayValue;
|
|
});
|
|
_focus.unfocus();
|
|
return KeyEventResult.handled;
|
|
}
|
|
return KeyEventResult.ignored;
|
|
},
|
|
child: input,
|
|
);
|
|
}
|
|
}
|