Refactoring: Input text and focus

This commit is contained in:
estevez 2018-09-30 10:00:19 +03:00
parent 2f135169a9
commit 9e83a3e447
3 changed files with 33 additions and 102 deletions

View File

@ -71,51 +71,27 @@ class Entity {
eventBus.fire(new ShowEntityPageEvent(this)); eventBus.fire(new ShowEntityPageEvent(this));
} }
Widget buildWidget(GlobalKey<FormState> formKey, bool tapAction) { Widget buildWidget(bool inCard) {
return SizedBox( return SizedBox(
height: Entity.WIDGET_HEIGHT, height: Entity.WIDGET_HEIGHT,
child: Row( child: Row(
children: <Widget>[ children: <Widget>[
GestureDetector( GestureDetector(
child: _buildIconWidget(), child: _buildIconWidget(),
onTap: tapAction ? openEntityPage : null, onTap: inCard ? openEntityPage : null,
), ),
Expanded( Expanded(
child: GestureDetector( child: GestureDetector(
child: _buildNameWidget(), child: _buildNameWidget(),
onTap: tapAction ? openEntityPage : null, onTap: inCard ? openEntityPage : null,
), ),
), ),
_buildActionWidget() _buildActionWidget(inCard)
], ],
), ),
); );
} }
/*Widget buildExtendedWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) {
return Row(
children: <Widget>[
_buildIconWidget(),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: _buildNameWidget(),
),
_buildExtendedActionWidget(context, staticState)
],
),
_buildLastUpdatedWidget()
],
),
)
],
);
}*/
Widget _buildIconWidget() { Widget _buildIconWidget() {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0), padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
@ -148,7 +124,7 @@ class Entity {
); );
} }
Widget _buildActionWidget() { Widget _buildActionWidget(bool inCard) {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0), padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: GestureDetector( child: GestureDetector(
@ -175,7 +151,7 @@ class SwitchEntity extends Entity {
SwitchEntity(Map rawData) : super(rawData); SwitchEntity(Map rawData) : super(rawData);
@override @override
Widget _buildActionWidget() { Widget _buildActionWidget(bool inCard) {
return Switch( return Switch(
value: this.isOn, value: this.isOn,
onChanged: ((switchState) { onChanged: ((switchState) {
@ -191,7 +167,7 @@ class ButtonEntity extends Entity {
ButtonEntity(Map rawData) : super(rawData); ButtonEntity(Map rawData) : super(rawData);
@override @override
Widget _buildActionWidget() { Widget _buildActionWidget(bool inCard) {
return FlatButton( return FlatButton(
onPressed: (() { onPressed: (() {
eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null)); eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null));
@ -207,35 +183,25 @@ class ButtonEntity extends Entity {
} }
class InputEntity extends Entity { class InputEntity extends Entity {
String tmpState;
FocusNode _focusNode;
InputEntity(Map rawData) : super(rawData); InputEntity(Map rawData) : super(rawData) {
_focusNode = FocusNode();
//TODO memory leak generator
_focusNode.addListener(_focusListener);
tmpState = state;
}
/*@override void _focusListener() {
Widget buildExtendedWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) { //TheLogger.log("Debug", "Focused ${_focusNode.hasFocus? 'on' : 'of'} $entityId. Old state: $state. New state: $tmpState");
return Column( if (!_focusNode.hasFocus && (tmpState != state)) {
children: <Widget>[ eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId, {"value": "$tmpState"}));
SizedBox( }
height: Entity.EXTENDED_WIDGET_HEIGHT, }
child: Row(
children: <Widget>[
_buildIconWidget(),
Expanded(
child: _buildNameWidget(),
),
_buildLastUpdatedWidget()
],
),
),
SizedBox(
height: Entity.EXTENDED_WIDGET_HEIGHT,
child: _buildInputWidget(context, formKey, staticState),
)
],
);
}*/
@override @override
Widget _buildActionWidget() { Widget _buildActionWidget(bool inCard) {
if (this.isSliderField) { if (this.isSliderField) {
return Container( return Container(
width: 200.0, width: 200.0,
@ -271,55 +237,17 @@ class InputEntity extends Entity {
return Container( return Container(
width: 160.0, width: 160.0,
child: TextField( child: TextField(
focusNode: inCard ? _focusNode : null,
obscureText: this.isPasswordField, obscureText: this.isPasswordField,
controller: TextEditingController( controller: new TextEditingController.fromValue(new TextEditingValue(text: _state,selection: new TextSelection.collapsed(offset: _state.length))),
text: _state,
),
onChanged: (value) { onChanged: (value) {
//TODO tmpState = value;
//staticState = value; }
},
), ),
); );
} else { } else {
return super._buildActionWidget(); return super._buildActionWidget(inCard);
} }
} }
/*Widget _buildInputWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) {
return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
obscureText: this.isPasswordField,
controller: TextEditingController(
text: staticState,
),
onChanged: (value) {
staticState = value;
},
),
),
SizedBox(
width: 63.0,
child: FlatButton(
onPressed: () {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "$staticState"}));
Navigator.pop(context);
},
child: Text(
"SET",
textAlign: TextAlign.right,
style: new TextStyle(fontSize: Entity.STATE_FONT_SIZE, color: Colors.blue),
),
),
)
],
)
);
}*/
} }

View File

@ -13,7 +13,6 @@ class _EntityViewPageState extends State<EntityViewPage> {
String _title; String _title;
Entity _entity; Entity _entity;
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
final _formKey = GlobalKey<FormState>();
@override @override
void initState() { void initState() {
@ -47,7 +46,7 @@ class _EntityViewPageState extends State<EntityViewPage> {
padding: EdgeInsets.all(10.0), padding: EdgeInsets.all(10.0),
child: ListView( child: ListView(
children: <Widget>[ children: <Widget>[
_entity.buildWidget(_formKey, false) _entity.buildWidget(false)
], ],
), ),
), ),
@ -56,6 +55,10 @@ class _EntityViewPageState extends State<EntityViewPage> {
@override @override
void dispose(){ void dispose(){
if (_entity is InputEntity && (_entity as InputEntity).tmpState != _entity.state) {
eventBus.fire(new ServiceCallEvent(_entity.domain, "set_value", _entity.entityId, {"value": "${(_entity as InputEntity).tmpState}"}));
TheLogger.log("Debug", "Saving changed input value for ${_entity.entityId}");
}
if (_stateSubscription != null) _stateSubscription.cancel(); if (_stateSubscription != null) _stateSubscription.cancel();
super.dispose(); super.dispose();
} }

View File

@ -409,7 +409,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
entities.add( entities.add(
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: entity.buildWidget(null, true), child: entity.buildWidget(true),
)); ));
} }
}); });