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

Not able to re-assign PainterController #37

Open
nayanbabariya95 opened this issue Jul 21, 2021 · 3 comments
Open

Not able to re-assign PainterController #37

nayanbabariya95 opened this issue Jul 21, 2021 · 3 comments

Comments

@nayanbabariya95
Copy link

nayanbabariya95 commented Jul 21, 2021

I want to re-assign PainterController object but it throws me A PainterController was used after being disposed.

My Scenario

I want to make a painter screen similar to the one of Social Media apps like instagram or fb messenger. So, the user can draw something on the image, but he/she can also update it in a second moment.

I have two screen ImageScreen and BrushScreen. in the ImageScreen there is option to go for Brush and draw anything on image. then user can tap "Done" button and get back to ImageScreen. But when user again go to the BrushScreen i need to keep PainterController. So user can update with existing one.

It works when assigned PainterController first time. but at second time it throws error as i mentioned above.

Any help on this would be much appreciated!

Code Snippet

BrushScreen

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:painter/painter.dart';

class BrushScreen extends StatefulWidget {
  final File imageFile;

  const BrushScreen({Key? key, required this.imageFile}) : super(key: key);

  @override
  _BrushScreenState createState() => _BrushScreenState();
}

class _BrushScreenState extends State<BrushScreen> {
  late final PainterController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PainterController()
      ..backgroundColor = Colors.transparent
      ..drawColor = Colors.white;

    _controller.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: Stack(
                children: [
                  Positioned.fill(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(16),
                      child: Image.file(
                        widget.imageFile,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Painter(_controller),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

ImageScreen Brush button

onTap: () async {
  final result = await Navigator.of(context).push(MaterialPageRoute(builder: (_) => 
  BrushScreen(imageFile: imageFile)));
  if (result is PainterController)
                      setState(() {
                        _paintController = result;
                      });
                  },
@prateek-aher
Copy link

Say I received a base64encoded image from an API. My app requires a feature to draw or erase on top of that image and then save it.
Is there no way to initialize the PaintController with a background image?

@nayanbabariya95
Copy link
Author

nayanbabariya95 commented Oct 26, 2021

@prateek-aher Use Stack widget. place Image and then Painter widget

@prateek-aher
Copy link

I thought of it too. But then, eraseMode=true doesn't work on the image,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants