2020-07-27 20:43:58 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:mobile_nebula/validators/dnsValidator.dart';
|
|
|
|
import 'package:mobile_nebula/validators/ipValidator.dart';
|
|
|
|
|
|
|
|
import 'IPField.dart';
|
|
|
|
|
|
|
|
//TODO: reset doesn't update the ui but clears the field
|
|
|
|
|
|
|
|
class IPFormField extends FormField<String> {
|
2022-07-27 16:38:02 +00:00
|
|
|
//TODO: validator, auto-validate, enabled?
|
2020-07-27 20:43:58 +00:00
|
|
|
IPFormField({
|
2022-09-21 20:27:35 +00:00
|
|
|
Key? key,
|
2020-07-27 20:43:58 +00:00
|
|
|
ipOnly = false,
|
2021-04-23 21:23:06 +00:00
|
|
|
enableIPV6 = false,
|
2020-07-27 20:43:58 +00:00
|
|
|
help = "ip address",
|
|
|
|
autoFocus = false,
|
|
|
|
focusNode,
|
|
|
|
nextFocusNode,
|
2022-09-21 20:27:35 +00:00
|
|
|
ValueChanged<String>? onChanged,
|
|
|
|
FormFieldSetter<String>? onSaved,
|
2020-07-27 20:43:58 +00:00
|
|
|
textPadding = const EdgeInsets.all(6.0),
|
|
|
|
textInputAction,
|
|
|
|
initialValue,
|
|
|
|
this.controller,
|
|
|
|
crossAxisAlignment = CrossAxisAlignment.center,
|
|
|
|
textAlign = TextAlign.center,
|
|
|
|
}) : super(
|
|
|
|
key: key,
|
|
|
|
initialValue: initialValue,
|
|
|
|
onSaved: onSaved,
|
|
|
|
validator: (ip) {
|
|
|
|
if (ip == null || ip == "") {
|
|
|
|
return "Please fill out this field";
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:23:06 +00:00
|
|
|
if (!ipValidator(ip, enableIPV6) || (!ipOnly && !dnsValidator(ip))) {
|
2020-07-27 20:43:58 +00:00
|
|
|
print(ip);
|
|
|
|
return ipOnly ? 'Please enter a valid ip address' : 'Please enter a valid ip address or dns name';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
builder: (FormFieldState<String> field) {
|
2022-09-21 20:27:35 +00:00
|
|
|
final _IPFormField state = field as _IPFormField;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
void onChangedHandler(String value) {
|
|
|
|
if (onChanged != null) {
|
|
|
|
onChanged(value);
|
|
|
|
}
|
|
|
|
field.didChange(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Column(crossAxisAlignment: crossAxisAlignment, children: <Widget>[
|
|
|
|
IPField(
|
|
|
|
ipOnly: ipOnly,
|
|
|
|
help: help,
|
|
|
|
autoFocus: autoFocus,
|
|
|
|
focusNode: focusNode,
|
|
|
|
nextFocusNode: nextFocusNode,
|
|
|
|
onChanged: onChangedHandler,
|
|
|
|
textPadding: textPadding,
|
|
|
|
textInputAction: textInputAction,
|
|
|
|
controller: state._effectiveController,
|
|
|
|
textAlign: textAlign),
|
|
|
|
field.hasError
|
|
|
|
? Text(
|
2022-09-21 20:27:35 +00:00
|
|
|
field.errorText!,
|
2020-07-27 20:43:58 +00:00
|
|
|
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(field.context), fontSize: 13),
|
|
|
|
textAlign: textAlign,
|
|
|
|
)
|
|
|
|
: Container(height: 0)
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
final TextEditingController? controller;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_IPFormField createState() => _IPFormField();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _IPFormField extends FormFieldState<String> {
|
2022-09-21 20:27:35 +00:00
|
|
|
TextEditingController? _controller;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
TextEditingController get _effectiveController => widget.controller ?? _controller!;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
2022-09-21 20:27:35 +00:00
|
|
|
IPFormField get widget => super.widget as IPFormField;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
if (widget.controller == null) {
|
|
|
|
_controller = TextEditingController(text: widget.initialValue);
|
|
|
|
} else {
|
2022-09-21 20:27:35 +00:00
|
|
|
widget.controller!.addListener(_handleControllerChanged);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(IPFormField oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
if (widget.controller != oldWidget.controller) {
|
|
|
|
oldWidget.controller?.removeListener(_handleControllerChanged);
|
|
|
|
widget.controller?.addListener(_handleControllerChanged);
|
|
|
|
|
|
|
|
if (oldWidget.controller != null && widget.controller == null)
|
2022-09-21 20:27:35 +00:00
|
|
|
_controller = TextEditingController.fromValue(oldWidget.controller!.value);
|
2020-07-27 20:43:58 +00:00
|
|
|
if (widget.controller != null) {
|
2022-09-21 20:27:35 +00:00
|
|
|
setValue(widget.controller!.text);
|
2020-07-27 20:43:58 +00:00
|
|
|
if (oldWidget.controller == null) _controller = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
widget.controller?.removeListener(_handleControllerChanged);
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void reset() {
|
|
|
|
super.reset();
|
|
|
|
setState(() {
|
2022-09-21 20:27:35 +00:00
|
|
|
_effectiveController.text = widget.initialValue ?? "";
|
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.
|
|
|
|
if (_effectiveController.text != value) didChange(_effectiveController.text);
|
|
|
|
}
|
|
|
|
}
|