简体   繁体   English

使用 String 调用 WIdget Flutter

[英]Use String to call a WIdget Flutter

In my case: I have listTemplates the same:就我而言:我的listTemplates是相同的:

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.而且我还有Temp_1()Temp_2()Temp_3()Temp_4()小部件。

I want to show each by name of template ex:我想按模板名称显示每个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.我不想在这种情况下使用if-elseswitch-case ,因为我有太大的列表模板。

Thank you for your answer!谢谢您的回答!

I think It's not support now.我认为现在不支持。 Do you use right solution for this case?您对这种情况使用正确的解决方案吗?

No, you can't.不,你不能。 Another option you can use - Map<int, Widget>.您可以使用的另一个选项 - Map<int, Widget>。

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

and then进而

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM