3
0
Fork 0
trifid_mobile/lib/components/IPAndPortFormField.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
6.3 KiB
Dart
Raw Permalink Normal View History

2020-07-27 20:43:58 +00:00
import 'package:flutter/cupertino.dart';
import 'package:mobile_nebula/models/IPAndPort.dart';
import 'package:mobile_nebula/validators/dnsValidator.dart';
import 'package:mobile_nebula/validators/ipValidator.dart';
import 'IPAndPortField.dart';
class IPAndPortFormField extends FormField<IPAndPort> {
2022-07-27 16:38:02 +00:00
//TODO: onSaved, validator, auto-validate, enabled?
2020-07-27 20:43:58 +00:00
IPAndPortFormField({
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
ipHelp = "ip address",
autoFocus = false,
focusNode,
nextFocusNode,
ValueChanged<IPAndPort>? onChanged,
FormFieldSetter<IPAndPort>? onSaved,
2020-07-27 20:43:58 +00:00
textInputAction,
IPAndPort? initialValue,
2020-07-27 20:43:58 +00:00
noBorder,
ipTextAlign = TextAlign.center,
this.ipController,
this.portController,
}) : super(
key: key,
initialValue: initialValue,
onSaved: onSaved,
validator: (ipAndPort) {
if (ipAndPort == null) {
return "Please fill out this field";
}
2021-04-23 21:23:06 +00:00
if (!ipValidator(ipAndPort.ip, enableIPV6) && (!ipOnly && !dnsValidator(ipAndPort.ip))) {
2020-07-27 20:43:58 +00:00
return ipOnly ? 'Please enter a valid ip address' : 'Please enter a valid ip address or dns name';
}
if (ipAndPort.port == null || ipAndPort.port! > 65535 || ipAndPort.port! < 0) {
2020-07-27 20:43:58 +00:00
return "Please enter a valid port";
}
return null;
},
builder: (FormFieldState<IPAndPort> field) {
final _IPAndPortFormField state = field as _IPAndPortFormField;
2020-07-27 20:43:58 +00:00
void onChangedHandler(IPAndPort value) {
if (onChanged != null) {
onChanged(value);
}
field.didChange(value);
}
return Column(children: <Widget>[
IPAndPortField(
ipOnly: ipOnly,
ipHelp: ipHelp,
autoFocus: autoFocus,
focusNode: focusNode,
nextFocusNode: nextFocusNode,
onChanged: onChangedHandler,
textInputAction: textInputAction,
ipController: state._effectiveIPController,
portController: state._effectivePortController,
noBorder: noBorder,
ipTextAlign: ipTextAlign,
),
field.hasError
? Text(field.errorText!,
2020-07-27 20:43:58 +00:00
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(field.context), fontSize: 13))
: Container(height: 0)
]);
});
final TextEditingController? ipController;
final TextEditingController? portController;
2020-07-27 20:43:58 +00:00
@override
_IPAndPortFormField createState() => _IPAndPortFormField();
}
class _IPAndPortFormField extends FormFieldState<IPAndPort> {
TextEditingController? _ipController;
TextEditingController? _portController;
2020-07-27 20:43:58 +00:00
TextEditingController get _effectiveIPController => widget.ipController ?? _ipController!;
TextEditingController get _effectivePortController => widget.portController ?? _portController!;
2020-07-27 20:43:58 +00:00
@override
IPAndPortFormField get widget => super.widget as IPAndPortFormField;
2020-07-27 20:43:58 +00:00
@override
void initState() {
super.initState();
if (widget.ipController == null) {
_ipController = TextEditingController(text: widget.initialValue?.ip ?? "");
2020-07-27 20:43:58 +00:00
} else {
widget.ipController!.addListener(_handleControllerChanged);
2020-07-27 20:43:58 +00:00
}
if (widget.portController == null) {
_portController = TextEditingController(text: widget.initialValue?.port?.toString() ?? "");
} else {
widget.portController!.addListener(_handleControllerChanged);
2020-07-27 20:43:58 +00:00
}
}
@override
void didUpdateWidget(IPAndPortFormField oldWidget) {
super.didUpdateWidget(oldWidget);
var update =
IPAndPort(ip: widget.ipController?.text, port: int.tryParse(widget.portController?.text ?? "") ?? null);
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);
2020-07-27 20:43:58 +00:00
}
if (widget.ipController != null) {
shouldUpdate = true;
update.ip = widget.ipController!.text;
2020-07-27 20:43:58 +00:00
if (oldWidget.ipController == null) _ipController = null;
}
}
if (widget.portController != oldWidget.portController) {
oldWidget.portController?.removeListener(_handleControllerChanged);
widget.portController?.addListener(_handleControllerChanged);
if (oldWidget.portController != null && widget.portController == null) {
_portController = TextEditingController.fromValue(oldWidget.portController!.value);
2020-07-27 20:43:58 +00:00
}
if (widget.portController != null) {
shouldUpdate = true;
update.port = int.parse(widget.portController!.text);
2020-07-27 20:43:58 +00:00
if (oldWidget.portController == null) _portController = null;
}
}
if (shouldUpdate) {
setValue(update);
}
}
@override
void dispose() {
widget.ipController?.removeListener(_handleControllerChanged);
widget.portController?.removeListener(_handleControllerChanged);
super.dispose();
}
@override
void reset() {
super.reset();
setState(() {
_effectiveIPController.text = widget.initialValue?.ip ?? "";
_effectivePortController.text = widget.initialValue?.port?.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 effectivePort = int.parse(_effectivePortController.text);
if (value == null) {
return;
}
if (_effectiveIPController.text != value!.ip || effectivePort != value!.port) {
2020-07-27 20:43:58 +00:00
didChange(IPAndPort(ip: _effectiveIPController.text, port: effectivePort));
}
}
}