flutter购物车

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter_app/widget/custom_check.dart';

import 'package:flutter_slidable/flutter_slidable.dart';

class ImproShop extends StatefulWidget {

  ImproShop({Key key}) : super(key: key);

  @override

  _ImproShopState createState() => _ImproShopState();

}

class _ImproShopState extends State<ImproShop> {

  //是否管理

  bool isManger = true;

  bool value = false;

  List list = [

    {

      "id": 1,

      "title": "珍珠白,美白就用珍珠白",

      "subTitle": "有你白的更闪耀",

      "moeny": "99",

      "number": "1",

      "value": false,

    },

    {

      "id": 2,

      "title": "珍珠白,美白就用珍珠白",

      "subTitle": "有你白的更闪耀",

      "moeny": "88",

      "number": "1",

      "value": false,

    },

    {

      "id": 3,

      "title": "珍珠白,美白就fndsjjkfdjkfbfhfghfghdsgfdhdfhfghjdgfjd用珍珠白",

      "subTitle": "有你白的更闪耀",

      "moeny": "200",

      "number": "1",

      "value": false,

    },

    {

      "id": 4,

      "title": "珍珠白,美白就用珍珠白",

      "subTitle": "有你白的更闪耀",

      "moeny": "188",

      "number": "1",

      "value": false,

    },

  ];

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text("购物车"),

