前言

最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器

提示:以下是本篇文章正文内容,下面案例可供参考

一、BarTypeColorPicker

条形选色板的功能实现,颜色的获取是通过位置来判断,然后赋予相应的颜色。这个封装好的组件可通过colorListener、onTapUpListener的回调方法,把颜色传出去。

代码如下(示例):

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


///描述:条形选色板

class BarTypeColorPicker extends StatefulWidget {
  final Color initialColor;
  final ValueChanged<Color> colorListener;
  final ValueChanged<Color> onTapUpListener;
  final int colorWidth;
  final int colorHeight;
  final Color color;

  const BarTypeColorPicker(
      {Key key,
      this.initialColor,
      this.colorListener,
      this.onTapUpListener,
      this.colorWidth,
      this.colorHeight,
      this.color})
      : super(key: key);

  @override
  _BarTypeColorPickerState createState() => _BarTypeColorPickerState(
        colorWidth.toDouble(),
        colorHeight.toDouble(),
      );
}

class _BarTypeColorPickerState extends State<BarTypeColorPicker> {
  double _top = 0.0;
  double _left = 0.0;
  double _Thumbsize = 20;
  double lightness;
  double _colorwidth;
  double _colorHeight;
  Color _color;

  _BarTypeColorPickerState(double colorwidth, double colorHeight) {
    this._colorwidth = colorwidth;
    this._colorHeight = colorHeight;
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        onPanStart: (DragStartDetails detail) {
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if (widget.colorListener != null) {
            widget.colorListener(_color);
          }
        },
        onPanDown: (DragDownDetails detail) {
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if (widget.colorListener != null) {
            widget.colorListener(_color);
          }
        },
        onPanUpdate: (DragUpdateDetails detail) {
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if (widget.colorListener != null) {
            widget.colorListener(_color);
          }
        },
        onPanEnd: (DragEndDetails detail) {
          if (widget.onTapUpListener != null) {
            widget.onTapUpListener(_color);
          }
        },
        onTapUp: (TapUpDetails detail) {
          if (widget.onTapUpListener != null) {
            widget.onTapUpListener(_color);
          }
        },
        child: ColorRect(
            colorHeight: _colorHeight,
            colorwidth: _colorwidth,
            top: _top,
            Thumbsize: _Thumbsize,
            left: _left,
            color: _color),
      ),
    );
  }

  void buildSetState(Offset localPosition, BuildContext context) {
    return setState(() {
      _left = localPosition.dx;
      _top = localPosition.dy;
      if (_left < 0) {
        _left = 0;
      } else if (0 <= _left && _left <= _colorwidth) {
        _left = _left;
      } else if (_left > _colorwidth) {
        _left = (_colorwidth);
      }
      if ((_colorwidth / 7) * 0 < _left && _left < (_colorwidth / 7) * 1) {
        _color = Color(0xFFFF0000);
      } else if ((_colorwidth / 7) * 1 < _left &&
          _left < (_colorwidth / 7) * 2) {
        _color = Color(0xFFFFFF00);
      } else if ((_colorwidth / 7) * 2 < _left &&
          _left < (_colorwidth / 7) * 3) {
        _color = Color(0xFF00FF00);
      } else if ((_colorwidth / 7) * 3 < _left &&
          _left < (_colorwidth / 7) * 4) {
        _color = Color(0xFF00FFFF);
      } else if ((_colorwidth / 7) * 4 < _left &&
          _left < (_colorwidth / 7) * 5) {
        _color = Color(0xFF0000FF);
      } else if ((_colorwidth / 7) * 5 < _left &&
          _left < (_colorwidth / 7) * 6) {
        _color = Color(0xFFFF00FF);
      } else if ((_colorwidth / 7) * 6 < _left &&
          _left < (_colorwidth / 7) * 7) {
        _color = Color(0xFFFFFFFF);
      }
      if (_top <= 0) {
        _top = 0;
      } else if (0 <= _top && _top <= _colorHeight) {
      } else if (_top > _colorHeight) {
        _top = _colorHeight;
      }
    });
  }
}

class ColorRect extends StatelessWidget {
  ColorRect({
    Key key,
    @required double colorHeight,
    @required double colorwidth,
    @required double top,
    @required double Thumbsize,
    @required double left,
    @required Color color,
  })  : _colorHeight = colorHeight,
        _colorwidth = colorwidth,
        _top = top,
        _Thumbsize = Thumbsize,
        _left = left,
        _color = color,
        super(key: key);

  final double _colorHeight;
  final double _colorwidth;
  final double _top;
  final double _Thumbsize;
  final double _left;
  final Color _color;
  List<Color> colorList = [
    Color(0xFFFF0000),
    Color(0xFFFFFF00),
    Color(0xFF00FF00),
    Color(0xFF00FFFF),
    Color(0xFF0000FF),
    Color(0xFFFF00FF),
    Color(0xFFFFFFFF),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      width: _colorwidth,
      height: _colorHeight,
      child: Stack(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: colorList
                .map(
                  (e) => Container(
                    padding: EdgeInsets.symmetric(horizontal: 2),
                    height: _colorHeight,
                    width: _colorwidth / 7,
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        color: e,
                      ),
                    ),
                  ),
                )
                .toList(),
          ),
          Container(
              child: Thumb(
                  top: _top,
                  Thumbsize: _Thumbsize,
                  left: _left,
                  color: _color)),
        ],
      ),
    );
  }
}

class Thumb extends StatelessWidget {
  const Thumb({
    Key key,
    @required double top,
    @required double Thumbsize,
    @required double left,
    @required Color color,
  })  : _top = top,
        _Thumbsize = Thumbsize,
        _left = left,
        _color = color,
        super(key: key);

  final double _top;
  final double _Thumbsize;
  final double _left;
  final Color _color;

  @override
  Widget build(BuildContext context) {
    return Positioned(
        top: _top - _Thumbsize / 2,
        left: _left - _Thumbsize / 2,
        child: GestureDetector(
            child: Container(
          child: Icon(
            Icons.circle,
            color: _color,
            size: _Thumbsize,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            boxShadow: [
              BoxShadow(
                blurRadius: 0.1, //阴影范围
                spreadRadius: 0.001, //阴影浓度
                color: Colors.black, //阴影颜色
              ),
            ],
          ),
        )));
  }
}

下面是使用方法。

BarTypeColorPicker(
   initialColor: Colors.white, 
   colorWidth: 360,
   colorHeight: 150,
),

具体效果图:

一、RectangleColorPicker

矩形选色板的功能实现,颜色的获取是通过位置的坐标值转换为相应的颜色。这个封装好的组件可通过colorListener、onTapUpListener的回调方法,把颜色传出去。

代码如下(示例):

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RectangleColorPicker extends StatefulWidget {
  final Color initialColor;
  final ValueChanged<Color> colorListener;
  final ValueChanged<Color> onTapUpListener;
  final int colorWidth;
  final int colorHeight;
  final Color color;

  const RectangleColorPicker(
      {Key key,
      this.initialColor,
      this.colorListener,
      this.onTapUpListener,
      this.colorWidth,
      this.colorHeight,
      this.color})
      : super(key: key);

  @override
  _RectangleColorPickerState createState() => _RectangleColorPickerState(
        colorWidth.toDouble(),
        colorHeight.toDouble(),
      );
}

class _RectangleColorPickerState extends State<RectangleColorPicker> {
  double _top = 0.0;
  double _left = 0.0;
  double _Thumbsize = 20;
  double _hue = 0.0;
  double _brightnum = 50.0;
  double lightness;
  double _colorwidth;
  double _colorHeight;

  _RectangleColorPickerState(double colorwidth, double colorHeight) {
    this._colorwidth = colorwidth;
    this._colorHeight = colorHeight;
  }

  Color get _color {
    return HSLColor.fromAHSL(
      1,
      _hue,
      1,
      lightness,
    ).toColor();
    //返回HSL、AHSL格式的色调亮度字符串
  }

