Flutter自定义圆盘取色器

本文实例为大家分享了Flutter自定义圆盘取色器的具体代码,供大家参考,具体内容如下

下面展示一些 内联代码片。

圆盘取色器
效果图

完整代码

import 'dart:math';

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

class CircularColorFinder extends StatefulWidget {
  Color color;
  double size;
  double thumbSize;
  ValueChanged<Color> onColorChange;
  ValueChanged<Color> onColorChangeEnd;
  CircularColorFinder({Key key,this.color,this.size=300,this.thumbSize=25,this.onColorChange,this.onColorChangeEnd}) : super(key: key);

  @override
  _CircularColorFinderState createState() => _CircularColorFinderState();
}

class _CircularColorFinderState extends State<CircularColorFinder> {
  Offset topPosition;
  Offset center;
  Offset position;
  Offset currentOffset;
  double radians = 0;
  double radius;
  double hue = 0;
  Color color;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    radius = widget.size/2;
    color = Colors.white;
    topPosition = Offset(0, widget.size/2);
    center = Offset(widget.size/2, widget.size/2);
    position = Offset(widget.size/2, widget.size/2);
    currentOffset = Offset(widget.size/2, widget.size/2);
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      width: widget.size+40,
      height: widget.size+40,
      child: Stack(
        children: [
          Container(
            width: widget.size+40,
            height: widget.size+40,
            alignment: Alignment.center,
            child: GestureDetector(
              onPanDown: onPanDown,
              onPanUpdate: onPanUpdate,
              onPanEnd: onPanEnd,
              child: Container(
                alignment: Alignment.center,
                width: widget.size,
                height: widget.size,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Transform.rotate(
                      angle:pi,
                      child: CustomPaint(
                        painter: CircularColorPainter(
                          color: color,
                          center: center,
                          radius: radius,
                        ),
                        child: Container(
                          alignment: Alignment.center,
                          width: widget.size,
                          height: widget.size,
                        ),
                      ),
                    ),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.wb_sunny_sharp,color: Color.fromRGBO(183, 202, 208, 1.0)),
                            Text('Warm',style: TextStyle(color: Color.fromRGBO(183, 202, 208, 1.0)),),
                            Text('White',style: TextStyle(color: Color.fromRGBO(183, 202, 208, 1.0)),),
                          ],
                        ),
                        SizedBox(width: 15,),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.brightness_2_sharp,color: Color.fromRGBO(183, 202, 208, 1.0)),
                            Text('Cool',style: TextStyle(color: Color.fromRGBO(183, 202, 208, 1.0)),),
                            Text('White',style: TextStyle(color: Color.fromRGBO(183, 202, 208, 1.0)),),
                          ],
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
          Thumb(
            // top: 0,
            // left: 0,
            top: currentOffset.dy+20,
            left: currentOffset.dx+20,
            thumbSize:widget.thumbSize,
            color: color,
          ),
        ],
      ),
    );
  }
  ///获取象限
  static int getQuadrant(double x, double y, double size) {
    if (x >= size / 2) {
      return y >= size / 2 ? 3 : 4;
    }
    return y >= size / 2 ? 2 : 1;
  }
  onPanDown(DragDownDetails details){
    change(details);
    widget.onColorChange(color);
  }
  onPanUpdate(DragUpdateDetails details){
    change(details);
    widget.onColorChange(color);
  }
  onPanEnd(DragEndDetails details){
    widget.onColorChangeEnd(color);
  }
  change(details){
    setState(() {
      position = Offset(details.localPosition.dx, details.localPosition.dy);
    });
    double cosA;
    double x = (sqrt(pow((topPosition.dx - center.dx), 2) + pow((topPosition.dy - center.dy), 2)));
    double y = (sqrt(pow((position.dx - center.dx), 2) + pow((position.dy - center.dy), 2)));
    double z = (sqrt(pow((topPosition.dx - position.dx), 2) + pow((topPosition.dy - position.dy), 2)));
    cosA = (x * x + y * y - z * z) / (2 * x * y);
    int quadrant = getQuadrant(position.dx, position.dy, center.dx*2);
    radians = acos(cosA);
    if (quadrant == 1 || quadrant == 4) {
      radians = acos(cosA);
    } else if (quadrant == 2 || quadrant == 3) {
      radians = 2 * pi - acos(cosA);
    }
    hue = 360*radians/(2*pi);
    // print('radians:${radians} hue:${hue}');
    color = HSVColor.fromAHSV(1, hue, 1, 1).toColor();
    if(y<=center.dx){
      currentOffset = Offset(details.localPosition.dx, details.localPosition.dy);
    }else{
      double dx = position.dx - center.dx;
      double dy = position.dy - center.dy;
      double distance = sqrt(dx * dx + dy * dy);
      double ratio = radius / distance;
      currentOffset = Offset(dx * ratio + center.dx, dy * ratio + center.dy);
    }
    if(y<=center.dx/2){
      if(position.dx<widget.size/2){
        color =Color.fromRGBO(255, 225, 178, 1.0);
      }else{
        color = Color.fromRGBO(220, 244, 255, 1.0);
      }
    }
  }
}

