Flutter SliverAppBar 和 SliverList 有不同的滚动
Flutter SliverAppBar and SliverList have different scrolls
[Initial Postion][1]
[Scrolling from SliverList side][2]
[Scrolling from SliverAppBar side][3]
class ProductScreen extends StatelessWidget {
static const routeName = "/product-screen";
@override
Widget build(BuildContext context) {
final _subCategory = Provider.of<SubCategoryProvider>(
context,
listen: false,
);
final _mediaQuery = MediaQuery.of(context);
return Scaffold(
body: SafeArea(
bottom: true,
top: true,
left: true,
right: true,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
expandedHeight: 200.0,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text("${_subCategory.currentSubCategoryName()}"),
background: Container(
margin: const EdgeInsets.only(
top: 4,
bottom: 50.0,
),
child: Image.asset(
'asset/images/grocery.jpeg',
),
),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
Column(
children: <Widget>[
Container(
height: _mediaQuery.size.height - kToolbarHeight,
color: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: _subCategory.differentProductCount(),
itemBuilder: (ctx, pdIndex) {
return Column(
children: <Widget>[
Container(
height: 30.0,
margin:
const EdgeInsets.symmetric(vertical: 5.0),
decoration: BoxDecoration(
border: Border.all(
width: 1.0,
color: Colors.black,
),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding:
const EdgeInsets.only(left: 8.0),
child: Text(
"${_subCategory.currentProductName(pdIndex)}",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
Icon(Icons.arrow_drop_down, size: 30.0),
],
),
),
Container(
height: 270.0 *
_subCategory
.differentCompanyCount(pdIndex),
child: ListView.builder(
physics: ClampingScrollPhysics(),
itemCount: _subCategory
.differentCompanyCount(pdIndex),
itemBuilder: (ctx, cyIndex) {
return Column(
children: <Widget>[
Container(
height: 250.0,
margin: const EdgeInsets.only(
bottom: 20.0),
color: cyIndex.isEven
? Colors.green
: Colors.pink),
],
);
},
)),
],
);
},
),
),
],
),
],
),
),
],
),
),
);
}
}
Sliver AppBar 有自己的滚动条,SliverList 有自己的滚动条。
如果我尝试从 sliver appbar 滚动,则只有两个滚动,但如果我从 sliver 列表滚动,则只有 sliver 列表滚动,并且 sliver appbar 具有完全展开的高度。
我曾尝试设置物理来移除内部滚动,但仍然有两种不同的滚动
问题是您在 SliverList 中创建了一个 Listview.builder,所以它看起来像一个嵌套的滚动条,请忽略该列和 ListView.builder 并仅使用 SliverChildBuilderDelegate
来创建项目按需
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int pdIndex) {
return Column(
children: [
//... The children inside the column of ListView.builder
]
);
}
childCount: _subCategory.differentProductCount(),
),
),
对于 Container 和 Bouncing 物理的颜色,您可以将它们分别添加到 Scaffold backgroundColor 和 CustomScrollView 物理中。
return Scaffold(
backgroundColor: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
body: SafeArea( //bottom, top, left and right are true by default so no need to add them
child: CustomScrollView(
physics: BouncingScrollPhysics(),
slivers: [
...
]
)
)
);
[Initial Postion][1]
[Scrolling from SliverList side][2]
[Scrolling from SliverAppBar side][3]
class ProductScreen extends StatelessWidget {
static const routeName = "/product-screen";
@override
Widget build(BuildContext context) {
final _subCategory = Provider.of<SubCategoryProvider>(
context,
listen: false,
);
final _mediaQuery = MediaQuery.of(context);
return Scaffold(
body: SafeArea(
bottom: true,
top: true,
left: true,
right: true,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
expandedHeight: 200.0,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text("${_subCategory.currentSubCategoryName()}"),
background: Container(
margin: const EdgeInsets.only(
top: 4,
bottom: 50.0,
),
child: Image.asset(
'asset/images/grocery.jpeg',
),
),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
Column(
children: <Widget>[
Container(
height: _mediaQuery.size.height - kToolbarHeight,
color: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: _subCategory.differentProductCount(),
itemBuilder: (ctx, pdIndex) {
return Column(
children: <Widget>[
Container(
height: 30.0,
margin:
const EdgeInsets.symmetric(vertical: 5.0),
decoration: BoxDecoration(
border: Border.all(
width: 1.0,
color: Colors.black,
),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding:
const EdgeInsets.only(left: 8.0),
child: Text(
"${_subCategory.currentProductName(pdIndex)}",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
Icon(Icons.arrow_drop_down, size: 30.0),
],
),
),
Container(
height: 270.0 *
_subCategory
.differentCompanyCount(pdIndex),
child: ListView.builder(
physics: ClampingScrollPhysics(),
itemCount: _subCategory
.differentCompanyCount(pdIndex),
itemBuilder: (ctx, cyIndex) {
return Column(
children: <Widget>[
Container(
height: 250.0,
margin: const EdgeInsets.only(
bottom: 20.0),
color: cyIndex.isEven
? Colors.green
: Colors.pink),
],
);
},
)),
],
);
},
),
),
],
),
],
),
),
],
),
),
);
}
}
Sliver AppBar 有自己的滚动条,SliverList 有自己的滚动条。
如果我尝试从 sliver appbar 滚动,则只有两个滚动,但如果我从 sliver 列表滚动,则只有 sliver 列表滚动,并且 sliver appbar 具有完全展开的高度。
我曾尝试设置物理来移除内部滚动,但仍然有两种不同的滚动
问题是您在 SliverList 中创建了一个 Listview.builder,所以它看起来像一个嵌套的滚动条,请忽略该列和 ListView.builder 并仅使用 SliverChildBuilderDelegate
来创建项目按需
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int pdIndex) {
return Column(
children: [
//... The children inside the column of ListView.builder
]
);
}
childCount: _subCategory.differentProductCount(),
),
),
对于 Container 和 Bouncing 物理的颜色,您可以将它们分别添加到 Scaffold backgroundColor 和 CustomScrollView 物理中。
return Scaffold(
backgroundColor: Color.fromARGB(0xff, 0xff, 0xcd, 0x3c),
body: SafeArea( //bottom, top, left and right are true by default so no need to add them
child: CustomScrollView(
physics: BouncingScrollPhysics(),
slivers: [
...
]
)
)
);