So I have this stack:
Stack(
children:[
Widget1(),
Widget2(),
Widget3(),
]);
they’re all full screen so you can only see Widget3
But I want to ‘see through’ these widgets to the one beneath at a certain area. Like maybe a circle of radius 100 in the center of the page or something: have a transparent area of the app.
And I’d love it if I could add in like a widget in the stack to accomplish this instead of wrapping the Widget, though that would work too:
Stack(
children:[
Widget1(),
Widget2(),
TransparentCenterArea(radius:50), // see through Widget2, to see Widget1
Widget3(),
TransparentCenterArea(radius:100), // see through Widget3, to see Widget2
]);
is something like this even possible? I can’t figure out a way to do it. Especially since I might want to change the radius, so as to have like an ‘opening’ animation or something…
Now, as a contrived example I have this, trying to use a CustomPainter but it really doesn’t work, I only see Widget3
import 'package:flutter/material.dart';
class TransparentCenterArea extends StatelessWidget {
final double radius;
TransparentCenterArea({required this.radius});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(radius),
child: CustomPaint(
size: Size.fromRadius(radius),
painter: TransparentPainter(),
),
);
}
}
class TransparentPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// Draw a transparent circle in the center of the widget
final paint = Paint()..color = Colors.transparent;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), size.width / 2, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Transparent Center Area Example'),
),
body: Stack(
children: [
Widget1(),
Widget2(),
TransparentCenterArea(radius: 50), // see through Widget2, to see Widget1
Widget3(),
TransparentCenterArea(radius: 100), // see through Widget3, to see Widget2
],
),
),
));
}
class Widget1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Center(child: Text('Widget 1')),
);
}
}
class Widget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: Center(child: Text('Widget 2')),
);
}
}
class Widget3 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.orange,
child: Center(child: Text('Widget 3')),
);
}
}
2
Answers
@RandalSchwartz had it right in his comment. I was able to come up with this from his advice:
In Flutter, constraints goes down, size goes up and parent position the child. Now for your scenario, Every colored widget retuning container on Stack. and the container taking full space of the stack. You can provide a fixed height on each Container or use Positioned widget for this. Also while adding child, it is necessary wrap with any positioned widget like
Positioned
,Align
widget for your case.First make sure to add a color on paint
Here is the demo
Widget will be paint from top to bottom, where
TransparentCenterArea(radius: 100),
will be render overWidget3
and so on.Find more about using Stack widget.