尝试使用 sticky header 环绕 GridView.builder 小部件,因为当用户向下滚动屏幕时,我希望有一个标题跟随,但得到“NoSuchMethodError: The getter 'position' was called on null. Receiver: null 尝试调用: position 另见: https://flutter.dev/docs/testing/errors". 我使用这个相同的小部件来包裹一个 Column 小部件。我尝试用 Container、Column 和其他小部件包裹 GridView。我只会得到其他错误。这个错误是什么意思?
return _stickyHeader.render(
headerChild: Text(''),
content: GridView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
),
itemCount: state.avatarUrlList.length,
itemBuilder: (BuildContext context, int index) {
if (index == state.currentSelectedAvatarIndex) {
return Center(
child: InkWell(
onTap: () => _onAvatarImageClicked(
state.avatarUrlList[index], index),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.red,
width: 4.0,
),
),
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.transparent,
backgroundImage:
NetworkImage(state.avatarUrlList[index]),
),
),
),
);
} else {
return Center(
child: InkWell(
onTap: () => _onAvatarImageClicked(
state.avatarUrlList[index], index),
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.transparent,
backgroundImage:
NetworkImage(state.avatarUrlList[index]),
),
),
);
}
},
),
);
}
Widget render({Widget headerChild, Widget content}) {
return StickyHeader(
header: Container(
decoration: BoxDecoration(
color: ThemeConstants.ceruleanCrayola,
borderRadius: BorderRadius.circular(15),
),
height: 50.0,
padding: EdgeInsets.symmetric(horizontal: 16.0),
margin: EdgeInsets.symmetric(horizontal: 20.0),
alignment: Alignment.centerLeft,
child: headerChild,
),
content: content,
);
}
答案 0 :(得分:0)
基于https://pub.dev/packages/sticky_headers
<块引用>您可以在任何可滚动内容中放置 StickyHeader 或 StickyHeaderBuilder,例如:ListView、GridView、CustomScrollView、SingleChildScrollView 或类似内容。
您可能需要尝试将 StickyHeader
设置为 SingleChildScrollView
以进行基本操作。