当前位置: 首页 > article >正文

【FlutterDart】页面切换 PageView PageController(9 /100)

上效果:

在这里插入图片描述
有些不能理解官方例子里的动画为什么没有效果,有可能是我写法不对
后续如果有动画效果修复了,再更新这篇,没有动画效果,总觉得感受的丝滑效果差了很多

上代码:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';

const TAG = 'OfficePageViewDemo';

class OfficePageViewDemo extends StatelessWidget {
  const OfficePageViewDemo({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PageView Demo!'),
        ),
        body: PageViewDemo(),
      ),
    );
  }
}

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

  
  createState() => _PageViewDemoState();
}

class _PageViewDemoState extends State<PageViewDemo>
    with TickerProviderStateMixin {
  late PageController _pageViewController;
  late TabController _tabController;
  int _currentPageIndex = 0;

  
  void initState() {
    super.initState();
    _pageViewController = PageController();
    _tabController = TabController(length: 3, vsync: this);
  }

  
  void dispose() {
    super.dispose();
    _pageViewController.dispose();
    _tabController.dispose();
  }

  
  Widget build(BuildContext context) {
    final TextTheme textTheme = Theme.of(context).textTheme;

    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        PageView(
          controller: _pageViewController,
          onPageChanged: _handlePageViewChanged,
          children: [
            Center(
              child: Text(
                'First Page',
                style: textTheme.titleLarge,
              ),
            ),
            Center(
              child: Text(
                'Second Page',
                style: textTheme.titleLarge,
              ),
            ),
            Center(
              child: Text(
                'Third Page',
                style: textTheme.titleLarge,
              ),
            ),
          ],
        ),
        PageIndicator(
          tabController: _tabController,
          currentPageIndex: _currentPageIndex,
          onUpdateCurrentPageIndex: _updateCurrentPageIndex,
          isOnDesktopAndWeb: _isOnDesktopAndWeb,
        )
      ],
    );
  }

  void _handlePageViewChanged(int currentPageIndex) {
    Logger(TAG).info(
        '_handlePageViewChanged called! currentPageIndex=$currentPageIndex');

    if (!_isOnDesktopAndWeb) {
      return;
    }
    _tabController.index = currentPageIndex;
    setState(() {
      _currentPageIndex = currentPageIndex;
    });
  }

  void _updateCurrentPageIndex(int index) {
    Logger(TAG).info('_updateCurrentPageIndex called! index=$index');

    _tabController.index = index;
    _pageViewController.animateToPage(index,
        duration: const Duration(microseconds: 400 * 2), curve: Curves.linear);
  }

  bool get _isOnDesktopAndWeb {
    if (kIsWeb) {
      return true;
    }
    switch (defaultTargetPlatform) {
      case TargetPlatform.macOS:
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        return true;
      case TargetPlatform.android:
      case TargetPlatform.iOS:
      case TargetPlatform.fuchsia:
        return false;
    }
  }
}

class PageIndicator extends StatelessWidget {
  const PageIndicator(
      {super.key,
      required this.tabController,
      required this.currentPageIndex,
      required this.onUpdateCurrentPageIndex,
      required this.isOnDesktopAndWeb});

  final int currentPageIndex;
  final TabController tabController;
  final void Function(int) onUpdateCurrentPageIndex;
  final bool isOnDesktopAndWeb;

  
  Widget build(BuildContext context) {
    if (!isOnDesktopAndWeb) {
      return const SizedBox();
    }
    final ColorScheme colorScheme = Theme.of(context).colorScheme;

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          IconButton(
              splashRadius: 16.0,
              padding: EdgeInsets.zero,
              onPressed: () => {
                    if (currentPageIndex == 0)
                      {}
                    else
                      {onUpdateCurrentPageIndex(currentPageIndex - 1)}
                  },
              icon: const Icon(
                Icons.arrow_left_rounded,
                size: 32.0,
              )),
          TabPageSelector(
            controller: tabController,
            color: colorScheme.surface,
            selectedColor: colorScheme.primary,
          ),
          IconButton(
              splashRadius: 16.0,
              padding: EdgeInsets.zero,
              onPressed: () => {
                    if (currentPageIndex == 2)
                      {}
                    else
                      {onUpdateCurrentPageIndex(currentPageIndex + 1)}
                  },
              icon: const Icon(
                Icons.arrow_right_rounded,
                size: 32.0,
              )),
        ],
      ),
    );
  }
}


事实就是官方代码,不过有点细微差别
有解决动画效果的回复一下蛤!

还差一个拖动边界框改变 widget 的宽高效果;

========END


http://www.kler.cn/a/469096.html

相关文章:

  • 【C++面向对象——类的多态性与虚函数】编写教学游戏:认识动物(头歌实践教学平台习题)【合集】
  • Qt 5.14.2 学习记录 —— 삼 初级认识
  • Mac软件介绍之录屏软件Filmage Screen
  • python 如何调整word 文档页眉页脚
  • Synthesia技术浅析(二):虚拟人物视频生成
  • 【微服务】2、网关
  • 常用的数据结构API概览
  • LeetCode -Hot100 - 73. 矩阵置零
  • 瑞吉外卖项目学习笔记(十)修改套餐、删除套餐、起售和停售套餐
  • 云原生监控与日志管理:确保云原生应用的可靠性与性能
  • Spring MVC和servlet
  • 【2025最新计算机毕业设计】基于SSM的医院挂号住院系统(高质量源码,提供文档,免费部署到本地)【提供源码+答辩PPT+文档+项目部署】
  • 西安电子科技大学初/复试笔试、面试、机试成绩占比
  • 初学stm32 --- RTC实时时钟
  • Pytest钩子函数,测试框架动态切换测试环境
  • 《Rust权威指南》学习笔记(二)
  • Node.js中使用Joi 和 express-joi-validation进行数据验证和校验
  • Win32汇编学习笔记04.重定位与汇编引擎
  • ubuntu 创建服务、查看服务日志
  • Linux-Ubuntu之RTC实时时钟显示
  • 使用 ThinkPHP 和 Vue.js 开发现代 Web 应用的指南
  • Swift Protocols(协议)、Extensions(扩展)、Error Handling(错误处理)、Generics(泛型)
  • c++第13课
  • VisualRules规则引擎语法介绍
  • Structured-Streaming集成Kafka
  • vue3组件化开发优势劣势分析,及一个案例