indent_widget.dart
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'package:flutter/material.dart';
/// A custom widget that adds an indent to the left or right of its child.
///
/// The [BlockQuoteWidget] widget is used to create a visual indent in the UI.
/// It takes a [child] parameter which is the content of the widget,
/// a [direction] parameter which specifies the direction of the indent,
/// and a [color] parameter to set the color of the indent.
class BlockQuoteWidget extends StatelessWidget {
const BlockQuoteWidget({
super.key,
required this.child,
required this.direction,
required this.color,
this.width = 3,
});
/// The child widget to be indented.
final Widget child;
/// The direction of the indent.
final TextDirection direction;
/// The color of the indent.
final Color color;
/// The width of the indent.
final double width;
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: CustomPaint(
foregroundPainter: BlockQuotePainter(color, direction, width),
child: child,
),
),
],
);
}
}
/// A custom painter that draws an indent on a canvas.
///
/// The [BlockQuotePainter] class extends CustomPainter and is responsible for
/// painting the indent on a canvas. It takes a [color] and [direction] parameter
/// and uses them to draw an indent in the UI.
class BlockQuotePainter extends CustomPainter {
BlockQuotePainter(this.color, this.direction, this.width);
final Color color;
final TextDirection direction;
final double width;
@override
void paint(Canvas canvas, Size size) {
var left = direction == TextDirection.ltr;
var start = left ? 0.0 : size.width - width;
var rect = Rect.fromLTWH(start, 0, width, size.height);
var paint = Paint()..color = color;
canvas.drawRect(rect, paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}