搜索插件、代码仓与 APP 分类

persistent_shopping_cart 封面
插件详情v0.0.8

persistent_shopping_cart

使用此全面的包,轻松管理并持久化您的 Flutter 应用中的购物车数据。简化本地存储,提升用户的购物体验。

AndroidiOSmacOSWindowsLinux
近 30 天下载
59
喜欢数
270
Pub 分数
160

安装插件

flutter pub add persistent_shopping_cart

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,可实现购物车数据在应用重启后的无缝管理和持久化存储。它通过自动将购物车内容保存到设备存储中,简化了本地数据处理,确保用户在关闭应用后仍能保留其选择。适用于电子商务应用,通过保持购物车完整性来提升用户体验。该包支持 Android、iOS、macOS、Windows 和 Linux 多个平台,适用于跨平台开发。

适合场景

  • 带有用户购物车的电子商务应用
  • 离线购物体验
  • 持久的产品选择
  • 跨设备购物车同步
  • 用户会话连续性

主要特点

  • 自动本地持久化
  • 跨平台兼容性
  • 购物车操作的简单 API
  • 实时购物车更新
  • 对复杂项目数据的支持

适合对象

  • 移动电子商务应用
  • 购物应用
  • 需要离线数据的应用程序
  • 寻求简易购物车管理的开发者
  • 针对多个平台的项目

使用建议

  • 需要对存储失败情况进行适当的错误处理
  • 可能需要为大型购物车数据集进行定制
  • 依赖于正确的初始化
  • 不适用于基于云的购物车同步
  • 仅限本地存储

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Persistent Shopping Cart

Persistent Shopping Cart is a Flutter package that provides a simple and persistent shopping cart functionality for your mobile application. It uses Hive for local storage, making the cart data persist across app sessions.

Demo Preview

Demo Preview

Features

  • Initialization: Easily initialize the shopping cart using init() method.
  • Add to Cart: Add products to the cart with the addToCart method.
  • Remove from Cart: Remove products from the cart using the removeFromCart method.
  • Increment/Decrement Quantity: Adjust the quantity of items in the cart with the incrementCartItemQuantity and decrementCartItemQuantity methods.
  • Calculate Total Price: Get the total price of items in the cart using the calculateTotalPrice method.
  • Get Cart Item Count: Retrieve the total number of items in the cart with the getCartItemCount method.
  • Clear Cart: Remove all items from the cart using the clearCart method.
  • Show Cart Items: Display the cart items using the showCartItems method, providing flexible builders for customizing how the cart is displayed (e.g., ListView, GridView).
  • Show Cart Item Count Widget: Show a widget displaying the current cart item count using the showCartItemCountWidget method.
  • Show Total Amount Widget: Display a widget showing the total amount of items in the cart with the showTotalAmountWidget method.
  • Show and Update Cart Item Widget: Show a widget that dynamically updates based on whether a product is in the cart or not, using the showAndUpdateCartItemWidget method.
  • Retrieve Cart Data and Total Price: Use getCartData method in the PersistentShoppingCart class to get a list of cart items and the total price.

Getting Started

  1. Import the package in your Dart file:
import 'package:persistent_shopping_cart/persistent_shopping_cart.dart';
  1. Initialize the cart by calling the init method:
await PersistentShoppingCart().init();
  1. Start using the shopping cart functionality in your application!

Example Usage

// Add product to the cart
await PersistentShoppingCart().addToCart(PersistentShoppingCartItem());

// Remove product from the cart
await PersistentShoppingCart().removeFromCart(productId);

// Increment product quantity in the cart
await PersistentShoppingCart().incrementCartItemQuantity(productId);

// Decrement product quantity in the cart
await PersistentShoppingCart().decrementCartItemQuantity(productId);

// Get total price of items in the cart
double totalPrice = PersistentShoppingCart().calculateTotalPrice();

// Get total number of items in the cart
int itemCount = PersistentShoppingCart().getCartItemCount();

// Clear the cart
PersistentShoppingCart().clearCart();

// Retrieve cart data and total price
Map<String, dynamic> cartData = PersistentShoppingCart().getCartData();
List<PersistentShoppingCartItem> cartItems = cartData['cartItems'];
double totalPriceFromData = cartData['totalPrice'];

Widgets

Show Cart Items

The showCartItems method now provides flexibility to define how cart items are displayed (e.g., ListView, GridView) using a builder function.

PersistentShoppingCart().showCartItems(
  cartItemsBuilder: (BuildContext context, List<PersistentShoppingCartItem> cartItems) {
    // no item added to cart
    if(cartItems.isEmpty){
    return EmptyCartMsgWidget();
    }
    // Define your custom widget for displaying cart items
    return ListView.builder(
      itemCount: cartItems.length,
      itemBuilder: (context, index) {
        final item = cartItems[index];
        return ListTile(
          title: Text(item.name), // Replace with your cart item widget
          subtitle: Text('Quantity: ${item.quantity}'),
        );
      },
    );
  },
);
Show Cart Item Count Widget
PersistentShoppingCart().showCartItemCountWidget(
  cartItemCountWidgetBuilder: (int itemCount) {
    // Your custom widget displaying the cart item count
  },
);
Show Total Amount Widget
PersistentShoppingCart().showTotalAmountWidget(
  cartTotalAmountWidgetBuilder: (double totalAmount) {
    // Your custom widget displaying the total amount
  },
);
Show and Update Cart Item Widget
PersistentShoppingCart().showAndUpdateCartItemWidget(
  inCartWidget: YourInCartWidget(),
  notInCartWidget: YourNotInCartWidget(),
  product: yourProduct,
);

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目

暂无相关数据。