现在我要将我的虚拟数据传递到轮播项目。
我希望在滑块中同时显示两个项目...
但是您知道map方法一次遍历列表的一项...
现在我用行小部件将轮播项目除以2 ...
还有其他方法可以完成吗?
类似的增量每次要增加2 ...
import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
var reports = [
{
'title': '1',
'content': '1'
},
{
'title': '2',
'content': '2'
},
{
'title': '3',
'content': '3'
},
{
'title': '4',
'content': '4'
},
];
Widget buildReport() {
return CarouselSlider(
options: CarouselOptions(
height: 250.0,
),
items: reports
.asMap()
.map(
(i, report) {
return MapEntry(
i,
Builder(
builder: (BuildContext context) {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Card(
child: Container(
child: Column(
children: [
Text(
'${report['title']}',
),
Text(
'${report['content']}',
),
],
),
),
),//i wish these two cards have different data.
Card(
child: Container(
child: Column(
children: [
Text(
'${report['title']}',
),
Text(
'${report['content']}',
),
],
),
),
),
],
),
);
},
),
);
},
)
.values
.toList(),
);
}
我想到了(for循环),但是我不知道如何在这段代码中做到这一点...
答案 0 :(得分:1)
导入“ package:flutter / material.dart”;
import'package:carousel_slider / carousel_slider.dart';
void main(){
runApp(MyApp());
}
MyApp类扩展了StatefulWidget {
const MyApp( { 关键键this.choice @必需this.item }):super(key:key);
最终选择选择; 最终选择项;
@override MyAppState createState()=> MyAppState();
}
_MyAppState类扩展了状态{
@override
小部件构建(BuildContext上下文){
返回MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Basic demo')),
body: Container(
child: CarouselSlider(
options: CarouselOptions(
disableCenter: false,
),
items: choices.map((Choice) => Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Card(
child: Container(
child: Column(
children: [
Text(
Choice.title , style: null, textAlign: TextAlign.left,
),
Text(
Choice.content, style: null, textAlign: TextAlign.left,
), ],),),),
Card(
child: Container(
child: Column(
children: [
Text(
Choice.title, style: null, textAlign: TextAlign.left,
),
Text(
Choice.content, style: null, textAlign: TextAlign.left,
),
],
),
),
),
],
),
color: Colors.green,
)).toList(),
)
),
));
}
}
班次选择{
const Choice({this.title,this.content});
最终字符串标题;
最终字符串内容;
}
const列表选择= const [
const Choice(title:'title:1',content:'content:1'),
const Choice(title:'title:2',content:'content:2'),
const Choice(title:'title:3',content:'content:3'),
const Choice(title:'title:4',content:'content:4'),
];