Flutter开发日常练习-小猫咪杂货店

养小猫咪的伙伴来我的店铺逛逛吧!抖音商城搜索#早睡早起的猫咪小铺子

 

贴三张效果图

1.欢迎页面

2.商品展示列表

3.购物车页面

因为数据是本地的所以创建本地数据

final List _shopItems = [
    ["ZaoShui.", "25.00", "assets/8b10de68e58cfef6bd5f22e5321537.jpg", Colors.green],
    ["ZaoQi.", "25.00", "assets/cat.jpg", Colors.yellow],
    ["Pupu.", "30.00", "assets/503421681131239_.jpg", Colors.orange],
    ["Piupiu.", "10.00", "assets/503471681132990_.jpg", Colors.tealAccent],
    ["XiaoXin.", "10.00", "assets/497401681033126_.jpg", Colors.amberAccent],
    ["QiuQiu.", "10.00", "assets/503351681128814_.jpg", Colors.deepOrange],
  ];

 涉及到了添加数据,删除数据,和价格统计

  void addItemToCart(int index) {
    cartItems.add(shopItems[index]);
  }
  void removeItemFromCart(int index) {
    cartItems.removeAt(index);
  }
  String clculateTotal() {
    double totalPrice = 0;
    for (var i = 0; i < cartItems.length; i++) {
      totalPrice += double.parse(cartItems[i][1]);
    }
    return totalPrice.toStringAsFixed(2);
  }

在添加和删除数据的时候需要进行通知内容更改

所以要在方法体里面加入

notifyListeners();

同时要在main.datr文件里更改为

return ChangeNotifierProvider()

商品列表采用GridView绘制

调用GroceryItemTile

  final String itemName;
  final String itemPrice;
  final String imagePath;
  final color;
  void Function()? onPressed;

用BoxDecoration来承载内容

decoration: BoxDecoration(
          color: color[100],
          borderRadius: BorderRadius.circular(12),
        ),

 Column来加载多个控件

child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Image.asset(
              imagePath,
              height: 64,
            ),
            Text(itemName),
            MaterialButton(
              onPressed: onPressed,
              color: color,
              child: Text(
                '\$$itemPrice',
                style: GoogleFonts.notoSerif(),
              ),
            ),
          ],
        ),

在商品列表遍历shopItems加载数据

return GroceryItemTile(
    itemName: value.shopItems[index][0],
    itemPrice: value.shopItems[index][1],
    imagePath: value.shopItems[index][2],
    color: value.shopItems[index][3],
    onPressed: (){
        Provider.of<CarModel>(
        context,listen: false
        ).addItemToCart(index);
    },
);

DEMO地址 

https://github.com/HelloJiada/flutter_cat_1https://github.com/HelloJiada/flutter_cat_1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值