栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

Flutter自定义组件-MultiShower

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter自定义组件-MultiShower

1. 先说需求

Flutter中有多如牛毛的控件,控件有多如牛毛的属性,属性又有多如牛毛的枚举或静态常量
是不是想想都头皮发麻,TM这么多我怎么玩。在思考如何玩转属性,然后便有此文。

本文你可以学到:

[1].自定义无状态组件的流程
[2].回调方法的使用
[3].如何批量生成你想要的组件
[4].清晰认识:TextDecoration、BoxFit、BlendMode、Alignment


2.MultiShower的使用 2.1:测试1-TextDecoration

var decorations = [TextDecoration.none,TextDecoration.lineThrough,
  TextDecoration.overline,TextDecoration.underline];

var show = MultiShower(
    decorations, (type) => Text("张风捷特烈",
   style: TextStyle(
fontSize: 20, //字号
decoration: type),
 ));


2.2:测试2-BoxFit

可指定容器的宽高和颜色

var fitModes = [BoxFit.none,BoxFit.contain,BoxFit.cover,
  BoxFit.fill,BoxFit.fitHeight,BoxFit.fitWidth,BoxFit.scaleDown];

var show = MultiShower(
  fitModes,
  (type) => Image(
    image: AssetImage("images/wy_300x200.jpg"),
    fit: type,
  ),
  width: 150,//容器宽
  color: Colors.red,//容器颜色
);


2.3:测试3-BlendMode

这如果一个一个测试出来,还不累死人

//叠合模式数组
var colorBlendModes = [
  BlendMode.clear,BlendMode.src,BlendMode.dst,
  BlendMode.srcOver,BlendMode.dstOver,BlendMode.srcIn,
  BlendMode.dstIn,BlendMode.srcOut,BlendMode.dstOut,
  BlendMode.srcATop,BlendMode.dstATop,BlendMode.xor,
  BlendMode.plus, BlendMode.modulate,BlendMode.screen,
  BlendMode.overlay,BlendMode.darken,BlendMode.lighten,
  BlendMode.colorDodge,BlendMode.colorBurn,BlendMode.hardLight,
  BlendMode.softLight,BlendMode.difference,BlendMode.exclusion,
  BlendMode.multiply,BlendMode.hue,BlendMode.saturation,
  BlendMode.color, BlendMode.luminosity,
];

var show = MultiShower(
  colorBlendModes,
      (mode) => Image(
 image: AssetImage("images/icon_head.png"),
 color: Colors.blue,
 colorBlendMode: mode,
      ),
  width: 60,
  height: 60,
);


2.4:测试4-Alignment

可以自定义下发的文字

var alignments = [
  Alignment.center, Alignment.centerLeft, Alignment.centerRight,
  Alignment.topCenter, Alignment.topLeft, Alignment.topRight,
  Alignment.bottomCenter, Alignment.bottomLeft, Alignment.bottomRight,
  Alignment(0, 0), Alignment(0.5, 0.5)
];

var alignmentInfos = [//下面的文字
  "center", "centerLeft", "centerRight",
  "topCenter", "topLeft", "topRight", "bottomCenter", "bottomLeft", "bottomRight",
  "Alignment(0,0)", "Alignment(0.5,0.5)"
];

var show = MultiShower(
  alignments,
      (mode) => Align(
 alignment: mode,
 child: Container(
   width: 40,
   height: 40,
   color: Colors.red,
 ),
      ),
  infos: alignmentInfos,
  width: 100,
);


3.组件的实现

看起来好像很厉害的感觉,如何实现的呢?

3.1:定义MultiShower类继承自StatelessWidget

继承StatelessWidget需要实现build抽象方法,返回Widget对象

import 'package:flutter/material.dart';

class MultiShower extends StatelessWidget {
  MultiShower();

  @override
  Widget build(BuildContext context) {
    return null;
  }
}


3.2:确定属性和入参
import 'package:flutter/material.dart';

class MultiShower extends StatelessWidget {
  MultiShower(
    this.list,
    this.call, {
    this.width = 110,
    this.height = 110 * 0.618,
    this.infos,
    this.color = Colors.cyanAccent,
  });

  final List list;
  final List infos;
  final Function call;
  final double width;
  final double height;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return null;
  }
}


3.3:实现build方法

核心就是在遍历列表时回调出来列表元素,以供使用。

@override
Widget build(BuildContext context) {
  var li = [];
  for (var i = 0; i < list.length; i++) {
    var child = Container(
 margin: EdgeInsets.all(7),
 color: color,
 width: width,
 height: height,
 child: call(list[i]));
    li.add(Column(
      children: [
 child,
 Text(infos != null ? infos[i] : list[i].toString().split(".")[1])
      ],
    ));
  }
  return Wrap(
    children: li,
  );
}

好了,这样就OK了,是不是没有想象中的那么难,最后贴个完整的


4.全类展示
import 'package:flutter/material.dart';

class MultiShower extends StatelessWidget {
  MultiShower(
    this.list,
    this.call, {
    this.width = 110,
    this.height = 110 * 0.618,
    this.infos,
    this.color = Colors.cyanAccent,
  });

  final List list;
  final List infos;
  final Function call;
  final double width;
  final double height;
  final Color color;

  @override
  Widget build(BuildContext context) {
    var li = [];
    for (var i = 0; i < list.length; i++) {
      var child = Container(
   margin: EdgeInsets.all(7),
   color: color,
   width: width,
   height: height,
   child: call(list[i]));
      li.add(Column(
 children: [
   child,
   Text(infos != null ? infos[i] : list[i].toString().split(".")[1])
 ],
      ));
    }
    return Wrap(
      children: li,
    );
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/242119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号