class CircularColorPainter extends CustomPainter {
  Color color;
  double radius;
  Offset center;
  List<Color> colorList;
  CircularColorPainter({this.color,this.center,this.radius});

  @override
  void paint(Canvas canvas, Size size) {
    Paint _paint = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 1
      ..color = Colors.black;
    Paint _paint2 = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 1
      ..color = Colors.black;
    Paint _paint3 = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1
      ..color = Colors.black;
    Paint _paint4 = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 0
      ..color = Color.fromRGBO(color.red, color.green, color.blue, 0.3);
    colorList = [Color(0xFFFF0000),Color(0xFFFFFF00),Color(0xFF00FF00),Color(0xFF00FFFF),Color(0xFF0000FF),Color(0xFFFF00FF),Color(0xFFFF0000),];
    Gradient gradient = SweepGradient(
      startAngle: 0,
      endAngle: 2 * pi,
      colors: colorList.map((e) => e).toList(),
    );
    _paint4..maskFilter = MaskFilter.blur(BlurStyle.outer, 30);
    canvas.drawArc(Rect.fromLTWH(0, 0, radius * 2, radius * 2), -pi / 2, pi * 2, false, _paint4);
    var rect = Rect.fromLTWH(0, 0, radius * 2, radius * 2);
    _paint.shader = gradient.createShader(rect);
    canvas.drawCircle(center, radius, _paint);
    var rect2 = Rect.fromLTWH(radius/2, radius/2, radius, radius);
    _paint2.color = Color.fromRGBO(255, 225, 178, 1.0);
    canvas.drawArc(rect2, -pi/2, pi, true, _paint2);
    _paint2.color = Color.fromRGBO(220, 244, 255, 1.0);
    canvas.drawArc(rect2, pi/2, pi, true, _paint2);
    canvas.drawCircle(center, radius/2, _paint3);
    _paint3.color = Colors.grey;
    canvas.drawLine(Offset(radius,radius/2), Offset(radius,radius+radius/2), _paint3);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

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(
          // width: 50.0,
          child: Icon(
            Icons.circle,
            color: _color == null ? Colors.white : _color,
            size: _Thumbsize,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(300),
            boxShadow: [
              BoxShadow(
                blurRadius: 0.1, //阴影范围
                spreadRadius: 0.001, //阴影浓度
                color: Colors.white, //阴影颜色
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

(0)

相关推荐

  • Flutter实现矩形取色器的封装

    前言 最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器 提示:以下是本篇文章正文内容,下面案例可供参考 一.BarTypeColorPicker 条形选色板的功能实现,颜色的获取是通过位置来判断,然后赋予相应的颜色.这个封装好的组件可通过colorListener.onTapUpListener的回调方法,把颜色传出去. 代码如下(示例): import 'package:flutter/cupertino.dart'; import 'package:f

  • Flutter自定义圆盘取色器

    本文实例为大家分享了Flutter自定义圆盘取色器的具体代码,供大家参考,具体内容如下 下面展示一些 内联代码片. 圆盘取色器 效果图 完整代码 import 'dart:math'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; class CircularColorFinder extends Sta

  • VUE 自定义取色器组件的实现

    本文主要介绍了VUE 自定义取色器组件的实现,分享给大家,具体如下: 效果: 功能: 1.点击色块中任何一点,色块中的正方形显示该点的颜色. 2.点击色块上方的颜色圆点,色块选中该颜色所在的位置,并在正方形中显示相应颜色. 3.上方圆点如果不是rgb格式,需要转换. 4.组件的大小从调用它的页面中传过去. 组件代码(CanvasColor2.vue): <template> <!--    <div>-->     <div id='outDiv' style=&

  • PHP模板引擎Smarty自定义变量调解器用法

    本文实例讲述了PHP模板引擎Smarty自定义变量调解器用法.分享给大家供大家参考,具体如下: 在 PHP 中,有很多处理文本的函数,您可以把要处理的文本通过函数处理之后,再调用 Smarty 模板引擎中的 assign() 赋值给变量,分配到模板中进行显示. Smarty 中的变量调解器和 PHP 中处理文本的函数相似,不过语法不相同,在 Smarty 中,是通过 "|" 后面直接跟调解器函数名,如果有参数,得加在 ":" 后面,多个参数的话,累加即可. 格式:{

  • javascript编程开发中取色器及封装$函数用法示例

    本文实例讲述了javascript编程开发中取色器及封装$函数用法.分享给大家供大家参考,具体如下: 1.封装$函数 function $(str){ //如果传入的是'#' 则选择id标签 //如果传入的是'.' 则选择所有的类名标签 //如果传入的既不是'#也不是'.' 选择复合标签 //判断传入的值 if(typeof str !='string'){ console.log('传入的参数有误!'); return null; } //获取参数的第一个字母 var firstChar=st

  • JS实现的RGB网页颜色在线取色器完整实例

    本文实例讲述了JS实现的RGB网页颜色在线取色器.分享给大家供大家参考,具体如下: 运行效果图如下: 完整实例代码如下: <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <meta name="description" content="在线取色器"> <me

  • 在C#使用字典存储事件示例及实现自定义事件访问器

    使用字典存储事件实例 accessor-declarations 的一种用法是公开很多事件但不为每个事件分配字段,而是使用字典来存储这些事件实例.这只在具有很多事件但您预计大多数事件都不会实现时才有用. public delegate void EventHandler1(int i); public delegate void EventHandler2(string s); public class PropertyEventsSample { private System.Collecti

  • Yii2实现自定义独立验证器的方法

    本文实例讲述了Yii2实现自定义独立验证器的方法.分享给大家供大家参考,具体如下: 新建一个文件: <?php /** * author : forecho <caizhenghai@gmail.com> * createTime : 2015/7/1 14:54 * description: */ namespace common\helps; use yii\validators\Validator; class ArrayValidator extends Validator {

  • spring security自定义决策管理器

    首先介绍下Spring的决策管理器,其接口为AccessDecisionManager,抽象类为AbstractAccessDecisionManager.而我们要自定义决策管理器的话一般是继承抽象类而不去直接实现接口. 在Spring中引入了投票器(AccessDecisionVoter)的概念,有无权限访问的最终觉得权是由投票器来决定的,最常见的投票器为RoleVoter,在RoleVoter中定义了权限的前缀,先看下Spring在RoleVoter中是怎么处理授权的. public int

  • Django框架自定义模型管理器与元选项用法分析

    本文实例讲述了Django框架自定义模型管理器与元选项用法.分享给大家供大家参考,具体如下: 自定义模型管理器 每个模型类默认都有一个 objects 类属性,可以把它叫 模型管理器.它由django自动生成,类型为 django.db.models.manager.Manager 可以在模型类中自定义模型管理器,自定义后, Django将不再生成默认的 objects.(模型类可以自定义多个管理器) 例如: class Department(models.Model): # 自定义模型管理器

随机推荐