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,
              ),
            ),
          );
        },
      ),
    );
  }
}
相关推荐
tangweiguo030519872 小时前
Flutter iOS 调试利器:idevicesyslog 从入门到精通
flutter
tangweiguo030519875 小时前
Flutter 异常捕获与处理:从入门到生产实践
flutter
不爱吃糖的程序媛6 小时前
已有 Flutter 应用适配鸿蒙平台指导文档
flutter·华为·harmonyos
weixin_443478516 小时前
flutter组件学习之卡片与列表
javascript·学习·flutter
不爱吃糖的程序媛6 小时前
Flutter-OH 升级指导
flutter
恋猫de小郭8 小时前
Android 禁止侧载将正式实施,需要等待 24 小时冷静期
android·flutter·harmonyos
FFF-X9 小时前
解决 Flutter Gradle 下载报错:修改默认 distributionUrl
flutter
程序员Ctrl喵1 天前
异步编程:Event Loop 与 Isolate 的深层博弈
开发语言·flutter
前端不太难1 天前
Flutter 如何设计可长期维护的模块边界?
flutter
小蜜蜂嗡嗡1 天前
flutter列表中实现置顶动画
flutter