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

[bug] Navigator.of(context).pop() from onImageEditingComplete can't return data #6

Closed
ynnob opened this issue Feb 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ynnob
Copy link

ynnob commented Feb 13, 2024

Describe the bug
When calling Navigator.of(context).pop('page2'); from inside the onImageEditingComplete event no data is returned.
i expect the string 'page2' to be retuned but the return value remains null. I also tried to use a explicit context that is consitend over the navigation but this still results in null being returned.

I made a simple app to reproduce the issue. Page1 returns 'page1' successfully while Page2 containing the ProImageEditor does not return 'page2'.

main.dart

import 'package:flutter/material.dart';
import 'package:poptest/page1.dart';
import 'package:poptest/page2.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
                onPressed: () => _goToPage1(), child: Text('Go to Page1')),
            TextButton(
                onPressed: () => _goToPage2(), child: Text('Go to Page2')),
          ],
        ),
      ),
    );
  }

  void _goToPage1() async {
    final result = await Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Page1()));
    print(result);
  }

  void _goToPage2() async {
    final result = await Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Page2()));
    print(result);
  }
}

page1.dart

import 'package:flutter/material.dart';

class Page1 extends StatefulWidget {
  const Page1({super.key});

  @override
  State<Page1> createState() => _Page1State();
}

class _Page1State extends State<Page1> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () => Navigator.of(context).pop('page1'),
          child: Text('Close page1'),
        ),
      ),
    );
  }
}

page2.dart

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

class Page2 extends StatefulWidget {
  const Page2({super.key});

  @override
  State<Page2> createState() => _Page1State();
}

class _Page1State extends State<Page2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProImageEditor.network(
        'https://picsum.photos/id/237/2000',
        onImageEditingComplete: (byte) async {
          Navigator.of(context).pop('page2');
        },
      ),
    );
  }
}
@ynnob ynnob added the bug Something isn't working label Feb 13, 2024
hm21 added a commit that referenced this issue Feb 13, 2024
Details can be found in [GitHub issue #6](#6).
@hm21
Copy link
Owner

hm21 commented Feb 13, 2024

Thanks for reporting that issue. I've fixed it in the latest release 2.4.1 by a solution that allows you to close the editor with custom parameters. That update includes a new callback function onCloseEditor that triggers when the editor needs to be closed. You can use this callback to pass your parameters when closing the editor.

class _Page2State extends State<Page2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProImageEditor.network(
        'https://picsum.photos/id/237/2000',
        onImageEditingComplete: (byte) async {
          /*
            `Your code for handling the edited image. Upload it to your server as an example.`

            You can choose whether you want to use await, so that the loading-dialog remains visible until your code is also ready, 
            or without async, so that the loading-dialog closes immediately.
          
            Close the page in the function `onCloseEditor` for the case you need to
            return your own parameters
          */
        },
        onCloseEditor: () {
          Navigator.of(context).pop('page2');
        },
      ),
    );
  }
}

@hm21 hm21 closed this as completed Feb 13, 2024
@Kora3 Kora3 mentioned this issue Apr 17, 2024
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