Skip to content

Not able to re-assign PainterController #37

@nayanbabariya95

Description

@nayanbabariya95

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;
                      });
                  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions