我试图通过ListView中的索引号滚动到特定索引。我曾经使用过https://pub.dev/packages/indexed_list_view/example的IndexedListView构建器。但是我在“ itemCount:myData.length”中出错,因此无法在ListView中使用修复程序。
class MyHomePage extends StatelessWidget {
static IndexedScrollController controller =
IndexedScrollController(initialIndex: 0);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: IndexedListView.builder(
controller: controller,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
height: 500,
child: Center(child: Text('ITEM $index')),),);},),),
Container(height: 3.0, color: Colors.black),
Container(
color: Colors.grey[800],
child: Column(
children: <Widget>[
GestureDetector(onTap: () {controller.jumpToIndex(5);},
child: Container(
color: Colors.green,
width: MediaQuery.of(context).size.width,
height: 100,
child: Center(child: Text("Button to go to An specific index", style: TextStyle(color: Colors.white)),),),)],),),],),);}
}
答案 0 :(得分:0)
您可以在itemCount = your_list_length.length
内添加ListView.builder
child: ListView.builder(
controller: controller,
itemCount = your_list_length.length
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
height: 500,
child: Center(child:
ps:使用ListView
的答案,如果您使用的是itemCount
,则无需指定IndexedListView
答案 1 :(得分:0)
这是一个简单的ListView.builder
工作代码,请尝试一下
Widget _ExampleListView(BuildContext context) {
return ListView.builder(
padding: EdgeInsets.symmetric(vertical: 10.0),
itemCount: yourLength.length,
itemBuilder: (context, index) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
//what you want to do when tapping on each list
// tap each column to see the index
print(index);
},
child: Column(
children: [
Container(
padding: EdgeInsets.all(10),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('text1'),
Text('text2'),
],
),
],
),
),
],
),
);
},
);
}