Home > Enterprise >  Way to efficiently create multiple same widgets with different text
Way to efficiently create multiple same widgets with different text

Time:12-10

We are trying to create a language learning app as a project in flutter. One level would have same widget tree just repeated multiple times with different questions and answers to be chosen. Once correct answer is chosen it takes you to new question.

Any ideas on how we can efficiently create multiple of same widgets with different text inside?

CodePudding user response:

You can extract your widget as Widget with text parameter. So just use the widget anywhere and pass text argument now you will get same widget with different text.

CodePudding user response:

Generally speaking, if you want to create multiple objects/widgets from the same class with slight changes, all you need to do is to create a constructor and pass some data to it.

Here's an example:

Class MyText extends StatelessWidget {

  final String text;
  MyText({this.text});

  // your build method that returns the text goes here..
}

To use:

MyText('Hello');
MyText('World');

Good luck building your game!

  • Related