mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-03-06 16:46:35 +00:00
* 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
177 lines
5.8 KiB
Dart
177 lines
5.8 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:mobile_nebula/components/CIDRField.dart';
|
|
import 'package:mobile_nebula/models/CIDR.dart';
|
|
import 'package:mobile_nebula/validators/ipValidator.dart';
|
|
|
|
class CIDRFormField extends FormField<CIDR> {
|
|
//TODO: onSaved, validator, auto-validate, enabled?
|
|
CIDRFormField({
|
|
super.key,
|
|
autoFocus = false,
|
|
enableIPV6 = false,
|
|
focusNode,
|
|
nextFocusNode,
|
|
ValueChanged<CIDR>? onChanged,
|
|
super.onSaved,
|
|
textInputAction,
|
|
super.initialValue,
|
|
this.ipController,
|
|
this.bitsController,
|
|
}) : super(
|
|
validator: (cidr) {
|
|
if (cidr == null) {
|
|
return "Please fill out this field";
|
|
}
|
|
|
|
if (!ipValidator(cidr.ip, enableIPV6)) {
|
|
return 'Please enter a valid ip address';
|
|
}
|
|
|
|
if (cidr.bits > 32 || cidr.bits < 0) {
|
|
return "Please enter a valid number of bits";
|
|
}
|
|
|
|
return null;
|
|
},
|
|
builder: (FormFieldState<CIDR> field) {
|
|
final _CIDRFormField state = field as _CIDRFormField;
|
|
|
|
void onChangedHandler(CIDR value) {
|
|
if (onChanged != null) {
|
|
onChanged(value);
|
|
}
|
|
field.didChange(value);
|
|
}
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: <Widget>[
|
|
CIDRField(
|
|
autoFocus: autoFocus,
|
|
focusNode: focusNode,
|
|
nextFocusNode: nextFocusNode,
|
|
onChanged: onChangedHandler,
|
|
textInputAction: textInputAction,
|
|
ipController: state._effectiveIPController,
|
|
bitsController: state._effectiveBitsController,
|
|
),
|
|
field.hasError
|
|
? Text(
|
|
field.errorText ?? "Unknown error",
|
|
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(field.context), fontSize: 13),
|
|
textAlign: TextAlign.end,
|
|
)
|
|
: Container(height: 0),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
|
|
final TextEditingController? ipController;
|
|
final TextEditingController? bitsController;
|
|
|
|
@override
|
|
_CIDRFormField createState() => _CIDRFormField();
|
|
}
|
|
|
|
class _CIDRFormField extends FormFieldState<CIDR> {
|
|
TextEditingController? _ipController = TextEditingController();
|
|
TextEditingController? _bitsController = TextEditingController();
|
|
|
|
TextEditingController get _effectiveIPController => widget.ipController ?? _ipController!;
|
|
TextEditingController get _effectiveBitsController => widget.bitsController ?? _bitsController!;
|
|
|
|
@override
|
|
CIDRFormField get widget => super.widget as CIDRFormField;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.ipController == null) {
|
|
_ipController = TextEditingController(text: widget.initialValue?.ip);
|
|
} else {
|
|
widget.ipController!.addListener(_handleControllerChanged);
|
|
}
|
|
|
|
if (widget.bitsController == null) {
|
|
_bitsController = TextEditingController(text: widget.initialValue?.bits.toString() ?? "");
|
|
} else {
|
|
widget.bitsController!.addListener(_handleControllerChanged);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(CIDRFormField oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
var update = CIDR(ip: widget.ipController?.text ?? "", bits: int.tryParse(widget.bitsController?.text ?? "") ?? 0);
|
|
bool shouldUpdate = false;
|
|
|
|
if (widget.ipController != oldWidget.ipController) {
|
|
oldWidget.ipController?.removeListener(_handleControllerChanged);
|
|
widget.ipController?.addListener(_handleControllerChanged);
|
|
|
|
if (oldWidget.ipController != null && widget.ipController == null) {
|
|
_ipController = TextEditingController.fromValue(oldWidget.ipController!.value);
|
|
}
|
|
|
|
if (widget.ipController != null) {
|
|
shouldUpdate = true;
|
|
update.ip = widget.ipController!.text;
|
|
if (oldWidget.ipController == null) _ipController = null;
|
|
}
|
|
}
|
|
|
|
if (widget.bitsController != oldWidget.bitsController) {
|
|
oldWidget.bitsController?.removeListener(_handleControllerChanged);
|
|
widget.bitsController?.addListener(_handleControllerChanged);
|
|
|
|
if (oldWidget.bitsController != null && widget.bitsController == null) {
|
|
_bitsController = TextEditingController.fromValue(oldWidget.bitsController!.value);
|
|
}
|
|
|
|
if (widget.bitsController != null) {
|
|
shouldUpdate = true;
|
|
update.bits = int.parse(widget.bitsController!.text);
|
|
if (oldWidget.bitsController == null) _bitsController = null;
|
|
}
|
|
}
|
|
|
|
if (shouldUpdate) {
|
|
setValue(update);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.ipController?.removeListener(_handleControllerChanged);
|
|
widget.bitsController?.removeListener(_handleControllerChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void reset() {
|
|
super.reset();
|
|
setState(() {
|
|
_effectiveIPController.text = widget.initialValue?.ip ?? "";
|
|
_effectiveBitsController.text = widget.initialValue?.bits.toString() ?? "";
|
|
});
|
|
}
|
|
|
|
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.
|
|
final effectiveBits = int.parse(_effectiveBitsController.text);
|
|
if (value == null) {
|
|
return;
|
|
}
|
|
|
|
if (_effectiveIPController.text != value!.ip || effectiveBits != value!.bits) {
|
|
didChange(CIDR(ip: _effectiveIPController.text, bits: effectiveBits));
|
|
}
|
|
}
|
|
}
|