首页 > 其他分享 >一统天下 flutter - widget 自定义: 通过 CustomPaint 实现自定义组件

一统天下 flutter - widget 自定义: 通过 CustomPaint 实现自定义组件

时间:2023-05-06 14:56:59浏览次数:50  
标签:CustomPaint widget 自定义 borderWidth child override

源码 https://github.com/webabcd/flutter_demo
作者 webabcd

一统天下 flutter - widget 自定义: 通过 CustomPaint 实现自定义组件

示例如下:

lib\widget\custom\custom_paint.dart

/*
 * 通过 CustomPaint 实现自定义组件
 *
 * CustomPaint 继承自 SingleChildRenderObjectWidget
 * 关于 SingleChildRenderObjectWidget 的说明请参见 single_child_render_object_widget.dart
 *
 * CustomPaint 通过 CustomPainter 实现自定义的绘制逻辑,关于 CustomPainter 的说明请参见 /lib/widget/shape/paint.dart
 */

import 'package:flutter/material.dart';

import '../../helper.dart';

class CustomPaintDemo extends StatefulWidget {
  const CustomPaintDemo({Key? key}) : super(key: key);

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

class _CustomPaintDemoState extends State<CustomPaintDemo> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("title"),),
      backgroundColor: Colors.orange,
      body: const Center(
        /// 使用自定义组件
        child: BorderWidget(
          child: MyText("我是 CustomPaint 的 child"),
          borderWidth: 2,
        ),
      ),
    );
  }
}

/// 继承 CustomPaint 实现一个自带边框效果的自定义组件
/// 先绘制 painter,然后在 painter 的前面渲染 child, 然后在 child 的前面绘制 foregroundPainter
class BorderWidget extends CustomPaint {
  final double borderWidth;
  const BorderWidget({super.key, super.child, required this.borderWidth});

  /// 如果不指定 child 则 CustomPaint 使用 size 作为自己的尺寸
  /// 如果指定了 child 则 CustomPaint 使用 child 的尺寸作为自己的尺寸
  @override
  Size get size => Size.zero;

  @override
  CustomPainter? get painter => MyCustomPainter(borderWidth: borderWidth);

  @override
  CustomPainter? get foregroundPainter => null;
}

/// CustomPainter - 自定义绘制逻辑
/// 原点在左上角,右为 x 轴正方向,下为 y 轴正方向,顺时针为旋转正方向
/// 弧度是弧的长度与弧的半径的比值,所以弧度 π 就是 180 度
class MyCustomPainter extends CustomPainter {
  double borderWidth;
  MyCustomPainter({required this.borderWidth});

  /// 自定义绘制
  ///   canvas - 画布,所有 canvas 的操作都是调用的 native 方法
  ///   size - 绘制区域(即 CustomPaint 的尺寸,参见上面 CustomPaint 的 size 的说明)
  @override
  void paint(Canvas canvas, Size size) {
    /// 创建一个画笔
    final paint = Paint()..color = Colors.white..strokeWidth = borderWidth..style = PaintingStyle.stroke;
    /// 通过 Offset & Size 生成一个 Rect
    final rect = Offset.zero & size;
    /// 绘制一个边框
    canvas.drawRect(rect, paint);
  }

  /// 如果之后 widget 重新 build 了,就会执行到这里
  /// 一般通过判断 MyCustomPainter 新旧实例的与 UI 相关的参数是否发生变化来决定是否需要重绘
  @override
  bool shouldRepaint(MyCustomPainter oldDelegate) {
    /// 新旧 MyCustomPainter 实例的 borderWidth 不同时,则重绘
    return borderWidth != oldDelegate.borderWidth;
  }
}

源码 https://github.com/webabcd/flutter_demo
作者 webabcd

标签:CustomPaint,widget,自定义,borderWidth,child,override
From: https://www.cnblogs.com/webabcd/p/flutter_lib_widget_custom_custom_paint.html

相关文章

  • 一统天下 flutter - widget 自定义: 通过组合多个 Widget 的方式实现自定义组件
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget自定义:通过组合多个Widget的方式实现自定义组件示例如下:lib\widget\custom\custom_widget.dart/**通过组合多个Widget的方式实现自定义组件*/import'dart:math';impo......
  • Material Design UI Widgets
     AndroidL开发者预览支持库提供两个新的Widgets,RecyclerView和CardView。使用这两个Widgets可以显示复杂的Listview和卡片布局,这两个Widgets默认使用Materialdesign。RecyclerView  RecyclerView是一个更高级柔性版本的Listview,RecyclerView是一个能包含很多视图的容器,它能......
  • 【Apache POI】Word文档转换HTML,多级列表自定义处理
    本文使用poi和xdocreport组件,在其基础自定义实现某些功能最近有个需求,文档的转换,需要把Word文档转换为编辑器可识别支持的HTML格式类型,Apache的开源组件poi可以解析docx和doc类型的文档,于是使用该组件实现需求关于Word文档的俩种格式,docx格式是一种压缩文件,由xml格......
  • D365: 实体自定义暂存表到目标表
    当提供的模板文件不能满足D365数据表的数据结构或者模板的数据不能直接通过DMF导入到实际的业务表时,我们按照模板自定义实体后,可以实现如下方法来将暂存表的数据通过一定的逻辑处理,将数据转换到我们的业务表中,sample代码如下publicstaticcontainercopyCustomStagingToTarget(......
  • 自定义mybatis插件之全局数据过滤
    目录一、介绍二、实现三、效果四、源码一、介绍通过开发mybatis的插件来实现对全局的sql查询语句进行拦截,并新增全局的过滤条件做到无感知的数据过滤,比如全局过滤某个租户的数据。二、实现实现思路1、通过mybatis的拦截器拦截所有查询的sql2、使用Druid里面的工具类解析sq......
  • 微信小程序-根据同声传译插件创建语音转文字的自定义插件
    使用了vantweapp组件.js//page/common/components/voice/voice.jsimportToastfrom'../../../../vant-weapp/dist/toast/toast';//引入插件:微信同声传译varplugin=requirePlugin("WechatVoice");//获取全局唯一的语音识别管理器recordRecoManagerletmanager=pl......
  • CesiumWidget.js的作用
    顾名思义,CesiumWidget就是cesium小部件的意思。但是,cesium包含哪些小部件?这些小部件又有哪些意义?这些小部件是不可或缺的吗?看《Cesium原理篇:1最长的一帧之渲染调度》讲,好像cesium的启动就是由widget来触发的?CesiumWidget.jsstartRenderLoop函数需要传入一个widget参数。这个w......
  • .net maui blazor创建存储自定义目录文件
    stringdir="/storage/emulated/0/Android/data/com.example.myapp/data";if(!Directory.Exists(dir)){Directory.CreateDirectory(dir);}stringpath=Path.Combine(dir,"a.txt");File.WriteAllText(path,"abc");//com.example.myapp......
  • 【剪裁 widget】Flutter ClipOval 与 Flutter ClipRRect
    本文是【剪裁widget】系列的第三篇,也是最后一篇,今天介绍一下ClipOval和ClipRRect。ClipOval介绍FlutterClipOval用椭圆形去剪裁child,path以外的部分不显示,还能高效的实现动画。剪裁是在绘制阶段,具体实现是在paint方法中调用PaintingContext类的pushClipPath方法进......
  • djangoadmin后台搜索结果筛选自定义模版
    django-admin对搜索结果进行自定义统计,可参考代码如下:defchangelist_view(self,request,extra_context=None):#cur1_time=datetime.now()data_dict={}value=request.GET.get('q',"")bill_cycle=request.GET.get('bi......