需求
在 flutter 中,pageview 是一个非常常用的组件,能够实现多个页面的滑动切换。然而,默认的 pageview 高度是固定的,这在展示不同高度的页面时,可能会导致不必要的空白或内容裁剪问题。为了使 pageview 能够根据每个页面的内容高度动态调整,我们需要一个自适应高度的 pageview 实现。
效果

本方案的 pageview 可以根据每个页面内容的高度自动调整,保证每个页面的内容在其实际高度内完整显示,并且在页面滑动时,使用平滑的过渡效果。具体来说:
- 每个页面的内容高度不同,
pageview能够动态调整高度。 - 页面切换时,高度变化平滑,不会造成突兀的视觉效果。
实现思路
1. 测量每个页面的高度
首先,我们需要为每个页面添加一个高度测量的机制,测量页面内容的高度。在 flutter 中,我们可以通过 globalkey 和 renderbox 获取每个 widget 的实际高度。
2. 动态调整 pageview 高度
在页面滑动时,我们需要根据滑动的进度动态计算当前 pageview 的高度。这就需要在 pageview 的滑动过程中实时更新高度,使得高度随滑动位置逐步过渡。
3. 动态高度过渡
我们可以使用 animatedcontainer 来平滑过渡 pageview 的高度,避免切换时高度变化过于突兀。通过监听 pageview 的滑动状态,实时调整容器高度。
实现代码
1. 高度测量组件 heightmeasurewidget
这个组件负责测量每个页面的高度,并将测量结果通过回调传递出去。
import 'package:flutter/material.dart';
class heightmeasurewidget extends statefulwidget {
final widget child;
final function(double height) onheightchanged;
const heightmeasurewidget(
{super.key, required this.child, required this.onheightchanged});
@override
heightmeasurestate createstate() => heightmeasurestate();
}
class heightmeasurestate extends state<heightmeasurewidget> {
final globalkey _key = globalkey();
@override
void initstate() {
super.initstate();
widgetsbinding.instance.addpostframecallback((_) {
_measureheight();
});
}
void _measureheight() {
final renderbox? renderbox =
_key.currentcontext!.findrenderobject() as renderbox?;
if (renderbox != null) {
widget.onheightchanged(renderbox.size.height);
}
}
@override
widget build(buildcontext context) {
return container(
key: _key,
child: widget.child,
);
}
}2. 自适应高度的 autoheightpageview
这个组件使用了前面创建的 heightmeasurewidget 来测量每个页面的高度,然后根据滑动进度调整高度。
import 'package:flutter/material.dart';
import 'measure_height_widget.dart';
class autoheightpageview extends statefulwidget {
final list<widget> children;
final pagecontroller pagecontroller;
const autoheightpageview({
key? key,
required this.children,
required this.pagecontroller,
}) : super(key: key);
@override
autoheightpageviewstate createstate() => autoheightpageviewstate();
}
class autoheightpageviewstate extends state<autoheightpageview> {
final list<double> _heights = [];
double _currentheight = 0;
@override
void initstate() {
super.initstate();
widget.pagecontroller.addlistener(_updateheight);
}
void _updateheight() {
if (widget.pagecontroller.position.havedimensions && _heights.isnotempty) {
double page = widget.pagecontroller.page ?? 0.0;
int index = page.floor();
int nextindex = (index + 1) < _heights.length ? index + 1 : index;
double percent = page - index;
double height =
_heights[index] + (_heights[nextindex] - _heights[index]) * percent;
setstate(() {
_currentheight = height;
});
}
}
@override
widget build(buildcontext context) {
var ismeasureheight =
_heights.length == widget.children.length ? false : true;
return column(
children: [
stack(
children: [
visibility(
visible: ismeasureheight,
child: stack(
children: widget.children
.map((e) => heightmeasurewidget(
child: e,
onheightchanged: (height) {
_heights.add(height);
if (_heights.length == widget.children.length) {
setstate(() {
_currentheight = _heights[0];
});
}
},
))
.tolist(),
),
),
if (!ismeasureheight)
animatedcontainer(
duration: const duration(milliseconds: 200),
height: _currentheight,
curve: curves.easeout,
child: pageview(
controller: widget.pagecontroller,
children: widget.children,
),
),
],
)
],
);
}
@override
void dispose() {
widget.pagecontroller.dispose();
super.dispose();
}
}3. 使用示例 autoheightpageviewpage
该页面演示了如何使用自适应高度的 pageview,通过内容高度的动态调整,确保 pageview 始终适应当前页面的高度。
import 'package:flutter/material.dart';
import 'package:flutter_xy/r.dart';
import 'package:flutter_xy/xydemo/vp/pageview/auto_height_page_view.dart';
class autoheightpageviewpage extends statefulwidget {
const autoheightpageviewpage({key? key}) : super(key: key);
@override
state<statefulwidget> createstate() => autoheightpageviewstate();
}
class autoheightpageviewstate extends state<autoheightpageviewpage> {
final pagecontroller _pagecontroller = pagecontroller();
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(
title: text('自适用高度pageview'),
),
body: container(
color: colors.white,
child: singlechildscrollview(
child: column(
children: [
autoheightpageview(
pagecontroller: _pagecontroller,
children: [
container(
color: colors.white,
child: listview(
shrinkwrap: true,
physics: const neverscrollablescrollphysics(),
children: [
container(
color: colors.red,
height: 50,
alignment: alignment.center,
child: const text("第一个界面"),
),
container(
color: colors.yellow,
height: 50,
alignment: alignment.center,
child: const text("第一个界面"),
),
container(
color: colors.blue,
height: 50,
alignment: alignment.center,
child: const text("第一个界面"),
),
],
),
),
container(
color: colors.green,
height: 250,
child: const center(child: text('第二个界面'))),
],
),
image.asset(
r.vp_content_jpg,
width: mediaquery.of(context).size.width,
fit: boxfit.fill,
),
],
),
),
),
);
}
}总结
通过本方案,我们实现了一个自适应高度的 pageview,它能够根据每个页面的内容高度进行动态调整。该实现依赖于对每个页面内容的测量,并使用 animatedcontainer 来平滑地过渡高度变化。这样可以确保页面切换时,用户体验更加自然流畅,避免了内容被裁剪或空白区域的出现。这种自适应高度的 pageview 非常适合用于不同页面内容高度不一致的场景。
详情:github.com/yixiaolunhui/flutter_xy
到此这篇关于flutter自适用高度pageview的实现方案的文章就介绍到这了,更多相关flutter pageview内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论