Draw background

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-08-30 16:20:34 +02:00
parent 6c1b7fac76
commit 889a3e5c34
+79 -40
View File
@@ -1,5 +1,4 @@
import "package:flutter/material.dart";
import "package:flutter/rendering.dart";
void main() {
runApp(const MainApp());
@@ -8,59 +7,99 @@ void main() {
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return const Background();
}
}
class Background extends StatelessWidget {
const new({
super.key,
});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) => CustomPaint(
size: constraints.biggest,
painter: Sky(),
builder: (context, constraints) => Stack(
textDirection: TextDirection.ltr,
children: [
CustomPaint(
size: constraints.biggest,
painter: Ceiling(),
),
CustomPaint(
size: constraints.biggest,
painter: Floor(),
),
],
),
);
}
}
class Sky extends CustomPainter {
class Ceiling extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Rect rect = Offset.zero & size;
const RadialGradient gradient = RadialGradient(
center: Alignment(0.7, -0.6),
radius: 0.2,
colors: <Color>[Color(0xFFFFFF00), Color(0xFF0099FF)],
stops: <double>[0.4, 1.0],
final double halfHeight = rect.height / 2;
final Rect topHalf = Rect.fromLTWH(
rect.left,
rect.top,
rect.width,
halfHeight,
);
const LinearGradient gradient = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(32, 32, 32, 1.0),
Color.fromRGBO(96, 96, 96, 1.0),
],
stops: [0.0, 1.0],
);
canvas.drawRect(
topHalf,
Paint()..shader = gradient.createShader(topHalf),
);
canvas.drawRect(rect, Paint()..shader = gradient.createShader(rect));
}
@override
SemanticsBuilderCallback get semanticsBuilder {
return (Size size) {
// Annotate a rectangle containing the picture of the sun
// with the label "Sun". When text to speech feature is enabled on the
// device, a user will be able to locate the sun on this picture by
// touch.
Rect rect = Offset.zero & size;
final double width = size.shortestSide * 0.4;
rect = const Alignment(0.8, -0.9).inscribe(Size(width, width), rect);
return <CustomPainterSemantics>[
CustomPainterSemantics(
rect: rect,
properties: const SemanticsProperties(
label: "Sun",
textDirection: TextDirection.ltr,
),
),
];
};
}
// Since this Sky painter has no fields, it always paints
// the same thing and semantics information is the same.
// Therefore we return false here. If we had fields (set
// from the constructor) then we would return true if any
// of them differed from the same fields on the oldDelegate.
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRepaint(Sky oldDelegate) => false;
@override
bool shouldRebuildSemantics(Sky oldDelegate) => false;
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}
class Floor extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Rect rect = Offset.zero & size;
final double halfHeight = rect.height / 2;
final Rect bottomHalf = Rect.fromLTWH(
rect.left,
halfHeight,
rect.width,
rect.bottom,
);
const LinearGradient gradient = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(32, 32, 0, 1.0),
Color.fromRGBO(96, 96, 0, 1.0),
],
stops: [0.0, 1.0],
);
canvas.drawRect(
bottomHalf,
Paint()..shader = gradient.createShader(bottomHalf),
);
}
@override
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}