使用Flutter能更容易通过Widget创建好看的一屏幕布局
但是如何创造一堆屏幕布局,并供你的使用者自由滑动,到底有多难呢?
如果你使用PageView Widget,就会发现这一点也不难
首先你必须使用PageController,它会管理滑动侦测并提供动画
final controller = PageController();
使用initialPage属性来设定开始的页面
final controller = PageController(
initialPage: 1,
);
然后就可以使用PageView来创建你的页面了,并且还为其能提供控制器以及页面来展示
final pageView = PageView(
controller: controller,
children: [
MyPage1Widger(),
MyPage2Widget(),
MyPage3Widget(),
],
);
如果想要上下滑动你的页面呢?
只要在scrollDirection设置垂直方向即可
final pageView = PageView(
controller: controller,
scrollDirection: Axis.vertical,
children: [
MyPage1Widger(),
MyPage2Widget(),
MyPage3Widget(),
],
);
使用PageView和PageController 你的使用者就可以在屏幕间自由滑动,一直滑到他们想要的内容
如果想了解有关PageView的内容,或者关于Flutter的其他功能,请访问flutter.io
原文翻译自视频:视频地址