[英]Flutter how to create horizontal scrollable cardview using list of data
I'm creating a horizontal scrollable cardviews.我正在创建一个水平可滚动的卡片视图。 Inside the cardview, I want to set the image and textview.The data inside cardview will be fetch from the list.Now how to set the list data inside the cardview在cardview里面,我想设置image和textview。cardview里面的数据将从列表中获取。现在如何在cardview里面设置列表数据
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: new Scaffold(
body: new SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
buildBody(),
productsCard()
],
),
),
)
);
}
Widget buildBody() {
return Stack(
// alignment: Alignment.bottomCenter,
children: <Widget> [
new Image(
image: new AssetImage('assets/homebg.png'),
),
Positioned(
left: 50.0,
right: 50.0,
bottom: 40.0,
height: 64.0,// adjust this if you want some padding
child: RaisedGradientButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => StylistPage()));
},
child: new Text("BOOK AN APPOINTMENT",
style: TextStyle(fontSize: 20.0, color: Colors.white),
),
gradient: LinearGradient(
colors: <Color>[const Color(0xFF000000),const Color(0xFF000000), const Color(0xFF40079B)],
),
), // child widget
),
]
);
}
Widget productsCard(){
return Container(
child: ListView(
scrollDirection: Axis.horizontal, // <-- Like so
children: <Widget>[
Container(
width: 160.0,
color: Colors.red,
),
Container(
width: 160.0,
color: Colors.blue,
),
Container(
width: 160.0,
color: Colors.green,
),
],
)
);
}
// Here is my list of data.I have four values in each item of the list. // 这是我的数据列表。我在列表的每个项目中有四个值。
List<ProductsCollection> productData = []
..add(ProductsCollection('Discipline curl','https://sgdfgdgd/jdkjdhj.png/jashdghd',20,'akhsgdahghsgdh'))
..add(ProductsCollection('Discipline curl','https://sgdfgdgd/jdkjdhj.png/jashdghd',20,'akhsgdahghsgdh'))
..add(ProductsCollection('Discipline curl','https://sgdfgdgd/jdkjdhj.png/jashdghd',20,'akhsgdahghsgdh'));
When I try to call the productsCard method, I'm not able to see any widgets in my screen.The screen appears blank after the code and also the number of cars should depends upon the number of values available in the list.当我尝试调用 productsCard 方法时,我无法在屏幕中看到任何小部件。代码后屏幕显示为空白,而且汽车数量应取决于列表中可用值的数量。
Your list isn't showing because you haven't given any height to it.你的列表没有显示,因为你没有给它任何高度。 Add height
to the Container wrapping the ListView.将height
添加到包装 ListView 的 Container。
To create a dynamic list you can use ListView.builder
and to show your data inside a card, Column
will help you.要创建动态列表,您可以使用ListView.builder
并在卡片中显示您的数据, Column
将为您提供帮助。 itemCount
will build productData.length number of cards. itemCount
将构建 productData.length 卡的数量。
Widget productsCard(){
return Container(
height: 200.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: productData.length,
itemBuilder: (BuildContext context, int i) =>
Card(
child: Container(
width: 160.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('Discipline curl'),
Text('https://sgdfgdgd/jdkjdhj.png/jashdghd'),
Text('20'),
Text('akhsgdahghsgdh')
],
),
),
),
)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.