[英]I want to add a scroll controller to the pageview so that I can hide the content like appbar that are above it
I want to add a scroll controller to the pageview (which can scroll horizontally as well as vertically) so that I can hide the content like appbar that are above it when I scroll down the contents of the pageview.我想在页面视图中添加一个滚动 controller (可以水平和垂直滚动),这样当我向下滚动页面视图的内容时,我可以隐藏其上方的 appbar 等内容。
I tried adding the Singlechildscrollview but it doesn't work properly don't know why.我尝试添加 Singlechildscrollview 但它不能正常工作不知道为什么。 It doesn't register the up down scrolls.它不注册上下滚动。
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int currentPage = 0;
bool isScrollingDown = false;
PageController _controller = PageController(
initialPage: 0,
);
ScrollController _scrollController;
@override
void initState() {
super.initState();
_initImages();
_scrollController = ScrollController();
_scrollController.addListener(() {
if (_scrollController.position.userScrollDirection ==
ScrollDirection.reverse) {
if (!isScrollingDown) {
setState(() {
isScrollingDown = true;
});
print(isScrollingDown);
}
}
if (_scrollController.position.userScrollDirection ==
ScrollDirection.forward) {
if (isScrollingDown) {
setState(() {
isScrollingDown = false;
});
print(isScrollingDown);
}
}
});
}
@override
void dispose() {
_controller.dispose();
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
),
body: Container(
color: Colors.black,
child: Column(
children: [
AnimatedContainer(
height: isScrollingDown ? 0 : height * 0.25,
duration: Duration(milliseconds: 400),
child: Column(
children: [
CircleAvatar(
backgroundImage: AssetImage("assets/profile.jpg"),
),
SizedBox(
height: 10,
),
Text(
"Samantha Smith",
style: profileText(),
),
Text(
"@imsamanthasmith",
style: profileValues(),
),
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
Text(
"1.2m",
style: profileStats(),
),
Text(
"Liked",
style: profileValues(),
)
],
),
Column(
children: [
Text(
"12.8k",
style: profileStats(),
),
Text(
"Followers",
style: profileValues(),
)
],
),
Column(
children: [
Text(
"1.9k",
style: profileStats(),
),
Text(
"Following",
style: profileValues(),
)
],
),
],
)
],
),
),
SizedBox(
height: 30,
),
Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20))),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
icon: currentPage == 0
? Icon(Icons.grid_on_rounded)
: Icon(Icons.grid_view),
onPressed: () {
_controller.animateToPage(0,
duration: Duration(milliseconds: 50),
curve: Curves.bounceOut);
},
),
IconButton(
icon: currentPage == 1
? Icon(Icons.favorite)
: Icon(Icons.favorite_border),
onPressed: () {
_controller.animateToPage(1,
duration: Duration(milliseconds: 50),
curve: Curves.bounceOut);
},
),
IconButton(
icon: currentPage == 2
? Icon(Icons.bookmark)
: Icon(Icons.bookmark_outline),
onPressed: () {
_controller.animateToPage(2,
duration: Duration(milliseconds: 50),
curve: Curves.bounceOut);
},
),
],
),
),
],
),
Expanded(
child: PageView(
physics: BouncingScrollPhysics(),
controller: _controller,
onPageChanged: (value) {
setState(() {
currentPage = value;
});
},
children: [
Page1(foodImages),
Page2(danceImages),
Page3(lolImages),
],
),
)
],
),
),
);
}
}
You can use CustomScrollView instead of using Scaffold, so you can use SliverAppBar which does have properties which you want to do like hiding/appearing on scroll etc. Here is the link of detailed information about SliverAppBar https://api.flutter.dev/flutter/material/SliverAppBar-class.html您可以使用 CustomScrollView 而不是使用 Scaffold,因此您可以使用 SliverAppBar,它确实具有您想要执行的属性,例如在滚动上隐藏/显示等。这里是有关 SliverAppBar https://api.flutter.dev/的详细信息的链接颤振/材料/SliverAppBar-class.html
Official doc from Flutter about implementing SliverAppBar here https://flutter.dev/docs/cookbook/lists/floating-app-bar#2-use-sliverappbar-to-add-a-floating-app-bar来自 Flutter 关于在此处实现 SliverAppBar 的官方文档https://flutter.dev/docs/cookbook/lists/floating-app-bar#2-use-sliverappbar-to-add-a-floating-app-bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.