Flutter自定义绘制Widget初探
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('自定义绘制Widget'),
),
body: Center(
child: CustomPaint(
size: Size(200, 200),
painter: MyPainter(),
),
),
);
}
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
canvas.drawCircle(Offset(100, 100), 50, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
这段代码创建了一个简单的自定义绘制Widget,它在一个200x200像素的圆形中心绘制了一个蓝色的圆。这个例子展示了如何使用CustomPaint
Widget和CustomPainter
类来实现自定义绘制逻辑。
评论已关闭