Flutter故事页面展示插件story_page_view的使用

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

Flutter 故事页面展示插件 story_page_view 的使用

特性

story_page_view 是一个高度可定制的 Flutter 实现,用于展示类似 Instagram 故事页面视图。它可以渲染一个页面视图,在给定的时间后自动翻到下一页。此外,还可以包含带有动画计时器的页面指示器。

开始使用

pubspec.yaml 文件中添加 story_page_view

flutter pub add story_page_view

使用方法

基本用法

该组件非常易于使用:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('StoryPageView 示例')),
        body: StoryPageView(
          children: [
            Container(color: Colors.red),
            Container(color: Colors.orange),
            Container(color: Colors.yellow),
            Container(color: Colors.green),
            Container(color: Colors.blue),
            Container(color: Colors.indigo),
            Container(color: Colors.purple),
          ],
        ),
      ),
    );
  }
}

更改故事持续时间

可以轻松更改每页的计时器时间:

StoryPageView(
  storyDuration: const Duration(seconds: 5),
)

程序化控制页面

PageView 类似,StoryPageView 使用 StoryPageController 来控制其行为。StoryPageController 是一种特殊的 PageController,它支持所有来自 PageController 的功能。

自定义分页动画

除了 PageControllerStoryPageController 还控制分页动画。可以通过 StoryPageController 自定义分页动画:

StoryPageView(
  controller: StoryPageController(
    pagingCurve: Curves.elasticOut,
    pagingDuration: const Duration(milliseconds: 2000),
  ),
)

自定义页面指示器样式

可以自定义页面指示器的样式:

StoryPageView(
  indicatorStyle: StoryPageIndicatorStyle(
    height: 6,
    gap: 12,
    unvisitedColor: Colors.blue.shade200,
    visitedColor: Colors.blue.shade900,
    timerBarBackgroundColor: Colors.blue.shade300, // 默认为 unvisitedColor
    timerBarColor: Colors.blue.shade700, // 默认为 visitedColor
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
)

将页面指示器作为覆盖层

StoryPageIndicatorPosition.overlay 用于 indicatorPosition 时,页面指示器将被渲染为浮动在内容顶部的覆盖层,并且可以通过绝对坐标来定位:

// 对齐到顶部中间
StoryPageView(
  indicatorPosition: StoryPageIndicatorPosition.overlay(
    top: 32,
    left: 12,
    right: 12,
  ),
)

// 或者对齐到底部中间
StoryPageView(
  indicatorPosition: StoryPageIndicatorPosition.overlay(
    bottom: 32,
    left: 12,
    right: 12,
  ),
)

自定义布局

实际上,该组件允许开发者通过使用 StoryPageIndicatorPosition.custom 来完全控制布局。需要声明预期布局的特殊构建函数,PageViewStoryPageIndicator 的实例将作为参数传递给构建函数。

例如,以下代码将页面指示器和页面视图放在一个列中:

StoryPageView(
  indicatorPosition: StoryPageIndicatorPosition.custom(
    layoutBuilder: (c, pageView, indicator) => SafeArea(
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
            child: indicator, // 页面指示器
          ),
          Expanded(
            child: pageView, // 页面视图
          ),
        ],
      ),
    ),
  ),
)

完全自定义的 StoryPageView

可以完全自定义 StoryPageView

StoryPageView(
  // 自定义指示器外观
  indicatorStyle: StoryPageIndicatorStyle(
    height: 6,
    gap: 12,
    unvisitedColor: Colors.blue.shade200,
    visitedColor: Colors.blue.shade900,
    timerBarBackgroundColor: Colors.blue.shade300, // 默认为 unvisitedColor
    timerBarColor: Colors.blue.shade700, // 默认为 visitedColor
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
  controller: StoryPageController(
    // 自定义分页动画样式
    pagingCurve: Curves.elasticOut,
    pagingDuration: const Duration(milliseconds: 2000),
  ),
  storyDuration: const Duration(seconds: 5),
  // 自定义整体布局
  indicatorPosition: StoryPageIndicatorPosition.custom(
    layoutBuilder: (c, pageView, indicator) => SafeArea(
      child: Column(
        children: [
          // 将页面指示器放置在页面上方
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
            child: indicator,
          ),
          Expanded(
            child: pageView,
          ),
        ],
      ),
    ),
  ),
  children: [
    Container(color: Colors.red),
    Container(color: Colors.orange),
    Container(color: Colors.yellow),
    Container(color: Colors.green),
    Container(color: Colors.blue),
    Container(color: Colors.indigo),
    Container(color: Colors.purple),
  ],
)

无页面指示器

有时开发者可能想要隐藏页面指示器,这可以通过使用 StoryPageIndicatorPosition.none 来实现:

StoryPageView(
  storyDuration: const Duration(seconds: 1),
  // 无页面指示器,仅显示计时器
  indicatorPosition: const StoryPageIndicatorPosition.none(),
  // 使视口宽度仅为屏幕宽度的 95%
  controller: StoryPageController(
    viewportFraction: 0.95,
  ),
  children: [
    Container(color: Colors.red),
    Container(color: Colors.orange),
    Container(color: Colors.yellow),
    Container(color: Colors.green),
    Container(color: Colors.blue),
    Container(color: Colors.indigo),
    Container(color: Colors.purple),
  ],
),

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

1 回复

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


当然,以下是一个关于如何在Flutter中使用story_page_view插件来创建故事页面展示的示例代码。story_page_view是一个专门用于创建类似Instagram、TikTok等应用中故事页面滑动效果的插件。

首先,你需要在你的pubspec.yaml文件中添加story_page_view依赖:

dependencies:
  flutter:
    sdk: flutter
  story_page_view: ^最新版本号  # 请替换为实际可用的最新版本号

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

接下来是一个完整的示例代码,展示如何使用story_page_view来创建一个故事页面:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StoryPageScreen(),
    );
  }
}

class StoryPageScreen extends StatefulWidget {
  @override
  _StoryPageScreenState createState() => _StoryPageScreenState();
}

class _StoryPageScreenState extends State<StoryPageScreen> {
  final List<String> storyImages = [
    'https://example.com/image1.jpg',
    'https://example.com/image2.jpg',
    'https://example.com/image3.jpg',
    // 添加更多图片URL
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: StoryPageView(
          // 设置故事页面的总数
          itemCount: storyImages.length,
          // 每一项的构建器
          itemBuilder: (context, index) {
            return Center(
              child: Image.network(
                storyImages[index],
                fit: BoxFit.cover,
                loadingBuilder: (context, widget, progress) {
                  if (progress == null) {
                    return widget;
                  }

                  return Stack(
                    alignment: Alignment.center,
                    children: <Widget>[
                      widget,
                      CircularProgressIndicator(
                        value: progress.expectedTotalBytes != null
                            ? progress.cumulativeBytesLoaded.toDouble() / progress.expectedTotalBytes!
                            : null,
                      ),
                    ],
                  );
                },
              ),
            );
          },
          // 控制器,用于控制故事页面的导航
          controller: StoryPageViewController(),
          // 顶部和底部的指示器设置
          indicatorSettings: IndicatorSettings(
            indicatorColor: Colors.white,
            indicatorSelectedColor: Colors.red,
            indicatorSize: 10.0,
            indicatorActivePadding: 2.0,
          ),
          // 顶部和底部的导航按钮设置
          buttonSettings: ButtonSettings(
            iconColor: Colors.white,
            backgroundColor: Colors.transparent,
          ),
          // 是否显示顶部和底部的导航按钮
          showButtons: true,
          // 是否允许滑动
          isSwipeEnabled: true,
          // 是否循环播放
          isLooping: true,
          // 页面切换动画时间
          pageChangeDuration: Duration(milliseconds: 300),
          // 页面切换动画曲线
          pageChangeCurve: Curves.easeInOut,
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们创建了一个StoryPageScreen,它是一个StatefulWidget。
  2. _StoryPageScreenState中,我们定义了一个包含图片URL的列表storyImages
  3. 我们使用StoryPageView来展示这些图片,通过itemBuilder属性来构建每一项的内容。
  4. 我们设置了指示器、按钮的样式以及是否显示这些元素,还设置了是否允许滑动和循环播放等属性。

请确保将storyImages列表中的URL替换为你自己的图片URL。这样,你就可以在Flutter应用中创建一个故事页面展示效果了。

回到顶部