如何滚动到屏幕外元素? ListView.builder使用分页,并且隐藏项未嵌入在树中
示例:
import 'package:flutter/material.dart';
class ScrollView1 extends StatefulWidget {
@override
_ScrollView1State createState() => _ScrollView1State();
}
class _ScrollView1State extends State<ScrollView1> {
final dataKey = new GlobalKey();
List<String> list1 = [
'button1',
'button2',
'button3',
'button4',
'button5',
'button6',
'button7'
];
List<GlobalKey> listKey = [];
List<GlobalKey> listKeyBody = [];
void addKey() {
for (var i = 0; i < list1.length; i++) {
final key = GlobalKey();
listKey.add(key);
final key2 = GlobalKey();
listKeyBody.add(key2);
}
}
@override
void initState() {
addKey();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: 100,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, i) {
return FlatButton(
key: listKey[i],
child: Text('${list1[i]} - $i ${listKeyBody[i]}'),
onPressed: () {
Scrollable.ensureVisible(listKeyBody[i].currentContext);
},
);
},
itemCount: list1.length,
),
),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, i) {
return Container(
key: listKeyBody[i],
height: 200,
child: Center(
child:
Text('Hello ${list1[i]} $i - ${listKeyBody[i]}')));
},
itemCount: list1.length,
),
),
],
),
);
}
}