欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 【FlutterDart】页面切换 PageView PageController(9 /100)

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

2025/1/31 20:02:46 来源:https://blog.csdn.net/lichong951/article/details/144924094  浏览:    关键词:【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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com