flutter 全选_flutter 购物车功能

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter_app/http/Api.dart';

import 'package:flutter_app/http/ApiService.dart';

import 'package:flutter_app/ui/main/bean/ShopCartBean.dart';

import 'package:flutter_slidable/flutter_slidable.dart';//侧滑删除

class ShopPage extends StatefulWidget {

@override

State createState() => new ShopPageState();

}

class ShopPageState extends State {

List _list = new List();

var money = 0.00;

var selectCount = 0;

var listItemCount = 0;

@override

void initState() {

// TODO: implement initState

super.initState();

_categoryChild();

}

bool isSelect = false;

void _categoryChild() {

ApiService.getLogin("toBuy/list", "POST", null, (callBack) {

if (callBack != null) {

var categoryChildBean = ShopCartBean.fromJson(callBack);

if (categoryChildBean.success) {

_list.clear();

_list = categoryChildBean.result;

isSelect = _viewSelect();

_listMoneyCount();

}

}

}, (errorCallBack) => {});

}

_viewSelect() {

var count = 0;

for (var i = 0; i < _list.length; i++) {

if (_list[i].selected) {

count++;

}

}

return count == _list.length;

}

@override

Widget build(BuildContext context) {

// TODO: implement build

return new Scaffold(

appBar: new AppBar(

automaticallyImplyLeading: false, //左边按钮

centerTitle: true,

title: new Text(

"购物车($listItemCount)",

style: new TextStyle(color: Colors.white, fontSize: 20.0),

),

backgroundColor: Colors.red,

),

body: new Column(

children: [

new Expanded(

child: Container(

child: new ListView(

children: _itemView(),

padding: new EdgeInsets.only(bottom: 10),

),

width: double.infinity,

color: Colors.grey,

)),

Row(

children: [

new Checkbox(

value: isSelect,

activeColor: Color.fromARGB(255, 255, 67, 78),

onChanged: (bool) {

for (var i = 0; i < _list.length; i++) {

_list[i].selected = !isSelect;

for (var j = 0; j < _list[i].goodsToBuyDtos.length; j++) {

_list[i].goodsToBuyDtos[j].selected = !isSelect;

}

}

isSelect = !isSelect;

_listMoneyCount();

setState(() {});

}),

Text("全选"),

Expanded(

child: Row(

mainAxisAlignment: MainAxisAlignment.end,

children: [

Text(

"不含运费 ",

style: TextStyle(color: Colors.grey, fontSize: 10),

),

Text(

"合计:",

style: TextStyle(color: Colors.black, fontSize: 12),

),

Text(

"¥",

style: TextStyle(color: Colors.red, fontSize: 12),

),

Text(

"$money",

style: TextStyle(

color: Colors.red,

fontSize: 16,

),

),

Container(

alignment: Alignment.center,

child: new Text(

"结算($selectCount)",

style: TextStyle(color: Colors.white),

),

width: 130,

height: 50,

color: Colors.red,

)

],

),

)

],

)

],

),

);

}

List _itemView() {

List listWidget = List();

for (var i = 0; i < _list.length; i++) {

var item = _list[i].selected;

listWidget.add(new Card(

color: Colors.white,

elevation: 0,

margin: new EdgeInsets.only(left: 10, right: 10, top: 10),

child: Column(

children: _itemViewChild(i, item),

),

));

}

return listWidget;

}

List _itemViewChild(int index, bool item) {

var row = new Row(

children: [

new Checkbox(

value: item,

activeColor: Color.fromARGB(255, 255, 67, 78),

onChanged: (bool) {

_list[index].selected = !item;

for (var j = 0; j < _list[index].goodsToBuyDtos.length; j++) {

_list[index].goodsToBuyDtos[j].selected = !item;

}

isSelect = _viewSelect();

_listMoneyCount();

setState(() {});

}),

Text(null != _list[index].storeName ? _list[index].storeName : "")

],

);

List listWidget = List();

listWidget.clear();

listWidget.add(row);

listWidget.add(new Baseline(

baseline: 1,

baselineType: TextBaseline.alphabetic,

child: new Container(

color: Color(0xFFededed),

height: 1,

width: double.infinity,

),

));

List listWidgetChild = List();

listWidgetChild.clear();

for (var b = 0; b < _list[index].goodsToBuyDtos.length; b++) {

var selected = _list[index].goodsToBuyDtos[b].selected;

listWidgetChild.add(new Padding(

padding: EdgeInsets.only(top: 10, bottom: 10),

child: new Slidable(

child: new Container(

child: new Row(

children: [

new Checkbox(

value: selected,

activeColor: Color.fromARGB(255, 255, 67, 78),

onChanged: (bool) {

_list[index].goodsToBuyDtos[b].selected = !selected;

var count = 0;

_list[index].goodsToBuyDtos.forEach((fe) {

if (fe.selected) {

count++;

}

});

_list[index].selected =

count == _list[index].goodsToBuyDtos.length;

isSelect = _viewSelect();

_listMoneyCount();

setState(() {});

}),

new Image.network(

Api.getInstance().photo +

_list[index].goodsToBuyDtos[b].path,

fit: BoxFit.fill,

width: 80,

height: 80,

),

Expanded(

child: new Padding(

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

child: new Column(

crossAxisAlignment: CrossAxisAlignment.start,

children: [

Text(

_list[index].goodsToBuyDtos[b].name,

maxLines: 2,

overflow: TextOverflow.ellipsis,

),

new Expanded(

child: new Column(

crossAxisAlignment: CrossAxisAlignment.start,

mainAxisAlignment: MainAxisAlignment.end,

children: [

Text(

_list[index].goodsToBuyDtos[b].skuCfg,

softWrap: false,

overflow: TextOverflow.ellipsis,

),

Text(

"¥${_list[index].goodsToBuyDtos[b].price}",

style: TextStyle(color: Colors.red),

softWrap: false,

overflow: TextOverflow.ellipsis,

)

],

))

],

),

),

)

],

),

height: 85,

),

delegate: new SlidableDrawerDelegate(),

secondaryActions: [

new IconSlideAction(

caption: '删除',

color: Colors.red,

icon: Icons.delete,

onTap: () => {},

),

]),

));

}

listWidget.add(new Column(

children: listWidgetChild,

));

return listWidget;

}

//计算金额

_listMoneyCount() {

money = 0.00;

selectCount = 0;

listItemCount = 0;

_list.forEach((f) {

f.goodsToBuyDtos.forEach((item) {

if (item.selected) {

var itemMoney = double.parse(item.price) * double.parse(item.count);

money = money + itemMoney;

selectCount++;

}

listItemCount++;

});

});

}

}

复制代码

ShopCartBean

import 'package:json_annotation/json_annotation.dart';

part 'ShopCartBean.g.dart';

@JsonSerializable()

class ShopCartBean {

@JsonKey(name: 'code')

String code;

@JsonKey(name: 'msg')

String msg;

@JsonKey(name: 'result')

List result;

@JsonKey(name: 'success')

bool success;

ShopCartBean(this.code,this.msg,this.result,this.success,);

factory ShopCartBean.fromJson(Map srcJson) => _$ShopCartBeanFromJson(srcJson);

}

@JsonSerializable()

class ShopCartResult {

@JsonKey(name: 'couponShow')

bool couponShow;

@JsonKey(name: 'goodsIdStr')

String goodsIdStr;

@JsonKey(name: 'goodsToBuyDtos')

List goodsToBuyDtos;

@JsonKey(name: 'selected')

bool selected;

@JsonKey(name: 'storeId')

String storeId;

@JsonKey(name: 'storeName')

String storeName;

ShopCartResult(this.couponShow,this.goodsIdStr,this.goodsToBuyDtos,this.selected,this.storeId,this.storeName,);

factory ShopCartResult.fromJson(Map srcJson) => _$ShopCartResultFromJson(srcJson);

}

@JsonSerializable()

class GoodsToBuyDtos {

@JsonKey(name: 'count')

String count;

@JsonKey(name: 'dValue')

String dValue;

@JsonKey(name: 'fee')

String fee;

@JsonKey(name: 'goodsId')

String goodsId;

@JsonKey(name: 'id')

String id;

@JsonKey(name: 'inventory')

String inventory;

@JsonKey(name: 'isGoodsNew')

bool isGoodsNew;

@JsonKey(name: 'limitDesc')

String limitDesc;

@JsonKey(name: 'maxBatch')

String maxBatch;

@JsonKey(name: 'memo')

String memo;

@JsonKey(name: 'minBatch')

String minBatch;

@JsonKey(name: 'name')

String name;

@JsonKey(name: 'path')

String path;

@JsonKey(name: 'price')

String price;

@JsonKey(name: 'selected')

bool selected;

@JsonKey(name: 'skuCfg')

String skuCfg;

@JsonKey(name: 'standardCfg')

String standardCfg;

@JsonKey(name: 'status')

String status;

@JsonKey(name: 'storeType')

String storeType;

GoodsToBuyDtos(this.count,this.dValue,this.fee,this.goodsId,this.id,this.inventory,this.isGoodsNew,this.limitDesc,this.maxBatch,this.memo,this.minBatch,this.name,this.path,this.price,this.selected,this.skuCfg,this.standardCfg,this.status,this.storeType,);

factory GoodsToBuyDtos.fromJson(Map srcJson) => _$GoodsToBuyDtosFromJson(srcJson);

}

复制代码

本人刚刚坑,代码没有进行拆分等优化 只是做了页面效果以及选中和未选中等逻辑操作(删除逻辑暂时没有写)

数据可以自己添加假数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter Zoomable Image 是一个用于 Flutter 应用程序的库,它提供了一个可缩放和拖动的图像小部件。使用 Flutter Zoomable Image,您可以轻松地实现图像的缩放、拖动和捏放手势操作。这对于创建具有可交互性的图像查看器和画廊等应用程序非常有用。 要使用 Flutter Zoomable Image,您需要在项目的 `pubspec.yaml` 文件中添加依赖项,并运行 `flutter packages get` 命令来获取库。 以下是一个简单的示例代码,演示了如何在 Flutter 中使用 Zoomable Image: ```dart import 'package:flutter/material.dart'; import 'package:flutter_zoomable_image/flutter_zoomable_image.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Zoomable Image Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Zoomable Image Demo'), ), body: Center( child: ZoomableImage( AssetImage('path/to/your/image.jpg'), placeholder: Center(child: CircularProgressIndicator()), backgroundColor: Colors.black, ), ), ); } } ``` 在上面的示例中,我们创建了一个简单的 Flutter 应用程序,其中包含一个使用 ZoomableImage 小部件的页面。ZoomableImage 接受一个 AssetImage 对象作为图像源,并提供了一些可选参数,例如 placeholder(用于在图像加载期间显示的小部件)和 backgroundColor(用于设置图像背景色)。 您可以根据自己的需求定制 Zoomable Image 的样式和行为。要了解更多关于 Flutter Zoomable Image 的信息和用法,请参考官方文档或库的 GitHub 页面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值