简体   繁体   English

使用 getx 切换小部件属性

[英]toggle widget attribute using getx

I am using Getx.我正在使用 Getx。 I need when the user click a card only the selected one's border change like this:我需要当用户点击一张卡片时,只有选定的边框会发生这样的变化:

在此处输入图像描述

but if the user select both two of them will be colored.但如果用户 select 两者都将着色。 how to prevent that?如何防止?

在此处输入图像描述

The Code is:该代码是:

class SelectRoleController extends GetxController{
  bool isClicked=false;
  Color color=whiteColor;
  
  void toggle({required String id}){
    isClicked=!isClicked;
     isClicked?color=primaryColor : color=whiteColor;
     update([id]);
  }
}

Am not so sure exactly what you asking.我不太确定你问的是什么。 Does each card widget use that controller on its own?每个卡片小部件是否单独使用该 controller? It seems to me you are managing two states independently when they should be one.在我看来,当它们应该是一个状态时,您正在独立管理两个状态。 So instead of having a SelectRoleController for each card.因此,不要为每张卡设置一个 SelectRoleController。 Create one controller class that both cards listen to and instead of using a boolean value for state use something like an index to show the current selected one.创建一个 controller class 两个卡都听,而不是使用 boolean 值来显示当前的一个 Z9ED39E2EA93142EF73 的索引。

Am not so sure exactly what you asking.我不太确定你问的是什么。 Does each card widget use that controller on its own?每个卡片小部件是否单独使用该 controller? It seems to me you are managing two states independently when they should be one.在我看来,当它们应该是一个状态时,您正在独立管理两个状态。 So instead of having a SelectRoleController for each card.因此,不要为每张卡设置一个 SelectRoleController。

Solution解决方案

Create one controller class that both cards listen to and instead of using a boolean value for state use something like an index to show the current selected one.创建一个 controller class 两个卡都听,而不是使用 boolean 值来显示当前的一个 Z9ED39E2EA93142EF73 的索引。 Instead of using GetxBuilder and updating it with an Id wrap the card widget with an Obx and listen to the controller's current.而不是使用 GetxBuilder 并使用 Id 更新它,而是使用 Obx 包装卡片小部件并监听控制器的电流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM