flutter - Flutter GridView.builder 不可滚动
问题描述
我有一个包含不同组件的屏幕,其中一个是 GridView.builder,无论我做什么都不会滚动。
我尝试了不同的方法,但根本没有任何效果,所以我将分享我目前正在尝试的方法:
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Container(
child: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
[
//Some widgets here, some images, some buttons. This section scrolls perfectly.
],
SliverList(
delegate: SliverChildListDelegate(
[
GridView.builder(
//physics: ScrollPhysics(),
shrinkWrap: true,
itemCount: imageURLlist.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemBuilder: (BuildContext context, int index) {
return ListView(
children: <Widget>[
Image.network(
imageURLlist[index],
height: 150,
width: MediaQuery.of(context).size.width * 0.5,
fit: BoxFit.fill,
),
],
);
}
),
),
],
),
],
),
),
),
}
GridView 部分填充了图像,这很好,但它不可滚动。我可以向下滚动到它,但是一旦我到达这个部分,我就失去了完全滚动的能力,我不知道为什么。
解决方案
您可以在下面复制粘贴运行完整代码
您可以使用NeverScrollableScrollPhysics()
在您GridView
的ListView
情况下,您可以在没有的情况下工作ListView
GridView.builder(
physics: NeverScrollableScrollPhysics()
...
ListView(
physics: NeverScrollableScrollPhysics()
工作演示
完整代码
import 'package:flutter/material.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> {
List<String> imageURLlist = [
"https://picsum.photos/250?image=9",
"https://picsum.photos/250?image=10",
"https://picsum.photos/250?image=11",
"https://picsum.photos/250?image=12"
];
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Container(
child: CustomScrollView(slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
[
Container(color: Colors.red, height: 150.0),
Container(color: Colors.purple, height: 150.0),
Container(color: Colors.green, height: 150.0),
],
),
),
SliverList(
delegate: SliverChildListDelegate([
GridView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: imageURLlist.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemBuilder: (BuildContext context, int index) {
return ListView(
physics: NeverScrollableScrollPhysics(),
children: <Widget>[
Image.network(
imageURLlist[index],
height: 150,
width: MediaQuery.of(context).size.width * 0.5,
fit: BoxFit.fill,
),
],
);
}),
]))
])));
}
}
推荐阅读
- powershell - 在 PowerShell 中编辑(拆分)字符串?
- reactjs - 如何在 React 中实现延迟加载或滚动加载
- git - 有没有办法将所有 Git Repo 信息从一台机器复制到源树中的另一台机器
- assembly - 除了我的文本 Assembly x86 之外,还会出现“堆栈”
- xml - 如何组合元素和属性?
- java - 根据 Enum 值选择服务类型
- reactjs - 如何使用reactjs显示抽屉列表而不重叠?
- android - Android Sygic Embedded SDK doesn't show a map
- oracle - 请求超过了oracle中允许的最大数据库
- html - 在 div 中添加所需的验证器