flutterdartwhiteboard

Is MaterialApp/Scaffold absorb my Listener?


I try to code a very simple Whiteboard. When I use

  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Container(
                child: Listener(

the app isn't recognizing any mouseclick (onPointerDown()). When I use directly

  Widget build(BuildContext context) {
    return Container(
      child: Listener(

everything is ok and I can see some action in the onPointerDown(). So, I think I miss something. I want to use the MaterialApp, to get access to some of the features.

What I tried so far: At first, I tried to minimize my function, to focus on only that problem. So, this is my full minimized code:

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatefulWidget {
  @override
  AppState createState() => AppState();
}

class AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Container(
                color: Colors.white,
                child: Listener(
                  onPointerDown: (details) {
                    print('Action');
                  },
                  child: CustomPaint(
                    painter: DrawingPainter(),
                  ),
                ))));
  }
}

class DrawingPainter extends CustomPainter {
  DrawingPainter();

  @override
  void paint(Canvas canvas, Size size) {}

  @override
  bool shouldRepaint(DrawingPainter oldDelegate) => true;
}

What I recognized, the size of the DrawingPainter is (0.0, 0.0). Maybe the Problem is that the Painter isn't span about the full size? If this is so, how can I change that? I tried to set the size, but ended with size == 0.0, 0.0 again.


Solution

  • Yes, you just have to use a SizedBox.expand or anything that will force your content to expand its size to match parent.

    see https://zu4c06ylu4d0.zapp.page/#/