Home > Blockchain >  GetX update a widget with list of widgets
GetX update a widget with list of widgets

Time:10-14

So what I want to do is pretty simple I have an integer called currentStep and I also have a list of widgets inside a builder when I click the button its updates both currentStep value and the widget that should be returned.

Here's the code:

  Widget StackPositioned(BuildContext context) {
    return Positioned(
      top: 260,
      width: MediaQuery.of(context).size.width,
      child: Container(
        width: MediaQuery.of(context).size.width,
        margin: const EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: <Widget>[
            RegistrationHeader(context),
            const Gap(20),
            RegistrationSteps(context)[currentStep.value],
          ],
        ),
      ),
    );
  }

List of widgets:

List<Widget> RegistrationSteps(BuildContext context) {
    return [
      Column(
        children: <Widget>[
          Label(context, "Tam Adınız"),
          const Gap(10),
          FullNameInput(context),
          const Gap(15),
          Label(context, "E-Posta"),
          const Gap(10),
          EmailInputField(context),
          const Gap(15),
          Label(context, "Şifreniz"),
          const Gap(10),
          PasswordInputField(context),
          ....

And the updater:

          TextButton(
            onPressed: () => currentStep.value  ,
            style: TextButton.styleFrom(
              backgroundColor: Colors.purple,
              minimumSize: Size(MediaQuery.of(context).size.width, 50),
            ),
            child: Text(
              "Devam Et",
              style: Displays.display2.copyWith(color: Colors.white),
            ),
          ),

My integer:

RxInt currentStep = 0.obs;

Edit: I have done this with setState() but, GetX is the project requirement.

CodePudding user response:

Just wrap your RegistrationSteps(context)[currentStep.value],widget with Obx.

like this

Obx(() {
return RegistrationSteps(context)[currentStep.value];
});

It will make RegistrationSteps widget observable. that means if anyvalue changes inside it , it will update itself.

  • Related