Skip to content

Instantly share code, notes, and snippets.

@themisir
Last active November 13, 2021 18:54
Show Gist options
  • Save themisir/b3a1781292d0d99a47c167311fad5381 to your computer and use it in GitHub Desktop.
Save themisir/b3a1781292d0d99a47c167311fad5381 to your computer and use it in GitHub Desktop.
Add dark mode to your flutter apps within seconds

Darkest

Place Darkest widget above widgets (or pages) you want to enable dark mode for. And it magically makes your app use state of art technology powered by latest Machine Learning trends to convert graphics into dark mode compatible style.

Darkest(
  enabled: true,
  child: MyWidget(),
),
import 'dart:math';
import 'package:flutter/widgets.dart';
class Darkest extends StatelessWidget {
const Darkest({
required this.enabled,
required this.child,
Key? key,
}) : super(key: key);
final bool enabled;
final Widget child;
@override
Widget build(BuildContext context) {
return enabled ? DarkestCanvas(child: child) : child;
}
}
class DarkestCanvas extends StatefulWidget {
const DarkestCanvas({Key? key, required this.child}) : super(key: key);
final Widget child;
@override
_DarkestCanvasState createState() => _DarkestCanvasState();
}
class _DarkestCanvasState extends State<DarkestCanvas> {
Offset? spotPoint;
@override
Widget build(BuildContext context) {
return Stack(
children: [
widget.child,
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onPanUpdate: (details) {
setState(() {
spotPoint = details.localPosition;
});
},
child: MouseRegion(
opaque: false,
onHover: (event) {
setState(() {
spotPoint = event.localPosition;
});
},
child: ClipPath(
clipper: InvertedOvalClip(spotPoint),
child: Container(color: const Color(0xFF000000)),
),
),
),
),
],
);
}
}
class InvertedOvalClip extends CustomClipper<Path> {
const InvertedOvalClip(this.spotPoint);
final Offset? spotPoint;
@override
Path getClip(Size size) {
if (spotPoint == null) {
return Path()..addRect(Rect.fromLTWH(0, 0, size.width, size.height));
}
final radius = min(size.width, size.height) * .3;
return Path()
..addOval(Rect.fromCircle(center: spotPoint!, radius: radius))
..addRect(Rect.fromLTWH(0, 0, size.width, size.height))
..fillType = PathFillType.evenOdd;
}
@override
bool shouldReclip(covariant InvertedOvalClip oldClipper) {
return oldClipper.spotPoint != spotPoint;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment