Skip to content

Instantly share code, notes, and snippets.

@luigi-rosso
Last active November 3, 2020 01:59
Show Gist options
  • Save luigi-rosso/4629c3422d8938d7ce0cf7cfcb01547c to your computer and use it in GitHub Desktop.
Save luigi-rosso/4629c3422d8938d7ce0cf7cfcb01547c to your computer and use it in GitHub Desktop.
Disappearing Box
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'The magically disappearing box...'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: [
ListView.builder(
itemCount: 1000,
itemBuilder: (context, idx) {
return ListTile(
title: Text(idx.toString()),
);
},
),
Center(
child: SizedBox(
width: 256,
height: 256,
child: FlatButton(
child: Houdini(),
onPressed: () {
//intentionally blank, showing how item appears on hover
},
),
),
),
],
),
);
}
}
// A simple LeafRenderObjectWidget that draws a circle.
class Houdini extends LeafRenderObjectWidget {
const Houdini();
@override
RenderObject createRenderObject(BuildContext context) {
return _HoudiniRenderObject();
}
@override
void updateRenderObject(
BuildContext context, covariant _HoudiniRenderObject renderObject) {
;
}
@override
void didUnmountRenderObject(covariant _HoudiniRenderObject renderObject) {}
}
class _HoudiniRenderObject extends RenderBox {
@override
void attach(PipelineOwner owner) {
super.attach(owner);
markNeedsPaint();
}
@override
bool hitTestSelf(Offset screenOffset) => false;
@override
void performResize() {
size = constraints.biggest;
}
@override
bool get sizedByParent => true;
@override
void paint(PaintingContext context, Offset offset) {
var canvas = context.canvas;
canvas.save();
bool showDisappearBug = true;
if (showDisappearBug) {
// This path shows the bug
var rect = Rect.fromLTWH(100, 100, 512, 512);
var scale = 0.25;
canvas.translate(offset.dx, offset.dy);
canvas.translate(size.width / 2 - rect.width * scale / 2,
size.height / 2 - rect.height * scale / 2);
canvas.scale(scale);
canvas.translate(-rect.left, -rect.top);
canvas.drawRect(
rect,
Paint()
..color = Colors.red
..blendMode = BlendMode.srcIn,
);
canvas.restore();
} else {
// This version with no custom transform works.
var rect = offset & size;
canvas.drawRect(
rect,
Paint()
..color = Colors.red
..blendMode = BlendMode.srcIn,
);
canvas.restore();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment