Skip to content

Instantly share code, notes, and snippets.

@tsherdiwala
Last active May 22, 2023 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsherdiwala/fc7230f6eb6a1933bd721dc2fb5d27c3 to your computer and use it in GitHub Desktop.
Save tsherdiwala/fc7230f6eb6a1933bd721dc2fb5d27c3 to your computer and use it in GitHub Desktop.
custom graph overlay
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
void main() => runApp(StartPage());
class StartPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: CustomPaint(
painter: PaintDemo(),
size: Size.infinite,
),
)
/**/
),
);
}
}
class PaintDemo extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// axis x
canvas.drawLine(
Offset(0, size.height / 2),
Offset(size.width, size.height / 2),
Paint()
..color = Colors.black
..strokeWidth = 1);
// axis y
canvas.drawLine(
Offset(size.width / 2, 0),
Offset(size.width / 2, size.height),
Paint()
..color = Colors.black
..strokeWidth = 1);
// top color
canvas.saveLayer(
Rect.fromLTRB(0, 0, size.width, size.height / 2),
Paint()
..colorFilter = ColorFilter.mode(Colors.blue, BlendMode.srcATop)
..color = Colors.blue
..blendMode = BlendMode.srcATop,
);
_drawGraph(canvas, size);
canvas.restore();
// bottom color
canvas.saveLayer(
Rect.fromLTRB(0, size.height / 2, size.width, size.height),
Paint()
..colorFilter = ColorFilter.mode(Colors.yellow, BlendMode.srcATop)
..color = Colors.yellow
..blendMode = BlendMode.srcATop,
);
_drawGraph(canvas, size);
canvas.restore();
}
void _drawGraph(Canvas canvas, Size size) {
canvas.drawLine(
Offset(0, 0),
Offset(size.width, size.height),
Paint()
..isAntiAlias = true
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 3);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment