使用飞镖在颤动中遍历按钮按下列表
Iterate through a list on button press in flutter using dart
您好,我需要在 list.want 中显示一项,以便在每次按下按钮时逐一遍历列表?。但我找不到办法。这是我的清单
[
{
"id": 1,
"name": "quidem molestiae enim",
"date": "2021, Jun, 20-24"
},
{
"id": 2,
"name": "sunt qui excepturi placeat culpa",
"date": "2019, Feb, 20-24"
},
{
"id": 3,
"name": "omnis laborum odio",
"date": "2021, Jan, 20-26"
},`enter code here`
]
由于您有自己的列表,因此我创建了一个名为 weekdays
的示例列表。请将该列表替换为您的 weeksModel
列表。
我所做的只是 flutter 中的基本列表迭代,没什么。由于我没有您的 WeeksModel
模型或 e
json,我认为我无法提供更多帮助。
import 'package:flutter/material.dart';
class WeekWidget extends StatefulWidget {
@override
_WeekWidgetState createState() => _WeekWidgetState();
}
class _WeekWidgetState extends State<WeekWidget> {
List weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
int index = 0;
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(weekdays[index]),
ElevatedButton(
onPressed: () {
if (index < weekdays.length - 1) {
setState(() {
index++;
});
}
},
child: Text("Next item"),
),
],
);
}
}
这是否回答了您的问题?
您好,我需要在 list.want 中显示一项,以便在每次按下按钮时逐一遍历列表?。但我找不到办法。这是我的清单
[
{
"id": 1,
"name": "quidem molestiae enim",
"date": "2021, Jun, 20-24"
},
{
"id": 2,
"name": "sunt qui excepturi placeat culpa",
"date": "2019, Feb, 20-24"
},
{
"id": 3,
"name": "omnis laborum odio",
"date": "2021, Jan, 20-26"
},`enter code here`
]
由于您有自己的列表,因此我创建了一个名为 weekdays
的示例列表。请将该列表替换为您的 weeksModel
列表。
我所做的只是 flutter 中的基本列表迭代,没什么。由于我没有您的 WeeksModel
模型或 e
json,我认为我无法提供更多帮助。
import 'package:flutter/material.dart';
class WeekWidget extends StatefulWidget {
@override
_WeekWidgetState createState() => _WeekWidgetState();
}
class _WeekWidgetState extends State<WeekWidget> {
List weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
int index = 0;
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(weekdays[index]),
ElevatedButton(
onPressed: () {
if (index < weekdays.length - 1) {
setState(() {
index++;
});
}
},
child: Text("Next item"),
),
],
);
}
}
这是否回答了您的问题?