【问题标题】:Use String to call a WIdget Flutter使用 String 调用 WIdget Flutter
【发布时间】:2021-12-20 05:41:52
【问题描述】:

就我而言: 我有 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),
  ];

我也有 Temp_1()Temp_2()Temp_3()Temp_4()小部件。

我想按模板名称显示每个 例如:

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

我不想在这种情况下使用 if-elseswitch-case,因为我的列表模板太大。

感谢您的回答!

【问题讨论】:

    标签: flutter dart widget


    【解决方案1】:

    我认为现在不支持。您是否针对这种情况使用了正确的解决方案?

    【讨论】:

      【解决方案2】:

      不,你不能。您可以使用的另一个选项 - Map

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

      然后

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-10
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        • 2020-06-29
        • 2019-10-24
        • 2018-09-04
        • 2021-11-13
        相关资源
        最近更新 更多