Flutter GridView.builder 不可滚动
Flutter GridView.builder not scrollable
我有一个包含不同组件的屏幕,其中之一是 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 部分填充了图像,这很好,但它不可滚动。我可以向下滚动到它,但是一旦我到达这个部分,我就完全失去了滚动的能力,我也不知道为什么。
好的,我现在已经开始工作了:
@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,
childAspectRatio: 2,
),
itemBuilder: (BuildContext context, int index) {
return ListView(
physics: ScrollPhysics(),
children: <Widget>[
Image.network(
imageURLlist[index],
height: 150,
width: MediaQuery.of(context).size.width * 0.5,
fit: BoxFit.fill,
),
],
);
}
),
),
],
),
],
),
),
),
}
基本上,GridView.builder 和 ListView 子项都需要物理:ScrollPhysics() 否则它将不起作用。
希望这可以帮助其他感到困惑的人。
您可以复制粘贴 运行 下面的完整代码
您可以在 GridView
和 ListView
中使用 NeverScrollableScrollPhysics()
在您的情况下,您可以在没有 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,
),
],
);
}),
]))
])));
}
}
我有一个包含不同组件的屏幕,其中之一是 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 部分填充了图像,这很好,但它不可滚动。我可以向下滚动到它,但是一旦我到达这个部分,我就完全失去了滚动的能力,我也不知道为什么。
好的,我现在已经开始工作了:
@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,
childAspectRatio: 2,
),
itemBuilder: (BuildContext context, int index) {
return ListView(
physics: ScrollPhysics(),
children: <Widget>[
Image.network(
imageURLlist[index],
height: 150,
width: MediaQuery.of(context).size.width * 0.5,
fit: BoxFit.fill,
),
],
);
}
),
),
],
),
],
),
),
),
}
基本上,GridView.builder 和 ListView 子项都需要物理:ScrollPhysics() 否则它将不起作用。
希望这可以帮助其他感到困惑的人。
您可以复制粘贴 运行 下面的完整代码
您可以在 GridView
和 ListView
中使用 NeverScrollableScrollPhysics()
在您的情况下,您可以在没有 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,
),
],
);
}),
]))
])));
}
}