英文:
Custom paint with flutter
问题
我有一个任务设计,这个图像在Figma文件中。
我想自定义绘制与相同的图像,但我不知道如何自定义绘制,因为我没有理想。请帮助我,我想变得更好,完成分配的工作。
谢谢你的帮助。
英文:
I have a task design, this image in figma file.
I want to custom paint as the same image, but I don't an ideal because I don't know custom paint. Please help me, I want to become better and I want to complete the assigned work.
Thank you for your help.
答案1
得分: 2
以下是您要翻译的代码部分:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final textEditingController = TextEditingController();
final key = GlobalKey<State<TextField>>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: SizedBox.square(
dimension: 400,
child: CustomPaint(
painter: CustomObjectPainter(
const LinearGradient(
colors: [Color(0xFF9DCF45), Color(0xFF729930)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
),
),
),
);
}
}
class CustomObjectPainter extends CustomPainter {
final Gradient gradient;
CustomObjectPainter(this gradient);
@override
void paint(Canvas canvas, Size size) {
canvas.save();
final radius = size.width * 0.1;
final paint = Paint();
final rect = Rect.fromPoints(size.topLeft(Offset.zero),
size.bottomRight(Offset(-size.width * 0.1, 0)));
paint.shader = gradient.createShader(rect);
final rrect = RRect.fromRectAndCorners(rect,
bottomLeft: Radius.circular(radius),
bottomRight: Radius.circular(radius));
canvas.drawRRect(rrect, paint);
final width = size.width * 0.2;
final height = size.height * 0.2;
final topMargin = size.height * 0.1;
final smallRect = Rect.fromCenter(
center: Offset(size.width * 0.9, topMargin + height / 2),
width: width,
height: height);
final smallRRect =
RRect.fromRectAndRadius(smallRect, Radius.circular(radius / 4));
canvas.drawRRect(smallRRect, paint);
paint.style = PaintingStyle.stroke;
paint.shader = null;
paint.color = Colors.white;
paint.strokeWidth = 5;
canvas.drawRRect(smallRRect, paint);
canvas.restore();
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return !(oldDelegate is CustomObjectPainter &&
oldDelegate.gradient == gradient);
}
}
英文:
You can use CustomPainter in following way
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final textEditingController = TextEditingController();
final key = GlobalKey<State<TextField>>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: SizedBox.square(
dimension: 400,
child: CustomPaint(
painter: CustomObjectPainter(
const LinearGradient(
colors: [Color(0xFF9DCF45), Color(0xFF729930)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
),
),
),
);
}
}
class CustomObjectPainter extends CustomPainter {
final Gradient gradient;
CustomObjectPainter(this.gradient);
@override
void paint(Canvas canvas, Size size) {
canvas.save();
final radius = size.width * 0.1;
final paint = Paint();
final rect = Rect.fromPoints(size.topLeft(Offset.zero),
size.bottomRight(Offset(-size.width * 0.1, 0)));
paint.shader = gradient.createShader(rect);
final rrect = RRect.fromRectAndCorners(rect,
bottomLeft: Radius.circular(radius),
bottomRight: Radius.circular(radius));
canvas.drawRRect(rrect, paint);
final width = size.width * 0.2;
final height = size.height * 0.2;
final topMargin = size.height * 0.1;
final smallRect = Rect.fromCenter(
center: Offset(size.width * 0.9, topMargin + height / 2),
width: width,
height: height);
final smallRRect =
RRect.fromRectAndRadius(smallRect, Radius.circular(radius / 4));
canvas.drawRRect(smallRRect, paint);
paint.style = PaintingStyle.stroke;
paint.shader = null;
paint.color = Colors.white;
paint.strokeWidth = 5;
canvas.drawRRect(smallRRect, paint);
canvas.restore();
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return !(oldDelegate is CustomObjectPainter &&
oldDelegate.gradient == gradient);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论