Suraj
Suraj

Reputation: 3

flutter create widgets at run time

I want to create forms in flutter. But the widgets that will be used in the form are not predefined. Which widgets to use will be determined by json response. Is there any way in flutter to create widgets at runtime and manage their state.

The json will look something like below

{  
  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"
}

Upvotes: 0

Views: 1574

Answers (1)

Raouf Rahiche
Raouf Rahiche

Reputation: 31356

let's say that your model class is Question and you want to make a widget for each Question you have in your list:

List<Question> Questions;

You simply map them :

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

Upvotes: 1

Related Questions