flutter PageView:竖直方向滑动,并且在屏幕中提前显示出下一页的四分之一

dart 复制代码
import 'package:flutter/material.dart';

class VerticalPageViewDemo extends StatefulWidget {
  const VerticalPageViewDemo({super.key});

  @override
  State<VerticalPageViewDemo> createState() => _VerticalPageViewDemoState();
}

class _VerticalPageViewDemoState extends State<VerticalPageViewDemo> {
  late PageController _pageController;
  int _currentPageIndex = 0;
  double _viewportFraction = 0.75; // 初始视口占比

  List<Color> pageColors = const [
    Colors.redAccent,
    Colors.greenAccent,
    Colors.blueAccent,
    Colors.yellowAccent,
    Colors.purpleAccent,
  ];

  @override
  void initState() {
    super.initState();
    // 初始化页面控制器
    _pageController = PageController(
      viewportFraction: _viewportFraction,
    );
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  // 动态切换视口占比
  void _updateViewportFraction(int newIndex) {
    int lastPageIndex = pageColors.length - 1;
    double targetFraction = newIndex == lastPageIndex ? 1.0 : 0.75;

    if (_viewportFraction != targetFraction) {
      setState(() {
        _viewportFraction = targetFraction;
      });

      // 重新创建控制器并保持当前页面位置,避免跳动
      _pageController.dispose();
      _pageController = PageController(
        viewportFraction: _viewportFraction,
        initialPage: newIndex,
      );

      // 平滑滚动到当前页面(可选,增强体验)
      WidgetsBinding.instance.addPostFrameCallback((_) {
        _pageController.animateToPage(
          newIndex,
          duration: const Duration(milliseconds: 200),
          curve: Curves.ease,
        );
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    double screenHeight = MediaQuery.of(context).size.height;
    int lastPageIndex = pageColors.length - 1;

    return Scaffold(
      appBar: AppBar(title: const Text('最后一页占比为1的PageView')),
      body: PageView.builder(
        controller: _pageController,
        padEnds: false,
        allowImplicitScrolling: true,
        scrollDirection: Axis.vertical,
        itemCount: pageColors.length,
        onPageChanged: (index) {
          setState(() {
            _currentPageIndex = index;
          });
          // 页面切换时更新视口占比
          _updateViewportFraction(index);
        },
        itemBuilder: (context, index) {
          bool isLastPage = index == lastPageIndex;

          return Container(
            margin: const EdgeInsets.symmetric(vertical: 8),
            decoration: BoxDecoration(
              color: pageColors[index],
              borderRadius: BorderRadius.circular(16),
              boxShadow: [
                BoxShadow(
                  color: Colors.black12,
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                )
              ],
            ),
            alignment: Alignment.center,
            child: Text(
              '第 ${index + 1} 页${isLastPage ? "(占比1)" : "(占比0.75)"}',
              style: const TextStyle(
                fontSize: 32,
                color: Colors.white,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    );
  }
}
相关推荐
音浪豆豆_Rachel2 小时前
Flutter鸿蒙化之深入解析Pigeon基本数据类型:primitive.dart全解
flutter·华为·harmonyos
翻斗花园岭第一爆破手2 小时前
flutter学习1
学习·flutter
kirk_wang2 小时前
鸿蒙与Flutter移动开发
flutter·移动开发·跨平台·arkts·鸿蒙
搬砖的kk3 小时前
Flutter适配鸿蒙:跨平台力量为鸿蒙生态注入增长新动能
分布式·flutter·harmonyos
西西学代码3 小时前
Flutter---轮播图
flutter
武玄天宗3 小时前
第五章、flutter怎么创建底部底部导航栏界面
前端·flutter
子榆.3 小时前
Flutter 与开源鸿蒙(OpenHarmony)生物识别实战:人脸 + 指纹双模认证,筑牢信创应用安全防线
flutter·开源·harmonyos
子榆.4 小时前
Flutter 与开源鸿蒙(OpenHarmony)离线地图与定位实战:无网络也能精准导航
flutter·开源·harmonyos
音浪豆豆_Rachel5 小时前
Flutter鸿蒙文件选择器进阶解析:多图选择的实现
flutter·华为·harmonyos