【Flutter&Dart】页面切换 PageView &PageController(9 /100)

上效果:

有些不能理解官方例子里的动画为什么没有效果,有可能是我写法不对

后续如果有动画效果修复了,再更新这篇,没有动画效果,总觉得感受的丝滑效果差了很多

上代码:

dart 复制代码
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});

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

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

  @override
  createState() => _PageViewDemoState();
}

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

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

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

  @override
  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;

  @override
  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

相关推荐
我爱一根柴哈1 小时前
IOS开发如何从入门进阶到高级
ios
Batac_蝠猫1 小时前
iOS - 引用计数(ARC)
macos·ios·xcode
Batac_蝠猫1 小时前
iOS - 自旋锁
ios
0wioiw02 小时前
逆向安卓抓包
android·linux·运维
zhangjiaofa2 小时前
深入理解 Android 中的 KeyguardManager
android
-代号95272 小时前
云计算中的可用性SLA
android·java·云计算
m0_748230443 小时前
眼见不一定为实之MySQL中的不可见字符
android·数据库·mysql
2401_889271463 小时前
iPhone升级iOS18黑屏?2025最新修复办法分享
ios·cocoa·iphone
雪花凌落的盛夏3 小时前
历代iPhone运行内存大小和电池容量信息
ios·iphone
_可乐无糖4 小时前
深入理解 pytest_runtest_makereport:如何在 pytest 中自定义测试报告
android·ui·ios·自动化·pytest