2020-07-27 20:43:58 +00:00
|
|
|
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> {
|
2022-07-27 16:38:02 +00:00
|
|
|
//TODO: onSaved, validator, auto-validate, enabled?
|
2020-07-27 20:43:58 +00:00
|
|
|
CIDRFormField({
|
2022-09-21 20:27:35 +00:00
|
|
|
Key? key,
|
2020-07-27 20:43:58 +00:00
|
|
|
autoFocus = false,
|
2021-04-23 21:23:06 +00:00
|
|
|
enableIPV6 = false,
|
2020-07-27 20:43:58 +00:00
|
|
|
focusNode,
|
|
|
|
nextFocusNode,
|
2022-09-21 20:27:35 +00:00
|
|
|
ValueChanged<CIDR>? onChanged,
|
|
|
|
FormFieldSetter<CIDR>? onSaved,
|
2020-07-27 20:43:58 +00:00
|
|
|
textInputAction,
|
2022-09-21 20:27:35 +00:00
|
|
|
CIDR? initialValue,
|
2020-07-27 20:43:58 +00:00
|
|
|
this.ipController,
|
|
|
|
this.bitsController,
|
|
|
|
}) : super(
|
|
|
|
key: key,
|
|
|
|
initialValue: initialValue,
|
|
|
|
onSaved: onSaved,
|
|
|
|
validator: (cidr) {
|
|
|
|
if (cidr == null) {
|
|
|
|
return "Please fill out this field";
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:23:06 +00:00
|
|
|
if (!ipValidator(cidr.ip, enableIPV6)) {
|
2020-07-27 20:43:58 +00:00
|
|
|
return 'Please enter a valid ip address';
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
if (cidr.bits > 32 || cidr.bits < 0) {
|
2020-07-27 20:43:58 +00:00
|
|
|
return "Please enter a valid number of bits";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
builder: (FormFieldState<CIDR> field) {
|
2022-09-21 20:27:35 +00:00
|
|
|
final _CIDRFormField state = field as _CIDRFormField;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
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
|
2022-09-21 20:27:35 +00:00
|
|
|
? Text(field.errorText ?? "Unknown error",
|
2020-07-27 20:43:58 +00:00
|
|
|
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(field.context), fontSize: 13),
|
|
|
|
textAlign: TextAlign.end)
|
|
|
|
: Container(height: 0)
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
final TextEditingController? ipController;
|
|
|
|
final TextEditingController? bitsController;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_CIDRFormField createState() => _CIDRFormField();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CIDRFormField extends FormFieldState<CIDR> {
|
2022-09-21 20:27:35 +00:00
|
|
|
TextEditingController? _ipController = TextEditingController();
|
|
|
|
TextEditingController? _bitsController = TextEditingController();
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
TextEditingController get _effectiveIPController => widget.ipController ?? _ipController!;
|
|
|
|
TextEditingController get _effectiveBitsController => widget.bitsController ?? _bitsController!;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
2022-09-21 20:27:35 +00:00
|
|
|
CIDRFormField get widget => super.widget as CIDRFormField;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
if (widget.ipController == null) {
|
2022-09-21 20:27:35 +00:00
|
|
|
_ipController = TextEditingController(text: widget.initialValue?.ip);
|
2020-07-27 20:43:58 +00:00
|
|
|
} else {
|
2022-09-21 20:27:35 +00:00
|
|
|
widget.ipController!.addListener(_handleControllerChanged);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.bitsController == null) {
|
2022-11-17 21:43:16 +00:00
|
|
|
_bitsController = TextEditingController(text: widget.initialValue?.bits.toString() ?? "");
|
2020-07-27 20:43:58 +00:00
|
|
|
} else {
|
2022-09-21 20:27:35 +00:00
|
|
|
widget.bitsController!.addListener(_handleControllerChanged);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(CIDRFormField oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2022-09-21 20:27:35 +00:00
|
|
|
var update = CIDR(ip: widget.ipController?.text ?? "", bits: int.tryParse(widget.bitsController?.text ?? "") ?? 0);
|
2020-07-27 20:43:58 +00:00
|
|
|
bool shouldUpdate = false;
|
|
|
|
|
|
|
|
if (widget.ipController != oldWidget.ipController) {
|
|
|
|
oldWidget.ipController?.removeListener(_handleControllerChanged);
|
|
|
|
widget.ipController?.addListener(_handleControllerChanged);
|
|
|
|
|
|
|
|
if (oldWidget.ipController != null && widget.ipController == null) {
|
2022-09-21 20:27:35 +00:00
|
|
|
_ipController = TextEditingController.fromValue(oldWidget.ipController!.value);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.ipController != null) {
|
|
|
|
shouldUpdate = true;
|
2022-09-21 20:27:35 +00:00
|
|
|
update.ip = widget.ipController!.text;
|
2020-07-27 20:43:58 +00:00
|
|
|
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) {
|
2022-09-21 20:27:35 +00:00
|
|
|
_bitsController = TextEditingController.fromValue(oldWidget.bitsController!.value);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.bitsController != null) {
|
|
|
|
shouldUpdate = true;
|
2022-09-21 20:27:35 +00:00
|
|
|
update.bits = int.parse(widget.bitsController!.text);
|
2020-07-27 20:43:58 +00:00
|
|
|
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(() {
|
2022-09-21 20:27:35 +00:00
|
|
|
_effectiveIPController.text = widget.initialValue?.ip ?? "";
|
|
|
|
_effectiveBitsController.text = widget.initialValue?.bits.toString() ?? "";
|
2020-07-27 20:43:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-09-21 20:27:35 +00:00
|
|
|
if (value == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_effectiveIPController.text != value!.ip || effectiveBits != value!.bits) {
|
2020-07-27 20:43:58 +00:00
|
|
|
didChange(CIDR(ip: _effectiveIPController.text, bits: effectiveBits));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|