flutter - 颤振在运行时创建小部件
问题描述
我想在颤振中创建表单。但是将在表单中使用的小部件不是预定义的。使用哪些小部件将由 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"
}
解决方案
假设您的模型类是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);
}
}
);
推荐阅读
- java - DropWizard(使用 Jetty)使用 maven 在“org/eclipse/jetty/util/thread/ExecutionStrategy$Factory”上抛出 NoClassDefFoundError
- linux - Shell 脚本 - 如何在不重复行的情况下合并两个文本文件
- javascript - Electron 应用程序在透明模式下显示额外的边框
- ssh - 我应该为 SSH 输入什么密码?
- r - 为投资组合分析编写条件 If 语句
- ionic-framework - 在离子警报中显示 svg
- azure - Azure 自动化:Runbook:它知道它是 TenantID 吗?
- kubernetes - Kubernetes 是否需要最少数量的副本才能执行滚动部署?
- powershell - 大文件的 JSON 到 CSV 转换
- javascript - 使用 javascript 通过 html 表单发送 Base64 图像源代码