Files
cutdex/lib/features/properties/property_number_input.dart
T
lingjhf abc1868550
Gitea CI / Check code and tests (push) Successful in 6m37s
Gitea CI / Build Windows x64 app (push) Successful in 3m18s
Gitea CI / Build Android APK (push) Successful in 3m47s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m7s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 50s
Gitea CI / Gitea CI passed (push) Successful in 2s
Gitea Release / Prepare draft Release (push) Successful in 9s
Gitea Release / Upload Android to draft Release (push) Successful in 16s
Gitea Release / Upload Windows to draft Release (push) Successful in 22s
Gitea Release / Verify and publish Release (push) Successful in 14s
feat(workspace): 桌面工作区、主题与交互更新,发布 0.1.2
2026-10-08 18:17:12 +00:00

187 lines
5.7 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.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 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: _format(widget.value));
final _focus = FocusNode();
bool _dirty = false;
late final _operations = OperationScope<Object>(widget.editContext);
String? _error;
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 = _format(widget.value);
});
}
@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 = _format(widget.value);
// 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 labelAbove =
AppTheme.isCompactInspector(context) && !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
? EdgeInsets.zero
: const EdgeInsets.symmetric(horizontal: AppSpacing.small),
enabled: widget.enabled && (!widget.readOnly || widget.scrubbing),
onStart: () {
_operations.invalidate();
_dirty = false;
_error = null;
_text.text = _format(widget.value);
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 : 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 = _format(widget.value);
});
_focus.unfocus();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: input,
);
}
}