| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) => MaterialApp(
- home: Stack(
- fit: StackFit.expand,
- children: [
- GestureDetector(
- onDoubleTap: () => print("double red"),
- child: Container(color: Colors.red),
- ),
- TransparentPointer(
- transparent: true,
- child: Stack(
- children: [
- Positioned(
- top: 100,
- left: 100,
- right: 100,
- bottom: 100,
- child: GestureDetector(
- onTap: () => print("green"),
- child: Container(color: Colors.green),
- ),
- )
- ],
- ),
- ),
- ],
- ),
- );
- }
- class TransparentPointer extends SingleChildRenderObjectWidget {
- const TransparentPointer({
- super. key,
- this.transparent = true,
- super.child,
- }) : assert(transparent != null);
- final bool transparent;
- @override
- RenderTransparentPointer createRenderObject(BuildContext context) {
- return RenderTransparentPointer(
- transparent: transparent,
- );
- }
- @override
- void updateRenderObject(BuildContext context, RenderTransparentPointer renderObject) {
- renderObject
- ..transparent = transparent;
- }
- @override
- void debugFillProperties(DiagnosticPropertiesBuilder properties) {
- super.debugFillProperties(properties);
- properties.add(DiagnosticsProperty<bool>('transparent', transparent));
- }
- }
- class RenderTransparentPointer extends RenderProxyBox {
- RenderTransparentPointer({
- RenderBox? child,
- bool transparent = true,
- }) : _transparent = transparent,
- super(child) {
- assert(_transparent != null);
- }
- bool get transparent => _transparent;
- bool _transparent;
- set transparent(bool value) {
- assert(value != null);
- if (value == _transparent) return;
- _transparent = value;
- }
- @override
- bool hitTest(BoxHitTestResult result, {required Offset position}) {
- // forward hits to our child:
- final hit = super.hitTest(result, position: position);
- // but report to our parent that we are not hit when `transparent` is true:
- return !transparent && hit;
- }
- @override
- void debugFillProperties(DiagnosticPropertiesBuilder properties) {
- super.debugFillProperties(properties);
- properties.add(DiagnosticsProperty<bool>('transparent', transparent));
- }
- }
|