Flutter中PageView内卡片列表无法垂直滚动问题
Flutter PageView内ListView无法垂直滚动的解决方法
你的问题根源很明确:每个ListView.builder都设置了physics: NeverScrollableScrollPhysics(),这个配置直接禁用了ListView的垂直滚动功能,所以才没法在单个PageView页面内滚动卡片列表。
ListView本身就是自带垂直滚动能力的组件,完全不需要额外套SingleChildScrollView,反而多套会引发滚动冲突。只需要把所有ListView里的physics: NeverScrollableScrollPhysics()这一行删掉即可,修改后的代码如下:
PageView( controller: swipeController, children: [ //Conditions Cards for sun/clouds and rain probability Consumer<Settings>( builder: (context, weatherList, child) { return ListView.builder( // 移除NeverScrollableScrollPhysics(),恢复默认滚动行为 itemCount: weatherList.weatherLocations.length, itemBuilder: (context, index) { return WeatherCard( lat: weatherList.weatherLocations[index].lat, long: weatherList.weatherLocations[index].lng, name: weatherList.weatherLocations[index].formattedAddress, forcastType: 'conditions', lastUpdate: weatherList.weatherLocations[index].lastUpdate, ); }, ); }, ), // Temperature Cards Consumer<Settings>( builder: (context, weatherList, child) { return ListView.builder( itemCount: weatherList.weatherLocations.length, itemBuilder: (context, index) { return WeatherCard( lat: weatherList.weatherLocations[index].lat, long: weatherList.weatherLocations[index].lng, name: weatherList.weatherLocations[index].formattedAddress, forcastType: 'temperature', lastUpdate: weatherList.weatherLocations[index].lastUpdate, ); }, ); }, ), Consumer<Settings>( builder: (context, weatherList, child) { return ListView.builder( itemCount: weatherList.weatherLocations.length, itemBuilder: (context, index) { return WeatherCard( lat: weatherList.weatherLocations[index].lat, long: weatherList.weatherLocations[index].lng, name: weatherList.weatherLocations[index].formattedAddress, forcastType: 'windSpeed', lastUpdate: weatherList.weatherLocations[index].lastUpdate, ); }, ); }, ), ], ),
修改后,PageView负责水平滑动切换不同页面,每个页面内的ListView负责垂直滚动卡片列表,两者滚动方向不同,不会产生冲突,就能实现你想要的效果了。
内容的提问来源于stack exchange,提问作者Ten Digit Grid
相关产品推荐
相关产品推荐