        actions: [

          InkWell(

            onTap:(){

              setState(() {

                this.isManger = !this.isManger;

              });

            },

            child: Center(

              child:Padding(padding: EdgeInsets.only(right: 10),

              child: Text(isManger?"管理":"恢复"),),

            ),

          )

        ],

      ),

      bottomNavigationBar: BottomAppBar(

        child: Container(

          height: 50,

          decoration:const BoxDecoration(

            border:Border(

              top:BorderSide(

                color: Colors.grey,

                width: 0.5

              )

            )

          ),

          child: Padding(padding: EdgeInsets.only(

            left: 20,right: 20,

          ),

          child: Row(

           mainAxisAlignment: MainAxisAlignment.spaceBetween,

          children: [

            Row(

              mainAxisSize: MainAxisSize.min,

              children: [

                 CustomCheck((){

              setState(() {

                this.value = !this.value;

              });

              _checkAll(this.value);

            }, this.value),

                Padding(padding: EdgeInsets.only(left: 10),

            child: Text("全选"),),

           

              ],

            ),

           isManger?Text("共:¥${getTotal()}",style: TextStyle(

              fontWeight: FontWeight.w700,

              fontSize: 18,

              color: Colors.pink

            ),):InkWell(

              onTap: (){

                  _deleteItems();

              },

              child:Container(

                padding: EdgeInsets.only(top:5,bottom: 5,left: 10,right: 10),

                decoration: BoxDecoration(

                  border: Border.all(

                    color: Colors.pink[200],

                    width: 0.5

                  ),

                  borderRadius: BorderRadius.all(Radius.circular(10))

                ),

                child: Text("删除",style: TextStyle(

                  color: Colors.pink

                ),),

              ),

            )

          ],

          ),

          ),

        ),),

      body: ListView(

        children: _item(),

      ),

    );

  }

  List<Widget> _item() {

    List<Widget> arr = [];

    // arr.add(Text("我是循环的"));

    Widget item;

    for (var index = 0; index < list.length; index++) {

     

      item = Slidable(

        key:ValueKey(index) ,

        //右滑

        endActionPane:ActionPane(

          motion:ScrollMotion(),

          children:[

            SlidableAction(

              onPressed:(value){

                _removeItem(list[index]);

              },

              icon: Icons.delete,

              label:"删除",

              backgroundColor: Colors.orange,

            )

          ]

        ) ,

        child: Container(

        width: double.infinity,

        // height: 100,

        padding: EdgeInsets.all(10.0),

        child: Row(

          mainAxisSize: MainAxisSize.max,

          children: [

            Container(

              child: CustomCheck(() {

                setState(() {

                  list[index]["value"] = !list[index]["value"];

                });

                getTotal();

                // print("打印数组list${list}");

              }, list[index]["value"]),

            ),

             Container(

               padding: EdgeInsets.only(left: 5),

                height: 64,

                width: 64,

                child: Image.network(

                  "https://img1.baidu.com/it/u=1849701997,2238263017&fm=26&fmt=auto",

                  fit: BoxFit.cover,

                ),

              ),

            _rightItem(list[index], index),

          ],

        ),

      ),

      );

     

      arr.add(item);

    }

    return arr;

  }

  //右边

  _rightItem(Map e, int index) {

    return Expanded(

        child: Padding(

            padding: EdgeInsets.only(left: 10,right: 20),

            child: Column(

              crossAxisAlignment: CrossAxisAlignment.start,

              mainAxisSize: MainAxisSize.min,

              children: [

                Container(

                  child: Text(

                    e["title"],

                    style: TextStyle(

                        overflow: TextOverflow.ellipsis,

                        fontWeight: FontWeight.bold,

                        fontSize: 14),

                  ),

                ),

                Padding(

                  padding: EdgeInsets.only(top: 5),

                  child: Text(

                    e["subTitle"],

                    style:

                        TextStyle(fontSize: 10.0, fontWeight: FontWeight.w400),

                  ),

                ),

                Padding(

                  padding: EdgeInsets.only(top: 5),

                  child: Row(

                    mainAxisAlignment: MainAxisAlignment.spaceBetween,

                    children: [

                      Text(e["moeny"]),

                      _numInput(e,index),

                    ],

                  ),

                )

              ],

            )

            )

            );

  }

  //购物车数量加减

  _numInput(Map e,int index) {

    return Row(

      mainAxisSize: MainAxisSize.min,

      children: [

        IconButton(onPressed: (){

          if(int.parse(e["number"])>1){

            int number = int.parse(e["number"]);

            number--;

            // print("打印减了之后得${number}");

            setState(() {

              list[index]["number"] = number.toString();

            });

            getTotal();

          }

        },

        icon: Icon(Icons.remove,size: 12.0,),

        ),

        Padding(padding: EdgeInsets.only(left: 5,right: 5),

        child: Text(e["number"]),),

        IconButton(onPressed: (){

          int addNumber = int.parse(e["number"]);

            addNumber++;

            setState(() {

              list[index]["number"] = addNumber.toString();

            });

            getTotal();

        }, icon: Icon(Icons.add,size: 12.0,))

      ],

    );

  }

  //获取价格

  String getTotal(){

    var arr ;

    double total = 0;

    arr = list.where((e){

     return e["value"] == true;

    });

    print("新组成得数组是${arr}");

    if(arr.toList().length ==  list.length){

      setState(() {

        this.value = true;

      });

    }

    else{

      setState(() {

        this.value = false;

      });

    }

     arr.toList().forEach((e) {

       total = total + double.parse(e["moeny"])*double.parse(e["number"]);

     });

    return total.toString();

  }

  //全选

  void _checkAll(bool value) {

    var newArr;

    if(this.value){

      newArr = list.map((e){

        return {...e,"value":true};

      });

    }

    else{

      newArr = list.map((e){

        return {...e,"value":false};

      } );

    }

    setState(() {

      this.list = newArr.toList();

    });

    print("全选之后得数组${list}");

  }


 

  //删除某一项

  void _removeItem(e) {

      list.remove(e);

      setState(() {

        this.list = list;

      });

  }

  //删除选中的

  void _deleteItems() {

    var deleArr;

    deleArr =list.where((e){

      return e["value"] == false;

    });

    setState(() {

      list = deleArr.toList();

    });

  }

}

//自定义多选框

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';


 

class CustomCheck extends StatefulWidget {

  final VoidCallback checkTap;

  final bool isChecked ;

  const CustomCheck( this.checkTap, this.isChecked,{Key key}) : super(key: key);

  @override

  _CustomCheckState createState() => _CustomCheckState();

}

class _CustomCheckState extends State<CustomCheck> {

  @override

  Widget build(BuildContext context) {

    return InkWell(

      onTap: widget.checkTap,

   

      child: ClipRRect(

        borderRadius: BorderRadius.all(Radius.circular(10)),

        child: Container(

        height: 20,

        width: 20,

        decoration: BoxDecoration(

       

           borderRadius: BorderRadius.all(Radius.circular(10)),

          border: Border.all(

            color: Colors.grey,

            width: 1.0

          ),

        ),

        child:

        Padding(padding: EdgeInsets.all(4),

          child:  Container(

            decoration: BoxDecoration(

              color:widget.isChecked?Colors.blue:Colors.white,

              borderRadius: BorderRadius.all(Radius.circular(6))

            ),

         

        )

        )

       

       

      ),

      )

     

    );

  }

 

}

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值