Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onPanUpdate does not work in Flutter Web #9

Open
rishisinghal opened this issue Sep 6, 2020 · 2 comments
Open

onPanUpdate does not work in Flutter Web #9

rishisinghal opened this issue Sep 6, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@rishisinghal
Copy link

Hi,

I just tried the example in https://medium.com/flutter-community/bring-your-custompainter-paintings-to-life-in-flutter-using-touchable-c2413cd1897 and it was working for Android but not for web. In web I was not able to drag the earth.

Here is the sample program

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:touchable/touchable.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home:MyCoolPage(),
    );
  }
}

class MyCoolPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(child: MyWidget());
  }
}

class PaintingController extends ChangeNotifier {
  Offset circlePosition;
  Color backgroundColor = Colors.yellow;

  void moveCircle(Offset newPosition) {
    circlePosition = newPosition;
    notifyListeners();
  }

  void toggleBackGroundColor() {
    backgroundColor = backgroundColor == Colors.white ? Colors.blueGrey : Colors.white;
    notifyListeners();
  }
}

class MyWidget extends StatelessWidget {
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<PaintingController>(
      create: (context) => PaintingController(),
      child: Consumer<PaintingController>(
        builder: (context, controller, child) => AnimatedContainer(
          color: controller.backgroundColor,
          duration: Duration(seconds: 1),
          child: CanvasTouchDetector(
            builder: (context) => CustomPaint(painter: MyPainter(context)),
          ),
        ),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  final BuildContext context;
  final PaintingController controller;

  MyPainter(this.context) : controller = Provider.of<PaintingController>(context, listen: false);

  @override
  void paint(Canvas canvas, Size size) {
    TouchyCanvas touchyCanvas = TouchyCanvas(context, canvas);
    drawEarth(touchyCanvas, size);
    drawSun(touchyCanvas, size);
  }

  void drawEarth(TouchyCanvas canvas, Size size) {
    var blueCircleCenter = controller.circlePosition ?? Offset(size.width / 2, size.height / 2 - 100);

    canvas.drawCircle(blueCircleCenter, 60, Paint()..color = Colors.lightGreen, onPanUpdate: (details) {
      print("pan update called");
      controller
          .moveCircle(Offset(blueCircleCenter.dx + details.delta.dx * 2, blueCircleCenter.dy + details.delta.dy * 2));
    },
      onPanDown: (details) {
        print("pan down called");
        controller.moveCircle(Offset(details.localPosition.dx, details.localPosition.dy));
      },
    );

    canvas.drawCircle(blueCircleCenter, 30, Paint()..color = Colors.lightBlueAccent,
        hitTestBehavior: HitTestBehavior.translucent);
  }

  void drawSun(TouchyCanvas canvas, Size size) {
    var orangeCircleCenter = Offset(size.width / 2, size.height / 2 + 100);
    canvas.drawCircle(orangeCircleCenter, 80, Paint()..color = Colors.orangeAccent, onTapDown: (_) {
      controller.toggleBackGroundColor();
    });
    canvas.drawCircle(orangeCircleCenter, 50, Paint()..color = Colors.orange,
        hitTestBehavior: HitTestBehavior.translucent);
    canvas.drawCircle(orangeCircleCenter, 30, Paint()..color = Colors.deepOrangeAccent,
        hitTestBehavior: HitTestBehavior.translucent);
    canvas.drawCircle(orangeCircleCenter, 20, Paint()..color = Colors.deepOrange,
        hitTestBehavior: HitTestBehavior.translucent);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.87. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@issue-label-bot issue-label-bot bot added the bug Something isn't working label Sep 6, 2020
@softmarshmallow
Copy link

updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants