【问题标题】:Create a favorite selection in Gridview using Sharepeference使用 Sharepeference 在 Gridview 中创建收藏夹
【发布时间】:2021-10-09 15:16:47
【问题描述】:

您好,我是 Flutter 的初学者,现在我想使用 Sharepeference 在 Gridview 中创建一个最喜欢的选择,用于存储来自 gridview 的最喜欢的项目,并将其标记为 favouite.so 当用户关闭应用程序并返回应用程序时,他们可以在里面看到 favouite gridview 你可以查看我下面的代码 我的代码不是很复杂它的简单代码你可以很容易地理解这段代码!

import 'package:favorite_button/favorite_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:untitled1/Singleimage.dart';

class CategoriesDetails extends StatefulWidget {
  late String categoryname;
  late int categorynumber;
  late String foldername;


  CategoriesDetails(this.categoryname, this.categorynumber,this.foldername);

  @override
  _CategoriesDetailsState createState() => _CategoriesDetailsState();
}

class _CategoriesDetailsState extends State<CategoriesDetails> {
   late List<String>addfavourite=[];
   late SharedPreferences prefs ;

  @override
  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    centerTitle: true,
    title: Text(widget.categoryname),
  ),
  body:Container(
    color: Colors.black,
    child: GridView.builder(
      itemCount: widget.categorynumber,
      gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2
      ),
      itemBuilder: (BuildContext context, int index) {


        return ClipRRect(
          borderRadius: BorderRadius.circular(40.0),
          child: Container(
            child: Column(
              children: [
             Expanded(
             child: Stack(
               alignment: Alignment.topRight,
               children: [
               Container(
                     margin: EdgeInsets.symmetric(vertical:10, horizontal:6),
                     height: 200,
                     width: 200,
                     color: Colors.amber,
                     child:  GestureDetector(
                       onTap: (){

                         Navigator.of(context).push(MaterialPageRoute(builder: (context) =>Singleimage(widget.foldername,widget.categorynumber,index,widget.categoryname)));

                       },
                       child: Image.asset(
                         'assets/images/${widget.foldername}/${index+1}.jpg',
                         height: 100,
                         fit: BoxFit.cover,
                       ),
                     )
                 ),
                 Positioned(
                   top: 0,
                   right: 0,
                   child: IconButton(
                     onPressed: () async {

                       },
                     color: Colors.white,
                     icon:Icon(Icons.favorite_border)

                   ),
                 )
               ],

             ),
           ),
                Text(
                '${index+1}',
                  style: TextStyle(
                  fontSize: 20,
                  color: Colors.white
                ),)

              ],

            ),
          ),
        ) ;

      },

    ),
  ),

);
  }


}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您不能将列表 obj 直接保存在 sharedprefrence 中 1)首先转换成 MAP 2)解码成字符串变量 3)然后通过json对其进行编码 4 将其转换为列表对象并保存在共享首选项中

    【讨论】:

    • 但在颤振共享性能中。使用此方法直接存储列表项 prefs.setStringList(key, value)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多