这可以通过将 CustomPainter 设置为 foregroundPainter 的 CustomPaint 小部件来完成:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CustomPaint spotlight',
home: Scaffold(
appBar: AppBar(
title: Text('CustomPaint spotlight'),
),
body: Center(
child: CustomPaint(
foregroundPainter: BorderPainter(),
child: Container(
width: 200,
height: 100,
color: Colors.deepOrange[50],
),
),
),
),
);
}
}
class BorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1; // desirable value for corners side
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 1.5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(BorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(BorderPainter oldDelegate) => false;
}
这会产生如下结果:
您需要知道 CustomPaint 不会裁剪其子角,并且在某些情况下可能会令人沮丧。你有两种方法可以解决这个问题:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CustomPaint spotlight',
home: Scaffold(
appBar: AppBar(
title: Text('CustomPaint spotlight'),
),
body: Center(
child: CustomPaint(
foregroundPainter: BorderPainter(),
child: ClipPath(
clipper: BorderClipper(),
child: Container(
width: 200,
height: 100,
color: Colors.deepOrange[50],
),
),
),
),
),
);
}
}
class BorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1; // desirable value for corners side
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 1.5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(BorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(BorderPainter oldDelegate) => false;
}
class BorderClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}