jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.38k stars 1.62k forks source link

Can only open a new page once in a dialog #985

Open Invincible1996 opened 3 years ago

Invincible1996 commented 3 years ago

Description

Can only open a new page once in a dialog

My suggested solution

Open new page again when click button

Reproduction code

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_demo/main.dart';
import 'package:get_demo/second_page.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    Future.delayed(Duration(milliseconds: 500), () {
      Get.dialog(Material(
        type: MaterialType.transparency,
        child: Container(
          // color: Colors.deepPurple,
          child: Center(
            child: RaisedButton(
              child: Text('to second'),
              onPressed: () async {
                // Get.back();
                Get.toNamed('/second');
              },
            ),
          ),
        ),
      ));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('HomePage')),
      body: Container(
        color: Colors.white,
        child: Center(
          child: RaisedButton(child: Text('to second page'), onPressed: () => Get.toNamed('/second')),
        ),
      ),
    );
  }
}

To Reproduce

Steps to reproduce the behavior:

  1. Click on button to second page
  2. Close second page
  3. Click on button to second page again

Expected behavior

Can open new page again

Flutter Version:

flutter 1.22.5 stable channel

Getx Version:

get: ^3.22.2

Describe on which device you found the bug:

Both android and iOS devices.

eduardoflorence commented 3 years ago

For your use case, use Get.toNamed('/second', preventDuplicates: false);

Let's leave this issue open, as we can improve this approach. What is happening is that on the back of the second page the navigation stack has a dialog before the home page, so the current route has not been changed yet and the GetX navigation blocks navigation for the same route. Here is a test code to help investigate this bug:

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

void main() {
  runApp(GetMaterialApp(
    initialRoute: '/home',
    getPages: [
      GetPage(
        name: '/home',
        page: () => HomePage(),
      ),
      GetPage(
        name: '/second',
        page: () => SecondPage(),
      ),
    ],
  ));
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 500), () {
      Get.dialog(Material(
        type: MaterialType.transparency,
        child: Container(
          child: Center(
            child: RaisedButton(
              child: Text('to second'),
              onPressed: () async {();
                // Works
                Get.toNamed('/second', preventDuplicates: false);
                // Dont Works
                // Get.toNamed('/second');
              },
            ),
          ),
        ),
      ));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('HomePage')),
      body: Container(
        color: Colors.white,
        child: Center(
          child: RaisedButton(
              child: Text('to second page'),
              onPressed: () => Get.toNamed('/second')),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('SecondPage')),
      body: Container(),
    );
  }
}
Suhailakl commented 3 years ago

@eduardoflorence @jonataslaw Any updates on this issue?

Suhailakl commented 3 years ago

Sorry !.,preventDuplicates:false worked in my case