flutterflutter-layoutclipping

Flutter Stack Clip not detecting gestures


I was trying to make a Solitaire game and was building the cards inside the previous one. But the stack clip parameter is giving me more work than I was imagining.

If I put nothing inside the parameter, the output is this:

Stack clipped on the bottom right edge.

If I put clipBehavior: Clip.none, the visual output is this (without the red lines):

Stack not clipped.

Great! Thats visually what I wanted. The only problem is that when I touch outside the first image boundries (represented by the red lines), flutter doesn't render the touch.

Does anyone know how to make the gesture detection work again?

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Stack Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Stack Test'),
      ),
      body: const Center(
        child: TestStackWidget(
          listSize: 3,
          width: 200,
        ),
      ),
    );
  }
}

class TestStackWidget extends StatelessWidget {
  const TestStackWidget({
    Key? key,
    required this.width,
    required this.listSize,
  }) : super(key: key);

  final double width;
  final int listSize;

  @override
  Widget build(BuildContext context) {
    return Stack(
      clipBehavior: Clip.none,  // ----------------------------Here-----------------------
      children: [
        SizedBox(
          width: width,
          child: AspectRatio(
            aspectRatio: 1 / 1.61,
            child: Card(
              margin: EdgeInsets.zero,
              color: Colors.blue,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20),
              ),
              child: InkWell(
                onTap: () => debugPrint(listSize.toString()),
                borderRadius: BorderRadius.circular(20),
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(),
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
              ),
            ),
          ),
        ),
        if (listSize > 0)
          Positioned(
            top: 50,
            left: 20,
            child: TestStackWidget(
              width: width,
              listSize: listSize - 1,
            ),
          ),
      ],
    );
  }
}

Solution

  • I found this issue about this problem and the only easy solution I found wast to wrap the first child inside the Stack in a Column and add a SizedBox like this:

    Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      SizedBox(
        width: width,
        child: AspectRatio(
          aspectRatio: 1 / 1.61,
          child: Card(
            margin: EdgeInsets.zero,
            color: Colors.blue,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20),
            ),
            child: InkWell(
              onTap: () => debugPrint(listSize.toString()),
              borderRadius: BorderRadius.circular(20),
              child: Container(
                decoration: BoxDecoration(
                  border: Border.all(),
                  borderRadius: BorderRadius.circular(20),
                ),
              ),
            ),
          ),
        ),
      ),
      SizedBox(height: 50.0 * listSize), //--------------Added-------------------
    ],