  @override
  void initState() {
    super.initState();
    HSLColor hslColor = HSLColor.fromColor(widget.initialColor);
    _left = (_colorwidth * hslColor.hue) / 360;
    _top = (_colorHeight * (hslColor.lightness - 0.5) * 200) / 100;
    this._hue = hslColor.hue;
    this.lightness = hslColor.lightness;
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        onPanStart: (DragStartDetails detail) {
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if(widget.colorListener != null){
            widget.colorListener(_color);
          }
          
        },
        onPanDown: (DragDownDetails detail) {
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if(widget.colorListener != null){
            widget.colorListener(_color);
          }
        },
        onPanUpdate: (DragUpdateDetails detail) {
          //获取当前触摸点的局部坐标
          var localPosition = detail.localPosition;
          buildSetState(localPosition, context);
          if(widget.colorListener != null){
            widget.colorListener(_color);
          }

        },
        onPanEnd: (DragEndDetails detail) {
          if(widget.onTapUpListener != null){
            widget.onTapUpListener(_color);
          }
        },
        onTapUp: (TapUpDetails detail){
          if(widget.onTapUpListener != null){
            widget.onTapUpListener(_color);
          }
        },
        child: ColorRect(
            colorHeight: _colorHeight,
            colorwidth: _colorwidth,
            top: _top,
            Thumbsize: _Thumbsize,
            left: _left,
            color: _color),
      ),
    );
  }

  void buildSetState(Offset localPosition, BuildContext context) {
    return setState(() {
      _left = localPosition.dx;
      _top = localPosition.dy;
      if (_left < 0) {
        _left = 0;
      } else if (0 <= _left && _left <= _colorwidth) {
        _left = _left;
        _hue = (360 * _left) / (_colorwidth);
      } else if (_left > _colorwidth) {
        _left = (_colorwidth);
        _hue = 360;
      }
      if (((5 / 360 - 5 / 360) < _left &&
              _left < (5 / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = 5 / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   350 / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   350 / 6) / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = (5   (1 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   (2 * 350) / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   (2 * 350) / 6) / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = (5   (2 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   (3 * 350) / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   (3 * 350) / 6) / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = (5   (3 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   (4 * 350) / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   (4 * 350) / 6) / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = (5   (4 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   (5 * 350) / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   (5 * 350) / 6) / 360   5 / 360) * _colorwidth) &&
          (_top < 5)) {
        _left = (5   (5 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      } else if ((((5   (6 * 350) / 6) / 360 - 5 / 360) * _colorwidth < _left &&
              _left < ((5   (6 * 350) / 6) / 360   5 / 360)) &&
          (_top < 5)) {
        _left = (5   (6 * 350) / 6) / 360 * _colorwidth;
        _top = 0;
      }
      if (_top <= 0) {
        _top = 0;
        _brightnum = 50;
        lightness = _brightnum / 100;
      } else if (0 <= _top && _top <= _colorHeight) {
        _brightnum = (100 * _top) / _colorHeight / 2   50;
        lightness = _brightnum / 100;
      } else if (_top > _colorHeight) {
        _top = _colorHeight;
        _brightnum = 100;
        lightness = _brightnum / 100;
      }
    });
  }
}

class ColorRect extends StatelessWidget {
  const ColorRect({
    Key key,
    @required double colorHeight,
    @required double colorwidth,
    @required double top,
    @required double Thumbsize,
    @required double left,
    @required Color color,
  })  : _colorHeight = colorHeight,
        _colorwidth = colorwidth,
        _top = top,
        _Thumbsize = Thumbsize,
        _left = left,
        _color = color,
        super(key: key);

  final double _colorHeight;
  final double _colorwidth;
  final double _top;
  final double _Thumbsize;
  final double _left;
  final Color _color;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [
          Container(
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: DecoratedBox(
                  child: Container(
                    height: _colorHeight,
                    width: _colorwidth,
                  ),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.centerLeft,
                      end: Alignment.centerRight,
                      stops: [
                        0,
                        5 / 360,
                        (5   350 / 6) / 360,
                        (5   (2 * 350) / 6) / 360,
                        (5   (3 * 350) / 6) / 360,
                        (5   (4 * 350) / 6) / 360,
                        (5   (5 * 350) / 6) / 360,
                        (5   (6 * 350) / 6) / 360,
                        1.0
                      ],
                      colors: [
                        Color.fromARGB(255, 255, 0, 0),
                        Color.fromARGB(255, 255, 0, 0),
                        Color.fromARGB(255, 255, 255, 0),
                        Color.fromARGB(255, 0, 255, 0),
                        Color.fromARGB(255, 0, 255, 255),
                        Color.fromARGB(255, 0, 0, 255),
                        Color.fromARGB(255, 255, 0, 255),
                        Color.fromARGB(255, 255, 0, 0),
                        Color.fromARGB(255, 255, 0, 0),
                      ],
                    ),
                  )),
            ),
          ),
          Container(
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: DecoratedBox(
                  child: Container(
                    height: _colorHeight,
                    width: _colorwidth,
                  ),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        Color.fromARGB(0, 255, 255, 255),
                        Colors.white,
                      ],
                    ),
                  )),
            ),
          ),
          Container(
              child: Thumb(
                  top: _top,
                  Thumbsize: _Thumbsize,
                  left: _left,
                  color: _color)),
        ],
      ),
    );
  }
}

