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,
              ),
            ),
          );
        },
      ),
    );
  }
}
相关推荐
云诗卡达7 小时前
Flutter安卓APP接入极光推送和本地通知
android·flutter
南村群童欺我老无力.7 小时前
Flutter应用鸿蒙迁移实战:性能优化与渐进式迁移指南
javascript·flutter·ci/cd·华为·性能优化·typescript·harmonyos
LawrenceLan7 小时前
Flutter 零基础入门(十二):枚举(enum)与状态管理的第一步
开发语言·前端·flutter·dart
程序员老刘·9 小时前
重拾Eval能力:D4rt为Flutter注入AI进化基因
人工智能·flutter·跨平台开发·客户端开发
kirk_wang10 小时前
Flutter艺术探索-Flutter响应式设计:MediaQuery与LayoutBuilder
flutter·移动开发·flutter教程·移动开发教程
SoaringHeart11 小时前
Flutter最佳实践:路由弹窗终极版NSlidePopupRoute
前端·flutter
kirk_wang14 小时前
Flutter艺术探索-Flutter自定义组件:组合与封装技巧
flutter·移动开发·flutter教程·移动开发教程
消失的旧时光-194315 小时前
BLoC vs Riverpod:命令式系统 与 声明式系统的两条架构路线
flutter·架构
奋斗的小青年!!16 小时前
Flutter跨平台开发适配OpenHarmony:下拉刷新组件的实战优化与深度解析
flutter·harmonyos·鸿蒙
摘星编程16 小时前
Flutter for OpenHarmony 实战:CustomScrollView 自定义滚动视图详解
android·javascript·flutter