Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 1da4988

Browse files
authored
Merge pull request #242 from IIzzaya/iizzaya
Add Widget placeholder
2 parents 47288d6 + d2facd2 commit 1da4988

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Runtime/widgets/placeholder.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections.Generic;
2+
using Unity.UIWidgets.foundation;
3+
using Unity.UIWidgets.ui;
4+
5+
namespace Unity.UIWidgets.widgets
6+
{
7+
internal class _Placeholderpainter : AbstractCustomPainter
8+
{
9+
public _Placeholderpainter(
10+
Color color,
11+
float strokeWidth = 0f
12+
)
13+
{
14+
this.color = color;
15+
this.strokeWidth = strokeWidth;
16+
}
17+
18+
19+
public readonly Color color;
20+
public readonly float strokeWidth;
21+
22+
public override void paint(Canvas canvas, Size size)
23+
{
24+
Paint paint = new Paint();
25+
paint.color = color;
26+
paint.style = PaintingStyle.stroke;
27+
paint.strokeWidth = strokeWidth;
28+
29+
Rect rect = Offset.zero & size;
30+
Path path = new Path();
31+
path.addRect(rect);
32+
path.addPolygon(new List<Offset> {rect.topRight, rect.bottomLeft}, false);
33+
path.addPolygon(new List<Offset> {rect.topLeft, rect.bottomRight}, false);
34+
35+
canvas.drawPath(path, paint);
36+
return;
37+
}
38+
39+
public override bool shouldRepaint(CustomPainter oldPainter)
40+
{
41+
return ((_Placeholderpainter) oldPainter).color != color ||
42+
((_Placeholderpainter) oldPainter).strokeWidth != strokeWidth;
43+
}
44+
45+
public override bool? hitTest(Offset position)
46+
{
47+
return false;
48+
}
49+
}
50+
51+
public class Placeholder : StatelessWidget
52+
{
53+
public Placeholder(
54+
Key key = null,
55+
Color color = null,
56+
float strokeWidth = 2.0f,
57+
float fallbackWidth = 400.0f,
58+
float fallbackHeight = 400.0f
59+
) : base(key)
60+
{
61+
this.color = color ?? new Color(0xFF455A64);
62+
this.strokeWidth = strokeWidth;
63+
this.fallbackWidth = fallbackWidth;
64+
this.fallbackHeight = fallbackHeight;
65+
}
66+
67+
public readonly Color color;
68+
public readonly float strokeWidth;
69+
public readonly float fallbackWidth;
70+
public readonly float fallbackHeight;
71+
72+
public override Widget build(BuildContext context)
73+
{
74+
return new LimitedBox(
75+
maxWidth: fallbackWidth,
76+
maxHeight: fallbackHeight,
77+
child: new CustomPaint(
78+
size: Size.infinite,
79+
foregroundPainter: new _Placeholderpainter(
80+
color: color,
81+
strokeWidth: strokeWidth
82+
)
83+
)
84+
);
85+
}
86+
}
87+
}

Runtime/widgets/placeholder.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)