In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use CustomPaint in Flutter, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Brief introduction
In some scenarios, we will need to draw some highly customized components, such as the UI designer gave us a problem-to create a grotesque border. For the sake of UI designer is a beautiful little sister, and embarrassed to say that this can not be done (that is also very humiliating). At this point, we can not directly use the components that come with Flutter, but need to draw the components manually, then we will need to use the CuntomPaint components. The CustomPaint component is similar to the front-end Canvas, allowing us to draw a variety of elements on a canvas, including points, lines, rectangles, arcs, text, pictures, and so on.
CustomPaint introduction
CustomPaint is a Widget with three important parameters:
CustomPaint (child: childWidget (), foregroundPainter: foregroundPainter (), painter: backgroundPainter (),)
Subcomponents of child:CustomPaint
Painter and foregroundPainter: both are CustomPainter classes that define what is drawn by canvas. The difference is that the first step is to execute the drawing instructions of painter. The child subcomponent is then rendered on the background. Finally, the contents of the foregroundPainter are drawn one layer above the child.
CustomPainter provides a paint drawing method for us to draw graphics, which takes two parameters, canvas and size, where canvas is the canvas and size is the canvas size. Canvas provides many ways to draw graphics, such as paths, rectangles, circles, lines, and so on.
CustomPainter example
With the above basic concepts in mind, let's give a simple example to demonstrate the hierarchical relationship of the three parameters. We draw a blue square on backgroundPainter, then pass in a prototype image component as child, and finally draw a translucent red circle on foregroundPainter to cover part of the image. The final effect is shown in the following figure, so that we can have a good understanding of the relationship between the three elements.
The complete code is as follows:
Import 'dart:ui';import' package:flutter/material.dart';class BasicPaintPage extends StatelessWidget {const BasicPaintPage ({Key? Key}): super (key: key) @ override Widget build (BuildContext context) {return CustomPaint (child: Center (child: ClipOval (child: Image.asset ('images/beauty.jpeg', width: 200, height: 200, fit: BoxFit.fitWidth,), painter: BackgroundPainter (), foregroundPainter: ForegroundPainter (),) }} class BackgroundPainter extends CustomPainter {@ override void paint (Canvas canvas, Size size) {canvas.drawColor (Color (0xFFF1F1F1), BlendMode.color); var center = size / 2; var paint = Paint ().. color = Color (0xFF2080E5); paint.strokeWidth = 2.0; canvas.drawRect (Rect.fromLTWH (center.width-120,120,240,240), paint,);} @ override bool shouldRepaint (covariant CustomPainter oldDelegate) {return false }} class ForegroundPainter extends CustomPainter {@ override void paint (Canvas canvas, Size size) {var center = size / 2; var paint = Paint ().. color = Color (0x80F53010); paint.strokeWidth = 2.0; canvas.drawCircle (Offset (center.width, center.height), 100, paint,);} @ override bool shouldRepaint (covariant CustomPainter oldDelegate) {return false;}}
In the background drawing, we set the background color of canvas, that is, the following line of code:
Canvas.drawColor (Color (0xFFF1F1F1), BlendMode.color)
The other code is relatively easy to understand. BackgroundPainter draws a blue square, child is a central circle, and ForegroundPainter draws a translucent red circle in the middle, which is the same size as a circle. So we got the effect we wanted.
Thank you for reading this article carefully. I hope the article "how to use CustomPaint in Flutter" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un