1

Is it possible to make a Text Widget Stateful so that the Text can be changed when i press a Button without having my Class to be Stateful? example:

class Rangliste extends StatelessWidget{.... Text(Test), Text(Test2)....}

Test and Test2 are Strings.

So when i press a Button the values inside the Text Widgets should change. Is there any Way to achieve this without changing my Class to Stateful? Or if not can somebody explain how i can change the Class to Stateful without messing up the rest of my Code

blurryface
  • 45
  • 2
  • 7

1 Answers1

1

No, you can't, a stateless widget is an immutable widget that cannot react to state changes and rerender, you will have to use some form of state management.

If your button and your text and your data are all within the same widget, then it is better to make your widget Stateful and use setState(() {}) to change your text and rerender.

You can use the Flutter plugin in order to convert your widget from a Stateless widget to a StatefulWidget, the plugin will handle everything.

Hamza Mogni
  • 672
  • 1
  • 7
  • 21
  • How about `ValueNotifier`? We can change value even if it is `Stateless` widget using `ValueNotifier` or `ChangeNotifier` – MobIT Jul 29 '21 at 13:36
  • Yes, we can use that and set up a ```ValueListenableBuilder``` to listen for changes. Though I think using ```Stateful``` widget, in this case, is the simplest. – Hamza Mogni Jul 29 '21 at 13:44
  • 1
    i did it like in the Orignial answer. Worked out pretty good for me thank you :) – blurryface Jul 29 '21 at 20:34