This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entity_class/text_entity.class.dart
2018-10-07 15:03:51 +03:00

85 lines
2.4 KiB
Dart

part of '../main.dart';
class _TextEntityWidgetState extends _EntityWidgetState {
String _tmpValue;
FocusNode _focusNode = FocusNode();
bool validValue = false;
int get valueMinLength => widget.entity.attributes["min"] ?? -1;
int get valueMaxLength => widget.entity.attributes["max"] ?? -1;
String get valuePattern => widget.entity.attributes["pattern"] ?? null;
bool get isTextField => widget.entity.attributes["mode"] == "text";
bool get isPasswordField => widget.entity.attributes["mode"] == "password";
@override
void initState() {
super.initState();
_focusNode.addListener(_focusListener);
_tmpValue = widget.entity.state;
}
@override
void setNewState(newValue) {
if (validate(newValue)) {
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
{"value": "$newValue"}));
} else {
setState(() {
_tmpValue = widget.entity.state;
});
}
}
bool validate(newValue) {
if (newValue is String) {
validValue = (newValue.length >= this.valueMinLength) &&
(this.valueMaxLength == -1 ||
(newValue.length <= this.valueMaxLength));
} else {
validValue = true;
}
return validValue;
}
void _focusListener() {
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
setNewState(_tmpValue);
}
}
@override
Widget _buildActionWidget(BuildContext context) {
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
_tmpValue = widget.entity.state;
}
if (this.isTextField || this.isPasswordField) {
return Expanded(
//width: Entity.INPUT_WIDTH,
child: TextField(
focusNode: _focusNode,
obscureText: this.isPasswordField,
controller: new TextEditingController.fromValue(
new TextEditingValue(
text: _tmpValue,
selection:
new TextSelection.collapsed(offset: _tmpValue.length)
)
),
onChanged: (value) {
_tmpValue = value;
}),
);
} else {
TheLogger.log("Warning", "Unsupported input mode for ${widget.entity.entityId}");
return super._buildActionWidget(context);
}
}
@override
void dispose() {
_focusNode.removeListener(_focusListener);
_focusNode.dispose();
super.dispose();
}
}