在flutter中,未使用Getx更新收藏夹图标

rqenqsqc  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(141)

//屏幕的代码ListView.builder(物理:常量永不滚动滚动物理(),收缩包裹:true,填充:EdgeInsets.zero,滚动方向:垂直轴,项目计数:控制器.偏好目标列表.长度,项目生成器:(BuildContext上下文,整数索引){ return容器(宽度:媒体查询.of(上下文).大小.宽度 * 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;
  }
83qze16e

83qze16e1#

你的问题不是很清楚,你必须重新表述它。但是,我认为在你的视图中没有任何Observer Widget或GetXBuilder。尝试用其中之一 Package listView.Builder,它应该工作。

相关问题