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

83 lines
2.4 KiB
Dart
Raw Normal View History

part of '../main.dart';
2018-10-02 00:41:40 +03:00
class _TextEntityWidgetState extends _EntityWidgetState {
String _tmpValue;
FocusNode _focusNode = FocusNode();
bool validValue = false;
2018-10-02 00:41:40 +03:00
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";
2018-10-02 00:41:40 +03:00
@override
void initState() {
super.initState();
_focusNode.addListener(_focusListener);
2018-10-02 00:41:40 +03:00
_tmpValue = widget.entity.state;
}
@override
2018-10-02 23:10:40 +03:00
void setNewState(newValue) {
if (validate(newValue)) {
2018-10-02 00:41:40 +03:00
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
{"value": "$newValue"}));
2018-10-02 00:41:40 +03:00
} 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() {
2018-10-02 00:41:40 +03:00
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
2018-10-02 23:10:40 +03:00
setNewState(_tmpValue);
}
}
@override
Widget _buildActionWidget(bool inCard, BuildContext context) {
2018-10-02 00:41:40 +03:00
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
_tmpValue = widget.entity.state;
}
if (this.isTextField || this.isPasswordField) {
return Expanded(
//width: Entity.INPUT_WIDTH,
child: TextField(
2018-10-02 00:41:40 +03:00
focusNode: _focusNode,
obscureText: this.isPasswordField,
controller: new TextEditingController.fromValue(
new TextEditingValue(
2018-10-02 00:41:40 +03:00
text: _tmpValue,
selection:
2018-10-02 00:41:40 +03:00
new TextSelection.collapsed(offset: _tmpValue.length))),
onChanged: (value) {
2018-10-02 00:41:40 +03:00
_tmpValue = value;
}),
);
} else {
2018-10-02 00:41:40 +03:00
TheLogger.log("Warning", "Unsupported input mode for ${widget.entity.entityId}");
return super._buildActionWidget(inCard, context);
}
}
2018-10-02 00:41:40 +03:00
@override
void dispose() {
_focusNode.removeListener(_focusListener);
_focusNode.dispose();
super.dispose();
}
}