LayoutBuilder deleting/restarting GetX instance

147 Views Asked by At

I'm using get package to make a Flutter Web App. In my application I have 4 layout's to different screen sizes. In my Controller class, I have an observable list that changes when user insert or delete something. But if I have some item on this list, and the screen size changes, the controller is deleted and a new one is created, and then I lost all my registers of the list.

Main.dart:

class _MainState extends State<Main>{

  Controller controller = Get.put(Controller(), permanent: true);

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Container(
          width: largura_total,
          height: altura_total,
          decoration: BoxDecoration(
              color: Colors.white,
              image: DecorationImage(
                  image: AssetImage('assets/wallpaper-tela.jpg'),
                  //colorFilter: ColorFilter.mode(Colors.white.withOpacity(0.75), BlendMode.modulate,),
                  fit: BoxFit.cover
              )
          ),
          child: LayoutBuilder(
              builder: (context, constraints){
                var largura = constraints.maxWidth;
                var altura = constraints.maxHeight;
                var largura_coluna = (largura - 240)/14; //15 x padding de 16
                if(largura>1400 && altura > 500){
                  return Scaffold([...]);
                }
                else if(largura>1000 && largura<1400 && altura > 500){
                  return Scaffold([...]);
                }
                else if(largura<1000 && largura>350 && altura > 500){
                  return Scaffold([...]);
                else{
                  return Scaffold([...]);
                }
  }
}

Controller.dart

class Controller extends GetxController{
    var lista_mostrar = <ItemMostrar>[].obs;
}

I have debuged a lot of times and for sure the controller instance is getting restarted. You can test it just with this two classes and the problem will appear.

My question is: is there a way to keep the Controller instance when the layout changes?

Any help will be appreciated.

0

There are 0 best solutions below