首页 文章

flutter在运行时创建小部件

提问于
浏览
0

我想在颤动中创建表单 . 但是,将在表单中使用的小部件未预定义 . 使用哪些小部件将由json响应确定 . 有没有办法在运行时创建小部件并管理它们的状态 .

json看起来像下面的样子

{  
  id:1,
  Answer: "Click on Register button so that we can get you started",
  Widget:[  
      "google OAuth"
  ],
  ReplyUrl:"www.example.com/api"
}
{
  id:2,
  Widget: [  
     {  
       "Question":"What is your name ?",
       "Type":"text",
       "VariableName":"Onboarding_Name"
     },
     {  
         "Question":"What is your birth date ?",
         "Type":"date",
         "VariableName":"Onboarding_BirthDate"
     }
   ],
   ReplyUrl:"www.example.com/api"
}

1 回答

  • 0

    假设您的模型类是 Question ,并且您想为列表中的每个 Question 创建一个小部件:

    List<Question> Questions;
    

    你只需映射它们:

    Questions.map((question) {
          if question.type == date {
            return DateQuestionWidget(question.title);
          } else if question.type == text {
            return TextQuestionWidget(question.title);
          }
         }
       );
    

相关问题