Flutter学习-自定义列表单元组件

参数说明

参数默认值说明
id回调函数关联主键
title列表单元标题
colorColors.black列表单元标题颜色
TfontWeightFontWeight.w500列表单元标题字体粗细
Tsize18列表单元标题字号大小
descThe msg is the Desc !列表单元备注信息
dcolorColors.black38列表单元备注信息颜色
imgpath列表单元图像
rightIconIcons.more_horiz列表单元右侧点击图标
onPressed列表单元右侧点击图标回调函数
onTap列表单元点击回调函数

使用参考

widget.DataForm = [
	{
	'id': 1,
	'title': 'test',
	'code': '001',
	'descshow':'The msg is the Desc !',
	'imgpath': 'https://www.eatqionline.top:8008/media/img/logo/maifeng_dgHndBi.png'
	}
]
_MakeList() {
    List<Widget> theList = [];
    theList = [
      SizedBox(height: widget.FrontHeight,),// 前置高度
    ];
    for(var item in widget.DataForm) {
      theList.add(MyWaresListElement(
        title:item['title'],
        code:item['code'],
        color: Colors.black,
        TfontWeight:FontWeight.w500,// FontWeight.bold,18,
        Tsize:18,
        desc:item['descshow'],// 'The msg is the Desc !',
        dcolor:Colors.black38,// Colors.black38,
        imgpath:item['imgpath'],
        id:item['id'],
        onPressed:(){widget.onPressed(item);},// (){MyToast.success2(item['id'].toString());},
        onTap:(){widget.onTap(item['id']);},// onTap: (){MyToast.success2('点击' + item['id'].toString());},
      ));
    }
    theList.add(SizedBox(height: 50,));
    return theList;
  }

效果图

在这里插入图片描述

列表单元组件代码

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';

class MyWaresListElement extends StatefulWidget {
  // 传入参数
  const MyWaresListElement({
    Key? key,
    required this.id,// 列表单元中对应数据List的id
    required this.title,// 列表单元标题
    this.color: Colors.black,// 列表单元标题颜色
    this.TfontWeight: FontWeight.w500,// 列表单元标题字体粗细
    this.Tsize:18,// 列表单元标题字号大小
    this.desc: 'The msg is the Desc !',// 列表单元备注信息
    this.dcolor: Colors.black38,// 列表单元备注信息颜色
    required this.imgpath,// 列表单元图像
    this.rightIcon: Icons.more_horiz,// 列表单元右侧点击图标
    required this.onPressed,// 列表单元右侧点击图标回调函数
    required this.onTap,// 列表单元点击回调函数
    this.code:'',
  }) : super(key: key,);
  final int id;
  final String title;
  final String code;
  final Color color;
  final FontWeight TfontWeight;
  final double Tsize;
  final String desc;
  final Color dcolor;
  final String imgpath;
  final VoidCallback onPressed;
  final VoidCallback onTap;
  final IconData rightIcon;
  @override
  State<StatefulWidget> createState() => MyWaresListElementState();
}

class MyWaresListElementState extends State<MyWaresListElement> {
  @override
  Widget build(BuildContext context) {
    return OneListLine(
        widget.color,
        widget.code,
        widget.title,
        widget.TfontWeight,
        widget.Tsize,
        widget.desc,
        widget.dcolor,
        widget.imgpath,
        widget.id,
        widget.onPressed,
        widget.onTap
    );
  }
}

// 标题
Widget Title(Color tcolor,String title,FontWeight fontWeight,double Size){
  return Container(
    padding: const EdgeInsets.only(bottom: 8.0),
    child: new Text(
      title,
      style: new TextStyle(
        color:tcolor,
        fontSize: Size-4,
        fontWeight: fontWeight,
      ),
    ),
  );
}
// 编码(因为这里是在构建商品列表单元,用不上可以自行删除,或者设置为null)
Widget Code(Color tcolor,String code,FontWeight fontWeight,double Size){
  return Container(
    padding: const EdgeInsets.only(bottom: 2.0),
    child: new Text(
      code,
      style: new TextStyle(
        color:tcolor,
        fontSize: Size,
        fontWeight: fontWeight,
      ),
    ),
  );
}

// 说明信息
Widget Desc(String desc,Color dcolor){
  return Text(
    desc,// 'The msg is the Desc !',
    style: new TextStyle(
      color: dcolor,// Colors.grey[500],
    ),
  );
}
// 组合信息部分(编码+标题+说明)
Widget DataHead(Color tcolor,String code,String title,FontWeight TfontWeight,double Tsize,String desc,Color dcolor){
 return Expanded(
   child: new Column(
     crossAxisAlignment: CrossAxisAlignment.start,
     children: [
       Code(tcolor,code,TfontWeight,Tsize),
       Title(tcolor,title,TfontWeight,Tsize),
       Desc(desc,dcolor),
     ],
   ),
 );
}
// 单行左边图片
Widget ListHeadLeft(String imgpath){
  return Container(
      padding: const EdgeInsets.fromLTRB(0, 0, 15, 0),
      child: new Image.network(
	  imgpath==''?'https://www.eatqionline.top:8008/media/img/logo/maifeng_dgHndBi.png':imgpath,
      width: 50.0,
      height: 50.0,
      fit: BoxFit.cover,
      )
    );
}
// 单行右侧点击图标
Widget LineCheck(int id,Function onPressed){
    return Container(
      margin: EdgeInsets.fromLTRB(0, 30, 0, 0),
      child: ButtonCheck(onPressed), // PopupTap(onPressed: (){onPressed();},id: id,)// ButtonCheck(onPressed)
    );
}
// 右侧点击按钮
Widget ButtonCheck(Function onPressed){
  return TextButton(
    // onPressed: () {MyToast.success2('$id');} ,
      onPressed: () {onPressed();},
      style: ButtonStyle(
        foregroundColor: MaterialStateProperty.all(Color.fromARGB(255, 53, 53, 53)),
        minimumSize: MaterialStateProperty.all(Size(10, 5)),
        padding: MaterialStateProperty.all(EdgeInsets.zero),
        // backgroundColor: MaterialStateProperty.all(Colors.grey),
      ),
      child: new Row(
        children: [
          new Icon(widget.rightIcon,),
        ],
      )
  );
}

// 组合列表单行(图片+组合信息部分+按钮)
Widget OneListLine(Color tcolor,String code,String title,FontWeight TfontWeight,double Tsize,String desc,Color dcolor,String imgpath,int id,Function onPressed,Function onTap){
  return InkWell(
    onTap: () {onTap();},
      child:Container(
      padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
      child: new Container(
          padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
          decoration: BoxDecoration(
            border: new Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)) ),
          ),
          child:new Row(
            children: [
              ListHeadLeft(imgpath),
              // DataHead('Title',FontWeight.bold,18,'The msg is the Desc !',Colors.black38),
              DataHead(tcolor,code,title,TfontWeight,Tsize,desc,dcolor),
              LineCheck(id,onPressed),
            ],
          )
      )
  ),);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isSamle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值