Home > Blockchain >  Use String to call a WIdget Flutter
Use String to call a WIdget Flutter

Time:11-07

In my case: I have listTemplates the same:

List<TemplateModel> listTemplate = [
TemplateModel(
    id: 1,
    name: 'Temp_1',
    previewBack: AppImages.TEMP_1,
    previewFront: AppImages.TEMP_1,),
TemplateModel(
    id: 2,
    name: 'Temp_2',
    previewBack: AppImages.TEMP_2,
    previewFront: AppImages.TEMP_2),
TemplateModel(
    id: 3,
    name: 'Temp_3',
    previewBack: AppImages.TEMP_3,
    previewFront: AppImages.TEMP_3),
TemplateModel(
    id: 4,
    name: 'Temp_4',
    previewBack: AppImages.TEMP_4,
    previewFront: AppImages.TEMP_4),
  ];

And I also have Temp_1(), Temp_2(), Temp_3(), Temp_4() Widgets.

I want to show each by name of template ex:

Container(
    child: `Temp_1`(), // It's my explain to try to show my problem, of course, it's not work.
);

I don't want to use if-else or switch-case for this case becuase I have the too large list templates.

Thank you for your answer!

CodePudding user response:

I think It's not support now. Do you use right solution for this case?

CodePudding user response:

No, you can't. Another option you can use - Map<int, Widget>.

final temperatureUI = {
    1: Temp_1(),
    ...
}

and then

Container(
    child: temperatureUI[templateModel.id],
);
  • Related