Git Product home page Git Product logo

Comments (11)

jonataslaw avatar jonataslaw commented on May 21, 2024

Flutter creates a GlobalKey navigation for Drawers, another for snackbars, another for dialogs, another for bottomsheets, and accesses each one through the context. 'Get' goes contrary to this, and to save memory, put it all together in a single globalKey, instead of having one for each thing (since the documentation itself explains that globalKeys are expensive and should be avoided, but navigation unlike Framework that is incredible, falls short in several aspects). Drawer is not part of Get yet, so I have two options: add it to the package (I'm very busy, I wouldn't be able to do it in less than 1 month) or add the context as an option for to/back, to do as in the main navigation, navigate through the Navigator widget, passing the context than GlobalKey is using.
This looks ugly hack, but if it is a problem for you to wait, let me know I can do it in minutes.

from getx.

svadhis avatar svadhis commented on May 21, 2024

from getx.

jonataslaw avatar jonataslaw commented on May 21, 2024

It is not necessary to add Drawer to Get, because it uses the root browser, which in this case will be Get.
In the test case, Get.back (); had the same behavior as Navigator.pop (context);

void main() {
  runApp(MaterialApp(
   home:MyHomePage(),
    navigatorKey: Get.key,
    navigatorObservers: [
      GetObserver(), 
    ],
  ));
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("test")),
      body: Center(child: Text('My Page!')),
      drawer: Drawer(
        // Add a ListView to the drawer. This ensures the user can scroll
        // through the options in the drawer if there isn't enough vertical
        // space to fit everything.
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                //Navigator.pop(context);
                // Get.back();
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Get.back();
                // Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}

So, closing this.

from getx.

luiszheng0627 avatar luiszheng0627 commented on May 21, 2024

This issue still not fixed.
When drawer closed, controller would be immediately deleted from memory.

from getx.

luiszheng0627 avatar luiszheng0627 commented on May 21, 2024

My get version is 3.21.3

from getx.

eduardoflorence avatar eduardoflorence commented on May 21, 2024

@zl910627, I did a test and the controller is not removed from memory when the drawer is closed. Create a new issue and place a sample code that shows this behavior.

from getx.

jonataslaw avatar jonataslaw commented on May 21, 2024

When the drawer is closed, nothing should be removed from memory. Drawer is not a route.

from getx.

luiszheng0627 avatar luiszheng0627 commented on May 21, 2024

Please try with GetView.
Thank you

from getx.

luiszheng0627 avatar luiszheng0627 commented on May 21, 2024

I just fixed it temporary by putting with permanent option

from getx.

luiszheng0627 avatar luiszheng0627 commented on May 21, 2024

Any update?
Get.back or Navigator.mayPop all same behavior.
They are all cleaning controller from memory when drawer closed.

from getx.

eduardoflorence avatar eduardoflorence commented on May 21, 2024

Please try with GetView.
Thank you

I tested with GetView and the controller is not removed from memory, see an example below:

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

void main() {
  Get.put(HomeController());
  runApp(GetMaterialApp(
    initialRoute: '/home',
    getPages: [
      GetPage(
        name: '/home',
        page: () => MyHomePage(),
      ),
    ],
  ));
}

class MyHomePage extends GetView<HomeController> {
  MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("test")),
      body: Center(child: Obx(() => Text('Count ${controller.count}'))),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                controller.increment();
                Get.back();
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                controller.increment();
                Get.back();
              },
            ),
          ],
        ),
      ),
    );
  }
}

class HomeController extends GetxController {
  final _count = 0.obs;

  int get count => _count.value;

  void increment() {
    _count.value += 1;
  }
}

from getx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.