Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在Listview中使用具有共享首选项的收藏夹按钮_Flutter_Dart - Fatal编程技术网

Flutter 在Listview中使用具有共享首选项的收藏夹按钮

Flutter 在Listview中使用具有共享首选项的收藏夹按钮,flutter,dart,Flutter,Dart,当我单击列表视图中的“收藏夹”按钮并单击列表中的所有项目时,我在列表视图中有一个收藏夹按钮,如何使用共享首选项修复该按钮+以下是我的代码: 在我的stfl状态下: static const likedKey='liked_key'; 布尔喜欢; @凌驾 void initState(){ super.initState(); _恢复PersistedReference(); } void\u restorePersistedReference()异步{ var preferences=awai

当我单击列表视图中的“收藏夹”按钮并单击列表中的所有项目时,我在列表视图中有一个收藏夹按钮,如何使用共享首选项修复该按钮+以下是我的代码: 在我的stfl状态下:

static const likedKey='liked_key';
布尔喜欢;
@凌驾
void initState(){
super.initState();
_恢复PersistedReference();
}
void\u restorePersistedReference()异步{
var preferences=await SharedPreferences.getInstance();
var liked=preferences.getBool(likedKey)?false;
setState(()=>this.liked=liked);
}
void\u persistPreference()异步{
setState(()=>liked=!liked);
var preferences=await SharedPreferences.getInstance();
setBool(likedKey,liked);

}
您可以通过生成
列表来解决问题。由于按下图标按钮将更改“喜欢”的值,因此您的所有图标都将成为icons.favorite,因为它们都引用了您喜欢的同一个bool。下面是一个关于如何解决此问题的示例:

  @override
  Widget build(BuildContext context) {
   List<bool> likedList=[];
   List<bool>.generate(yourItem.length, (i) => likedList.add(false);
   return ListView.builder(
    itemcount:yourItem.length,
    itemBuilder:(BuildContext context,int index){
     return IconButton(
             icon: Icon(
                      likedList[index] ? Icons.favorite : Icons.favorite_border,
                      color: likedList[index] ? Colors.red : Colors.grey,
             ),
             onPressed:()=> likedList[index]=!likedList[index], //toggle bool value
       );
     }
    )
@覆盖
小部件构建(构建上下文){
类似列表的列表=[];
List.generate(yourItem.length,(i)=>likedList.add(false);
返回ListView.builder(
itemcount:yourItem.length,
itemBuilder:(构建上下文,int索引){
返回图标按钮(
图标:图标(
likedList[索引]?Icons.favorite:Icons.favorite\u边框,
颜色:喜欢列表[索引]?颜色。红色:颜色。灰色,
),
按下:()=>likedList[index]=!likedList[index],//切换布尔值
);
}
)

您可以通过生成一个
列表来解决问题。由于按下图标按钮将更改“喜欢”的值,因此您的所有图标都将成为图标。因为它们都引用了您喜欢的同一个布尔值。下面是一个如何解决此问题的示例:

  @override
  Widget build(BuildContext context) {
   List<bool> likedList=[];
   List<bool>.generate(yourItem.length, (i) => likedList.add(false);
   return ListView.builder(
    itemcount:yourItem.length,
    itemBuilder:(BuildContext context,int index){
     return IconButton(
             icon: Icon(
                      likedList[index] ? Icons.favorite : Icons.favorite_border,
                      color: likedList[index] ? Colors.red : Colors.grey,
             ),
             onPressed:()=> likedList[index]=!likedList[index], //toggle bool value
       );
     }
    )
@覆盖
小部件构建(构建上下文){
类似列表的列表=[];
List.generate(yourItem.length,(i)=>likedList.add(false);
返回ListView.builder(
itemcount:yourItem.length,
itemBuilder:(构建上下文,int索引){
返回图标按钮(
图标:图标(
likedList[索引]?Icons.favorite:Icons.favorite\u边框,
颜色:喜欢列表[索引]?颜色。红色:颜色。灰色,
),
按下:()=>likedList[index]=!likedList[index],//切换布尔值
);
}
)

请更清楚、更详细地解释您的问题。没有人理解您的问题我有类似于posts的listview好吗?每个post都有button=like。当用户单击like按钮时,它会将所有post更改为like!清楚吗?我现在就知道了。这是因为它们都共享一个名为like的变量。因此,当您将like更改为true时,所有的他们会喜欢的。是的,我能帮你吗?我已经发布了一个答案@Ramzi。如果用户按下按钮,你需要生成一个列表,然后更改每个值。请更清楚地解释你的问题,并提供更多详细信息。没有人了解你的问题我有类似于帖子的listview好吗?每个帖子都有按钮=喜欢。当用户单击“喜欢”时e按钮将所有帖子更改为喜欢!明白吗?我现在明白了。这是因为所有帖子都共享一个名为喜欢的变量。因此,当您将喜欢更改为true时,所有帖子都会被喜欢。是的,我可以直接发送给您吗?我已经发布了一个答案@Ramzi。如果用户按下按钮,您需要生成一个列表,然后更改每个单独的值。