class Thumb extends StatelessWidget {
  const Thumb({
    Key key,
    @required double top,
    @required double Thumbsize,
    @required double left,
    @required Color color,
  })  : _top = top,
        _Thumbsize = Thumbsize,
        _left = left,
        _color = color,
        super(key: key);

  final double _top;
  final double _Thumbsize;
  final double _left;
  final Color _color;

  @override
  Widget build(BuildContext context) {
    return Positioned(
        top: _top - _Thumbsize / 2,
        left: _left - _Thumbsize / 2,
        child: GestureDetector(
            child: Container(
          child: Icon(
            Icons.circle,
            color: _color,
            size: _Thumbsize,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            boxShadow: [
              BoxShadow(
                blurRadius: 0.1, //阴影范围
                spreadRadius: 0.001, //阴影浓度
                color: Colors.black, //阴影颜色
              ),
            ],
          ),
        )));
  }
}

下面是使用方法。

RectangleColorPicker(
   [initialColor: Colors.white, 
   colorWidth: 360,
   colorHeight: 150,
   onTapUpListener: (_color) { }
   colorListener: (_color) { }
   ]
),

具体效果图:

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了两种颜色选色板的使用,后面还会陆续发布一些flutter的组件使用教程。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

Flutter实现矩形取色器的封装的更多相关文章

  1. Flutter中文教程-Cookbook

    Flutter中文网的Cookbook中包含了在编写Flutter应用程序时常见问题及示例。设计基础使用主题共享颜色和字体样式Images显示来自网上的图片用占位符淡入图片使用缓存图Lists创建一个基本list创建一个水平list使用长列表创建不同类型子项的List创建一个gridList处理手势处理点击添加Material触摸水波效果实现滑动关闭导航导航到新页面并返回给新页面传值从新页面返回数据给上一个页面网络从网上获取数据进行认证请求使用WebSockets

  2. android-studio – 未配置Dart SDK

    Initializinggradle…

  3. 安卓 – 从一个扑动的应用程序拨打电话

    或者有更好的选择从我的应用程序拨打电话?

  4. android – 如何在Flutter中添加Webview?

    我知道可以将WebView添加为整页,但找不到任何示例代码.我假设你可以使用PageView作为它的基础,但不知道如何调用本机androidWebView并将其添加到PageView.谁能指出我正确的方向?

  5. android – 如何将消息从Flutter传递给Native?

    如果需要与特定的API/硬件组件进行交互,您如何将Flutter的信息传递回Android/Native代码?是否有任何事件频道可以通过其他方式发送信息或类似于回调?

  6. android – 如何在Flutter App中处理onPause / onResume?

    我是否过于复杂的事情?即使我的用例似乎不需要它,我仍然想知道:如何自己处理onPause/onResume事件?

  7. android – 如何使用Flutter构建Augment Reality应用程序?

    我对Android开发有一些基础知识.最近听说过Flutter并且非常有兴趣研究它.我想知道是否有可能使用颤振构建增强现实应用程序以及要实现此目的的方法?请帮忙.解决方法截至目前,颤振不支持3D.Flutter现在专注于2D,团队长期计划为颤振提供优化的3Dapi.你读了常见问题here.

  8. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. Flutter StreamBuilder实现局部刷新实例详解

    这篇文章主要为大家介绍了Flutter StreamBuilder实现局部刷新实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. Flutter 首页必用组件NestedScrollView的示例详解

    今天介绍的组件是NestedScrollView,大部分的App首页都会用到这个组件。对Flutter 首页必用组件NestedScrollView的相关知识感兴趣的一起看看吧

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部