import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController _sTabController;
List<GlobalKey> keyList = <GlobalKey>[];
@override
void initState() {
keyList.clear();
_sTabController = ScrollController();
WidgetsBinding.instance.addPostFrameCallback((_) {
readyOfWidget();
});
for (int i = 0; i < 25; i++) {
keyList.add(GlobalKey(debugLabel: 'item_$i'));
}
super.initState();
}
void readyOfWidget() {
for (int i = 0; i < keyList.length; i++) {
RenderBox tabKeyItemRenderBox =
keyList[i].currentContext.findRenderObject();
if (tabKeyItemRenderBox != null) {
Offset positionOfRenderBox =
tabKeyItemRenderBox.localToGlobal(Offset.zero);
print('keyItemRenderBox dx {$i}: ${positionOfRenderBox.dx}');
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
// height: 60,
child: ListView(
controller: _sTabController,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: List.generate(keyList.length, (index) {
return Wrap(
children: [
Container(
key: keyList.elementAt(index),
color: index % 2 == 0 ? Colors.red : Colors.green,
child: Padding(
padding: EdgeInsets.only(top: 10,bottom: 10,left: 40,right: 40),
child: Text(
'Text $index',
style: TextStyle(fontSize: 21),
)),
)
],
);
}),
),
),
);
}
}
输出
I / flutter(5864):keyItemRenderBox dx {0}:0.0
I / flutter(5864):keyItemRenderBox dx {1}:77.0
I / flutter(5864):keyItemRenderBox dx {2}:154.0
I / flutter(5864):keyItemRenderBox dx {3}:231.0
I / flutter(5864):keyItemRenderBox dx {4}:308.0
I / flutter(5864):keyItemRenderBox dx {5}:385.0
I / flutter(5864):keyItemRenderBox dx {6}:462.0
I / flutter(5864):keyItemRenderBox dx {7}:539.0
I / flutter(5864):keyItemRenderBox dx {8}:616.0
════════调度程序库捕获了异常══════════════════════════════════ ═══════════════════
方法'findRenderObject'在null上被调用。
收件人:空
尝试调用:findRenderObject()
═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══
也可以尝试以下
padding: EdgeInsets.only(top: 10,bottom: 10,left: 40,right: 40),
输出
I / flutter(7007):keyItemRenderBox dx {0}:0.0
I / flutter(7007):keyItemRenderBox dx {1}:137.0
I / flutter(7007):keyItemRenderBox dx {2}:274.0
I / flutter(7007):keyItemRenderBox dx {3}:411.0
I / flutter(7007):keyItemRenderBox dx {4}:548.0
════════调度程序库捕获了异常══════════════════════════════════ ═══════════════════
方法'findRenderObject'在null上被调用。
收件人:空
尝试调用:findRenderObject()
═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══
是否可以获取所有水平列表项的findRenderObject()?
答案 0 :(得分:0)
之所以发生这种情况,是因为尚未渲染项目(第9版以上)。 ListView进行延迟渲染。
因此,如果要实现此目标,则需要轴为“ Axis.horizontal”的“ SingleChildScrollView”。