最喜欢的图标不会在颤音中使用getx进行更新

发布于 2025-02-08 07:27:15 字数 3251 浏览 2 评论 0原文

//屏幕代码 listView.builder( 物理:const noterscrollablesscrollphysics(), 缩水:是的, 填充:edgeinsets.zero, ScrolDirection:axis.tertical, itemCount:controller.favdestinationlist.length, itembuilder :( buildContext上下文,int index){ 返回容器( 宽度:MediaQuery .of(上下文) 。尺寸 .Width * 0.9, 孩子:行( 孩子们: [

              IconButton(
              icon:FaIcon(controller.favDestinationList[index].isfav?
              FontAwesomeIcons.solidHeart : FontAwesomeIcons.heart,
                color: secondaryHeaderColor,),
              onPressed: () {
                controller.changeStatus(
                    controller.favDestinationList[index].isfav);

                print(controller.favDestinationList[index].isfav);
              },
            );


            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(controller.favDestinationList[index].name),
            )
            ]
            ,
            )
            ,
            );
          }
      ),


///////code of the screen
ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              padding: EdgeInsets.zero,
              scrollDirection: Axis.vertical,
              itemCount: controller.favDestinationList.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.9,
                  child: Row(
                    children: [

                  IconButton(
                  icon:FaIcon(controller.favDestinationList[index].isfav?
                  FontAwesomeIcons.solidHeart : FontAwesomeIcons.heart,
                    color: secondaryHeaderColor,),
                  onPressed: () {
                    controller.changeStatus(
                        controller.favDestinationList[index].isfav);

                    print(controller.favDestinationList[index].isfav);
                  },
                );


                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(controller.favDestinationList[index].name),
                )
                ]
                ,
                )
                ,
                );
              }
          ),





/////////////////controller class 

class SearchFavoriteController extends GetxController{
var favDestinationList=[].obs;




  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getFavDestination();
  }



  getFavDestination() async{
    try{
      var destination=await Api().getfavdestinations();//call api
      favDestinationList.assignAll(destination);
    }catch(e){
      Get.snackbar('title', e.toString());
    }

  }



changeStatus(bool isfavorite)  {
    isfavorite = !isfavorite;
    update();
  }

}




/////////////api

  List<FavoriteModel> getfavdestinations() {
   return [
     FavoriteModel( id: '1',name: 'Germany', isfav: false),
     FavoriteModel( id: '2',name: 'Turkey', isfav: true),
     FavoriteModel( id: '3',name: 'Jordan', isfav: false),
     FavoriteModel( id: '4',name: 'London', isfav: false)
   ].obs;
  }

//code of the screen
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.zero,
scrollDirection: Axis.vertical,
itemCount: controller.favDestinationList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
width: MediaQuery
.of(context)
.size
.width * 0.9,
child: Row(
children: [

              IconButton(
              icon:FaIcon(controller.favDestinationList[index].isfav?
              FontAwesomeIcons.solidHeart : FontAwesomeIcons.heart,
                color: secondaryHeaderColor,),
              onPressed: () {
                controller.changeStatus(
                    controller.favDestinationList[index].isfav);

                print(controller.favDestinationList[index].isfav);
              },
            );


            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(controller.favDestinationList[index].name),
            )
            ]
            ,
            )
            ,
            );
          }
      ),


///////code of the screen
ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              padding: EdgeInsets.zero,
              scrollDirection: Axis.vertical,
              itemCount: controller.favDestinationList.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.9,
                  child: Row(
                    children: [

                  IconButton(
                  icon:FaIcon(controller.favDestinationList[index].isfav?
                  FontAwesomeIcons.solidHeart : FontAwesomeIcons.heart,
                    color: secondaryHeaderColor,),
                  onPressed: () {
                    controller.changeStatus(
                        controller.favDestinationList[index].isfav);

                    print(controller.favDestinationList[index].isfav);
                  },
                );


                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(controller.favDestinationList[index].name),
                )
                ]
                ,
                )
                ,
                );
              }
          ),





/////////////////controller class 

class SearchFavoriteController extends GetxController{
var favDestinationList=[].obs;




  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getFavDestination();
  }



  getFavDestination() async{
    try{
      var destination=await Api().getfavdestinations();//call api
      favDestinationList.assignAll(destination);
    }catch(e){
      Get.snackbar('title', e.toString());
    }

  }



changeStatus(bool isfavorite)  {
    isfavorite = !isfavorite;
    update();
  }

}




/////////////api

  List<FavoriteModel> getfavdestinations() {
   return [
     FavoriteModel( id: '1',name: 'Germany', isfav: false),
     FavoriteModel( id: '2',name: 'Turkey', isfav: true),
     FavoriteModel( id: '3',name: 'Jordan', isfav: false),
     FavoriteModel( id: '4',name: 'London', isfav: false)
   ].obs;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2025-02-15 07:27:15

您的问题不是很清楚,您必须重新提示它
但是,我认为您认为没有任何观察者小部件或getxbuilder
尝试将listView.builder与其中之一包装,它应该有效

Your question is not very clear, and you have to Rephrase it
However, i think there isn't any Observer Widget or GetXBuilder in your view
try to wrap the listView.Builder with one of them, it should works

魔法少女 2025-02-15 07:27:15

您的问题不是很清楚,但是您可以尝试使用此功能,将您的listView用OBX小部件或列从身体开始的位置包装

Your question is not very clear, But you can try this hope this works wrap your Listview with Obx widget or Column from where body starts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文