mobile_nebula/lib/components/PlatformTextFormField.dart
Caleb Jasik 2b844d27dd
Add Flutter lint (#253)
* Enable `flutter_lints` linting

* Fix unmarked deps, we aren't on web so we don't need a URL strategy

* ` dart fix --apply --code=use_super_parameters`

* `dart fix --apply --code=use_key_in_widget_constructors`

* `dart fix --apply --code=use_function_type_syntax_for_parameters`

* Ignore code-generated `lib/services/theme.dart` file

* `dart fix --apply --code=unnecessary_this`

* `dart fix --apply --code=unnecessary_null_in_if_null_operators`

* `dart fix --apply --code=unnecessary_new`

* `dart fix --apply --code=sort_child_properties_last`

* `dart fix --apply --code=sized_box_for_whitespace`

* `dart fix --apply --code=prefer_typing_uninitialized_variables`

* `dart fix --apply --code=prefer_is_empty`

* `dart fix --apply --code=prefer_interpolation_to_compose_strings`

* `dart fix --apply --code=prefer_final_fields`

* `dart fix --apply --code=prefer_const_constructors_in_immutables`

* `dart fix --apply --code=prefer_collection_literals`

* `dart fix --apply --code=no_leading_underscores_for_local_identifiers`

* `dart fix --apply --code=curly_braces_in_flow_control_structures`

* `dart fix --apply --code=avoid_function_literals_in_foreach_calls`

* `dart fix --apply --code=annotate_overrides`

* Add CI for dart linting

* `dart format lib/`

* Re-enable the `usePathUrlStrategy` call, with proper deps

https://docs.flutter.dev/ui/navigation/url-strategies#configuring-the-url-strategy
2025-03-04 11:29:23 -06:00

152 lines
4.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:mobile_nebula/components/SpecialTextField.dart';
//TODO: reset doesn't update the ui but clears the field
class PlatformTextFormField extends FormField<String> {
//TODO: auto-validate, enabled?
PlatformTextFormField({
super.key,
widgetKey,
this.controller,
focusNode,
nextFocusNode,
TextInputType? keyboardType,
textInputAction,
List<TextInputFormatter>? inputFormatters,
textAlign,
autofocus,
maxLines = 1,
maxLength,
maxLengthEnforcement,
onChanged,
keyboardAppearance,
minLines,
expands,
suffix,
textAlignVertical,
String? initialValue,
String? placeholder,
FormFieldValidator<String>? validator,
super.onSaved,
}) : super(
initialValue: controller != null ? controller.text : (initialValue ?? ''),
validator: (str) {
if (validator != null) {
return validator(str);
}
return null;
},
builder: (FormFieldState<String> field) {
final _PlatformTextFormFieldState state = field as _PlatformTextFormFieldState;
void onChangedHandler(String value) {
if (onChanged != null) {
onChanged(value);
}
field.didChange(value);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SpecialTextField(
key: widgetKey,
controller: state._effectiveController,
focusNode: focusNode,
nextFocusNode: nextFocusNode,
keyboardType: keyboardType,
textInputAction: textInputAction,
textAlign: textAlign,
autofocus: autofocus,
maxLines: maxLines,
maxLength: maxLength,
maxLengthEnforcement: maxLengthEnforcement,
onChanged: onChangedHandler,
keyboardAppearance: keyboardAppearance,
minLines: minLines,
expands: expands,
textAlignVertical: textAlignVertical,
placeholder: placeholder,
inputFormatters: inputFormatters,
suffix: suffix,
),
field.hasError
? Text(
field.errorText!,
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(field.context), fontSize: 13),
textAlign: textAlign,
)
: Container(height: 0),
],
);
},
);
final TextEditingController? controller;
@override
_PlatformTextFormFieldState createState() => _PlatformTextFormFieldState();
}
class _PlatformTextFormFieldState extends FormFieldState<String> {
TextEditingController? _controller;
TextEditingController get _effectiveController => widget.controller ?? _controller!;
@override
PlatformTextFormField get widget => super.widget as PlatformTextFormField;
@override
void initState() {
super.initState();
if (widget.controller == null) {
_controller = TextEditingController(text: widget.initialValue);
} else {
widget.controller!.addListener(_handleControllerChanged);
}
}
@override
void didUpdateWidget(PlatformTextFormField oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
oldWidget.controller?.removeListener(_handleControllerChanged);
widget.controller?.addListener(_handleControllerChanged);
if (oldWidget.controller != null && widget.controller == null) {
_controller = TextEditingController.fromValue(oldWidget.controller!.value);
}
if (widget.controller != null) {
setValue(widget.controller!.text);
if (oldWidget.controller == null) _controller = null;
}
}
}
@override
void dispose() {
widget.controller?.removeListener(_handleControllerChanged);
super.dispose();
}
@override
void reset() {
super.reset();
setState(() {
_effectiveController.text = widget.initialValue ?? "";
});
}
void _handleControllerChanged() {
// Suppress changes that originated from within this class.
//
// In the case where a controller has been passed in to this widget, we
// register this change listener. In these cases, we'll also receive change
// notifications for changes originating from within this class -- for
// example, the reset() method. In such cases, the FormField value will
// already have been set.
if (_effectiveController.text != value) didChange(_effectiveController.text);
}
}