-1

I have a situation where I want to create multiple flutter StatefulWidgets based on the same state class. The problem I run into seems clear, the state widget needs to know the properties of the parent that calls createState. There has to be a way to accomplish this without having to copy paste large amounts of code.

What I have so far is a state class that looks like this:

abstract class XxxState<T extends StatefulWidget> extends State<T> 
{  TextEditingController textController; // instantiated by initState().
}

to accomplish what I want I create several abstract methods that the subclass will override to implement it's function. The problem is when I create the class I want to instantiate I don't know how to connect the new StatelessWidget to the state class that overrides the XxxState class.

class YyyWidget extends StatefulWidget {
@override
  _YyyState createState() => _YyyState();
}

class _YyyState<YyyWidget> extends XxxState {
/// In this class widget.xxx looks to StatefulWidget and not YyyWidget
}

Any reference for how this should be done would be appreciated.

Nefarious
  • 458
  • 6
  • 21
  • The problem isn't clear for me, but can I know why aren't you using a state management library – Mohammed Alfateh Dec 26 '21 at 01:55
  • @7mada, who says I'm not? The problem is I have a substantial amount of code that would need to be duplicated in different ways, slight variations, if I can't figure this out. – Nefarious Dec 26 '21 at 01:57
  • Using `mixin` or `abstract` class with any state management solution would easier than doing it with `StatefulWidget` widgets. – Mohammed Alfateh Dec 26 '21 at 02:00
  • I will edit the post. The reason it is stateful is because there is a TextController in the stateful widget. – Nefarious Dec 26 '21 at 02:04
  • I am not used to this kind of approach but you can try to use keys to identify widgets. – Mohammed Alfateh Dec 26 '21 at 02:23
  • I'm willing to try any approach. As it is, I am going to start duplication code. – Nefarious Dec 26 '21 at 02:37
  • Can you share more details, for example your data class and the expected result you would like to have, I would like to help but the question is a bit confusing for me. – Mohammed Alfateh Dec 26 '21 at 02:45
  • I will work out a small example that shows the problem and post it in the morning. I have to get ready for dinner now. – Nefarious Dec 26 '21 at 02:47

1 Answers1

1

All one has to do is learn how to subclass a StatefulWidget correctly and all works as expected.

abstract class YyyWidget extends StatefulWidget { ... }
// Do not do the createState here
// Someone edited the question and removed the abstract from the code, 
// thereby changing the question to one they thought they could answer.

Then create the abstract state

abstract class YyyState<Page extends XxxWidget>
extends State<Page> {
{
   TextEditingController textController;
   ...
}

Then the subclass calls

class ZzzWidget extends XxxWidget {
// Do the create state here
@override
_AaaState createState() => _AaaState();
}

and the subclass state is

class _AaaState extends YyyState<AaaWidget> {
//inside this class widget.field gets state from AaaWidget
}

I don't even remember what the original post was, So maybe deleting the question is the better option here.

Nefarious
  • 458
  • 6
  • 21
  • It's good to see that you're making progress by yourself, and you don't have to delete the question because it might help others who've had a similar problem. – Mohammed Alfateh Dec 27 '21 at 12:02