[英]Using Dart and Flutter, how can I save data from a dynamic widget, into a list of maps, that is inside of another list
apologies if this has already been asked.抱歉,如果已经有人问过这个问题。
Below is the code for a simplified version of an app I'm building.下面是我正在构建的应用程序的简化版本的代码。
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TextEditingController dutyDescriptionController = TextEditingController();
//this list holds a list of DynamicWidgets()
List<DynamicWidget> dynamicList = [];
//this list holds a list of duties
List<Map<String, dynamic>> dutiesList = [];
//this list holds a list of activities within a duty, currently just dummy data
List<Map<String, dynamic>> activitiesList = [
{'description': 'Some Activity 1'},
{'description': 'Some Activity 2'},
];
//add DynamicWidget to the dynamicList (max of 3 widgets)
addDynamic() {
if (dynamicList.length <= 2) {
dynamicList.add(DynamicWidget());
setState(() {});
}
}
//submit the data
submitData() {
//there should only be 1 element in the duties list hence why I clear the list prior to adding to it
//not sure if this is the correct way to go about it
dutiesList.clear();
dutiesList.add({
'dutyDescription': dutyDescriptionController.text,
'activityDescription': activitiesList,
});
print(dutiesList);
//completely stuck here.
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic Widget'),
),
body: ListView(
children: [
TextField(
controller: dutyDescriptionController,
decoration: InputDecoration(labelText: 'Duty Description'),
),
Expanded(
flex: 1,
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: dynamicList.length,
itemBuilder: (BuildContext context, int index) => dynamicList[index],
),
),
ElevatedButton(
onPressed: addDynamic,
child: Text('Add Activity'),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
submitData();
},
child: Icon(Icons.save),
),
);
}
}
//this widget is created dynamically and displayed in a listview.builder inside MyApp()
class DynamicWidget extends StatelessWidget {
final TextEditingController controller1 = TextEditingController();
@override
Widget build(BuildContext context) {
return TextField(
controller: controller1,
decoration: InputDecoration(labelText: 'Activity Description'),
);
}
}
The app has a single textfield used to store a duty for a shift of work.该应用程序有一个文本字段,用于存储轮班工作的职责。
Below that is a button labeled 'Add Activity'.下面是一个标有“添加活动”的按钮。 When pressed, it adds a DynamicWidget() to the the dynamicList which is used in conjunction with a listview.builder to display the widget.按下时,它会将 DynamicWidget() 添加到 dynamicList 中,该 dynamicList 与 listview.builder 结合使用以显示小部件。
There is also a floating action button that when pressed currently adds the contents of the 'Duty Description' textfield to the dutiesList list as well as the contents of activitiesList (which is currently harcoded with dummy data).还有一个浮动操作按钮,当按下该按钮时,当前会将“职责描述”文本字段的内容添加到职责列表列表以及活动列表的内容(当前使用虚拟数据进行硬编码)。
This is how the data appears in dutiesList at present.这就是数据目前在 dutiesList 中的显示方式。 In case it isn't obvious, the key 'activityDescription' is where I will store the activitiesList.如果不是很明显,键“activityDescription”是我将存储活动列表的地方。
[
{
dutyDescription: SomeDuty,
activityDescription: [
{
description: SomeActivity1
},
{
description: SomeActivity2
}
]
}
]
What I want to do is start with an empty activitiesList and add the contents of the 'Activity Description' textfields (because there will be multiple textfields as well as other user input sources located inside of DynamicWidget in the production app) to the activitiesList but I can't figure out how to do it.我想要做的是从一个空的 activitiesList 开始并将“活动描述”文本字段的内容(因为会有多个文本字段以及位于生产应用程序中 DynamicWidget 内部的其他用户输入源)添加到 activitiesList 但我不知道该怎么做。
I'm still fairly new to Flutter, learning at a fast pace but I've been staring at this for way to long.我对 Flutter 还很陌生,学习速度很快,但我已经盯着它看了很长时间了。 Any help would be awesome.任何帮助都是极好的。
Thanks in advance.提前致谢。
Have you actually tried running this code?你真的试过运行这段代码吗? I can't imagine that a ListView inside an Expanded inside another ListView won't cause errors.我无法想象另一个 ListView 内的 Expanded 内的 ListView 不会导致错误。
Why did you make "duties" a list if "there should only be 1 element?"如果“应该只有 1 个元素”,为什么要将“职责”列为一个列表?
Solution you asked for:您要求的解决方案:
Put the following function inside _MyAppState
, next to addDynamic
and submitData
for the sake of tidyness为了整洁起见,将以下 function 放在_MyAppState
旁边, addDynamic
和submitData
旁边
void update(String description) {
setState(() {
activitiesList.add({'description': description});
});
}
then change DynamicWidget
to something like然后将DynamicWidget
更改为类似
class DynamicWidget extends StatefulWidget {
final Function update;
DynamicWidget({required this.update});
@override
_DynamicWidgetState createState() => _DynamicWidgetState();
}
class _DynamicWidgetState extends State<DynamicWidget> {
String description = '';
@override
Widget build(BuildContext context) {
return SizedBox(
height: 80.0,
width: 350.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: 300.0,
child: TextField(
onChanged: (String text) {
setState(() {
description = text;
});
},
decoration: InputDecoration(labelText: 'Activity Description'),
),
),
IconButton(
icon: const Icon(Icons.arrow_forward),
tooltip: 'Submit',
onPressed: () => widget.update(description),
),
],
),
);
}
}
Call DynamicWidget
like so像这样调用DynamicWidget
DynamicWidget(update: update)
If you print activitiesList
before and after submitting you will see that whatever you type has been added.如果您在提交之前和之后打印activitiesList
,您将看到您输入的任何内容都已添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.