我在FutureBuilder和Wrap小部件中有一个ListView,该ListView有n个项目要显示。所有内容都从我的API中恢复并正确显示,但是当向下滚动并放开鼠标/手指时,ListView将迅速恢复到列表的顶部,但我希望它保持在原来的位置。
我还没有真正遇到任何网上解决这一问题,据我所知,我这样做的标准方式。
更新这是我的整个小工具,我注解掉了我正在使用的listview
,并添加了一个基本的listview
,仅计数到200个项目。问题仍然存在。
// ... Imports ...
class HomeScreen extends ConsumerStatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
ConsumerState<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends ConsumerState<HomeScreen> {
http.Client client = http.Client();
String _eventStatusDisplay = "accepted";
@override
Widget build(BuildContext context) {
final user = ref.watch(userProvider);
return Scaffold(
body: Align(
alignment: Alignment.topCenter,
child: FutureBuilder(
future: user.fetchUserEvents(client, dotenv.env['BASE_API_URL']!, user.uid),
builder: (context, snapshot) {
return Wrap(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
onPressed: () {
print("See Accepted");
setState(() {
_eventStatusDisplay = "accepted";
});
},
style: _eventStatusDisplay == "accepted"
? TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.onBackground, foregroundColor: Theme.of(context).colorScheme.background)
: TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.background, foregroundColor: Theme.of(context).colorScheme.onBackground),
child: const Text("Accepted"),
),
TextButton(
onPressed: () {
print("See Pending");
setState(() {
_eventStatusDisplay = "pending";
});
},
style: _eventStatusDisplay == "pending"
? TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.onBackground, foregroundColor: Theme.of(context).colorScheme.background)
: TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.background, foregroundColor: Theme.of(context).colorScheme.onBackground),
child: const Text("Pending"),
),
TextButton(
onPressed: () {
print("See Passed");
setState(() {
_eventStatusDisplay = "declined";
});
},
style: _eventStatusDisplay == "declined"
? TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.onBackground, foregroundColor: Theme.of(context).colorScheme.background)
: TextButton.styleFrom(backgroundColor: Theme.of(context).colorScheme.background, foregroundColor: Theme.of(context).colorScheme.onBackground),
child: const Text("Declined"),
),
],
),
ListView.builder(
shrinkWrap: true,
itemCount: 200,
itemBuilder: (BuildContext context, int index) {
return Text(index.toString());
},
),
// ListView.builder(
// shrinkWrap: true,
// itemCount: user.events[_eventStatusDisplay]?.length,
// itemBuilder: (BuildContext context, int index) {
// return GestureDetector(
// onTap: () {
// Navigator.pushNamed(context, SingleEventScreen.routeName, arguments: user.events[_eventStatusDisplay]![index]);
// },
// child: Card(
// elevation: 0,
// color: Colors.white,
// margin: const EdgeInsets.all(10.0),
// child: Row(
// mainAxisAlignment: MainAxisAlignment.start,
// children: [
// CircleAvatar(
// radius: 30,
// child: ClipOval(
// child: FutureBuilder(
// future: user.events[_eventStatusDisplay]?[index].prepareEventPhotoURL(client),
// builder: (context, snapshot) {
// switch (snapshot.connectionState) {
// case ConnectionState.none:
// case ConnectionState.waiting:
// return const Icon(Icons.photo);
// default:
// if (snapshot.hasError || !snapshot.hasData) {
// return const Icon(Icons.photo);
// }
// return CachedNetworkImage(
// fit: BoxFit.cover,
// width: 80,
// height: 80,
// imageUrl: snapshot.data.toString(),
// errorWidget: (context, url, error) => const Icon(Icons.photo),
// placeholder: (context, url) => const Icon(Icons.photo),
// );
// }
// },
// ),
// ),
// ),
// Expanded(
// child: Container(
// padding: const EdgeInsets.only(left: 20.0, top: 10, right: 20.0, bottom: 10.0),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// user.events[_eventStatusDisplay]![index].event.title!,
// maxLines: 1,
// softWrap: false,
// overflow: TextOverflow.ellipsis,
// style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
// ),
// Text(
// user.events[_eventStatusDisplay]![index].event.description!,
// maxLines: 2,
// softWrap: false,
// overflow: TextOverflow.ellipsis,
// style: const TextStyle(fontSize: 14),
// ),
// ],
// ),
// ),
// ),
// ],
// ),
// ),
// );
// },
// ),
],
);
},
),
),
appBar: AppBar(
automaticallyImplyLeading: false,
elevation: 0.0,
backgroundColor: Colors.white,
),
bottomNavigationBar: const BottomNavBar(),
floatingActionButton: const NewEventButton(),
);
}
}
1条答案
按热度按时间hi3rlvi21#
我不知道为什么这个工作(将张贴另一个问题询问它),但切换
Wrap
的Expanded
修复了它