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

【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.lryc.cn/news/515925.html

相关文章:

  • Backend - C# 的日志 NLog日志
  • Flask是什么?深入解析 Flask 的设计与应用实践
  • malloc函数和calloc函数的区别是什么?
  • Ansys Maxwell:3PH 变压器电感计算
  • 【Go】Go文件操作详解
  • [react+ts] useRef获取自定义组件dom或方法声明
  • AI 将在今年获得“永久记忆”,2028美国会耗尽能源储备
  • 【视频笔记】基于PyTorch从零构建多模态(视觉)大模型 by Umar Jamil【持续更新】
  • 解决 C++ 中头文件相互引用和解耦问题
  • 河马剧场(短剧)APP的邀请码怎么填写
  • 01:C语言的本质
  • 第1章:数据库基础
  • C++教程 | string类的定义和初始化方法
  • React中的合成事件
  • [SMARTFORMS] 创建FORM
  • 成都和力九垠科技有限公司九垠赢系统Common存在任意文件上传漏洞
  • 基于Python的考研学习系统
  • 『SQLite』几种向表中插入数据的方法
  • 什么是Kafka的重平衡机制?
  • pdf预览 报:Failed to load module script
  • AI 角色扮演法的深度剖析与实践
  • weblogic问题
  • Qt仿音乐播放器:客户端唯一化
  • ceph文件系统
  • 【数据结构-堆】力扣2530. 执行 K 次操作后的最大分数
  • Java jdk8新特性:Stream 流
  • 房产销售系统(源码+数据库+文档)
  • Vue 项目自动化部署:Coding + Jenkins + Nginx 实践分享
  • 从零开始开发纯血鸿蒙应用之实现起始页
  • CG顶会论文阅读|《科技论文写作》硕士课程报告