Flutter页面指示器插件page_view_indicator的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter页面指示器插件page_view_indicator的使用

PageViewIndicator #

构建PageView的指示标记。

导入 #

import 'package:page_view_indicator/page_view_indicator.dart';

使用 #

默认Material行为 #

return PageViewIndicator(
  pageIndexNotifier: pageIndexNotifier, // 用于监听当前页面索引的通知器
  length: length, // 页面总数
  normalBuilder: (animationController, index) => Circle(
        size: 8.0, // 普通状态下的圆圈大小
        color: Colors.black87, // 普通状态下的颜色
      ),
  highlightedBuilder: (animationController, index) => ScaleTransition(
        scale: CurvedAnimation(
          parent: animationController, // 动画控制器
          curve: Curves.ease, // 动画曲线
        ),
        child: Circle(
          size: 12.0, // 高亮状态下的圆圈大小
          color: Colors.black45, // 高亮状态下的颜色
        ),
      ),
);

效果展示:


自定义动画 #

return PageViewIndicator(
  pageIndexNotifier: pageIndexNotifier,
  length: length,
  normalBuilder: (animationController, index) => Circle(
        size: 8.0,
        color: Colors.black87,
      ),
  highlightedBuilder: (animationController, index) => ScaleTransition(
        scale: CurvedAnimation(
          parent: animationController,
          curve: Curves.ease,
        ),
        child: Circle(
          size: 8.0,
          color: Colors.white,
        ),
      ),
);

效果展示:


自定义图标 #

不仅仅是点!

return PageViewIndicator(
  pageIndexNotifier: pageIndexNotifier,
  length: length,
  normalBuilder: (animationController, index) => ScaleTransition(
        scale: CurvedAnimation(
          parent: animationController,
          curve: Curves.ease,
        ),
        child: Icon(
          Icons.favorite, // 普通状态下的图标
          color: Colors.black87,
        ),
      ),
  highlightedBuilder: (animationController, index) => ScaleTransition(
        scale: CurvedAnimation(
          parent: animationController,
          curve: Curves.ease,
        ),
        child: Icon(
          Icons.star, // 高亮状态下的图标
          color: Colors.white,
        ),
      ),
);

效果展示:


修改指示器之间的间距 #

可以通过indicatorPadding属性更改指示器周围的间距:

return PageViewIndicator(
  pageIndexNotifier: pageIndexNotifier,
  length: length,
  indicatorPadding: const EdgeInsets.all(4.0), // 设置间距为4.0
  ...
);

默认值为const EdgeInsets.all(8.0)


完整示例代码

以下是一个完整的示例代码,展示了如何使用page_view_indicator插件:

import 'package:flutter/material.dart';
import 'package:page_view_indicator/page_view_indicator.dart';

class App extends StatelessWidget {
  static const length = 3; // 页面总数
  final pageIndexNotifier = ValueNotifier<int>(0); // 用于通知当前页面索引

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          alignment: FractionalOffset.bottomCenter,
          children: <Widget>[
            // 构建PageView
            PageView.builder(
              onPageChanged: (index) => pageIndexNotifier.value = index, // 更新当前页面索引
              itemCount: length,
              itemBuilder: (context, index) {
                return Container(
                  decoration: BoxDecoration(color: Colors.accents[index]), // 设置背景颜色
                );
              },
            ),
            // 示例1:默认样式
            _buildExample1(),
            // 示例2:自定义动画
            _buildExample2(),
            // 示例3:自定义图标
            _buildExample3(),
          ],
        ),
      ),
    );
  }

  // 示例1:默认样式
  PageViewIndicator _buildExample1() {
    return PageViewIndicator(
      pageIndexNotifier: pageIndexNotifier,
      length: length,
      normalBuilder: (animationController, index) => Circle(
            size: 8.0,
            color: Colors.black87,
          ),
      highlightedBuilder: (animationController, index) => ScaleTransition(
            scale: CurvedAnimation(
              parent: animationController,
              curve: Curves.ease,
            ),
            child: Circle(
              size: 12.0,
              color: Colors.accents.elementAt((index + 3) * 3),
            ),
          ),
    );
  }

  // 示例2:自定义动画
  PageViewIndicator _buildExample2() {
    return PageViewIndicator(
      pageIndexNotifier: pageIndexNotifier,
      length: length,
      normalBuilder: (animationController, index) => Circle(
            size: 8.0,
            color: Colors.black87,
          ),
      highlightedBuilder: (animationController, index) => ScaleTransition(
            scale: CurvedAnimation(
              parent: animationController,
              curve: Curves.ease,
            ),
            child: Circle(
              size: 8.0,
              color: Colors.white,
            ),
          ),
    );
  }

  // 示例3:自定义图标
  PageViewIndicator _buildExample3() {
    return PageViewIndicator(
      pageIndexNotifier: pageIndexNotifier,
      length: length,
      normalBuilder: (animationController, index) => ScaleTransition(
            scale: CurvedAnimation(
              parent: animationController,
              curve: Curves.ease,
            ),
            child: Icon(Icons.favorite, color: Colors.black87),
          ),
      highlightedBuilder: (animationController, index) => ScaleTransition(
            scale: CurvedAnimation(
              parent: animationController,
              curve: Curves.ease,
            ),
            child: Icon(Icons.star, color: Colors.white),
          ),
    );
  }
}

void main() => runApp(App());

更多关于Flutter页面指示器插件page_view_indicator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter页面指示器插件page_view_indicator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


page_view_indicator 是一个用于 Flutter 的页面指示器插件,通常与 PageView 配合使用,用于显示当前页面的位置。以下是如何使用 page_view_indicator 插件的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 page_view_indicator 依赖:

dependencies:
  flutter:
    sdk: flutter
  page_view_indicator: ^1.0.0

然后运行 flutter pub get 来安装依赖。

2. 导入包

在需要使用 page_view_indicator 的 Dart 文件中导入包:

import 'package:page_view_indicator/page_view_indicator.dart';

3. 创建 PageView 和 PageViewIndicator

接下来,创建一个 PageView 和一个 PageViewIndicator,并将它们关联起来。

import 'package:flutter/material.dart';
import 'package:page_view_indicator/page_view_indicator.dart';

class MyPageViewIndicator extends StatefulWidget {
  [@override](/user/override)
  _MyPageViewIndicatorState createState() => _MyPageViewIndicatorState();
}

class _MyPageViewIndicatorState extends State<MyPageViewIndicator> {
  final _pageController = PageController();
  final _pageIndexNotifier = ValueNotifier<int>(0);

  final List<Widget> _pages = [
    Container(color: Colors.red),
    Container(color: Colors.green),
    Container(color: Colors.blue),
    Container(color: Colors.yellow),
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PageView Indicator Example'),
      ),
      body: Column(
        children: [
          Expanded(
            child: PageView(
              controller: _pageController,
              onPageChanged: (index) {
                _pageIndexNotifier.value = index;
              },
              children: _pages,
            ),
          ),
          PageViewIndicator(
            pageIndexNotifier: _pageIndexNotifier,
            length: _pages.length,
            normalBuilder: (animationController, index) => Circle(
              size: 8.0,
              color: Colors.grey,
            ),
            highlightedBuilder: (animationController, index) => ScaleTransition(
              scale: CurvedAnimation(
                parent: animationController,
                curve: Curves.ease,
              ),
              child: Circle(
                size: 8.0,
                color: Colors.blue,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: MyPageViewIndicator(),
  ));
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!