Flutter故事视图展示插件stories_view的使用

Flutter故事视图展示插件stories_view的使用

特点

TODO: 列出您的包可以做什么。可能包括图片、GIF或视频。

开始使用

TODO: 列出先决条件并提供或指向有关如何开始使用该包的信息。

使用方法

TODO: 包括用于包用户的简短且有用的示例。将较长的示例添加到/example文件夹。

const like = 'sample';

额外信息

TODO: 告诉用户更多关于该包的信息:在哪里找到更多信息,如何为该包做出贡献,如何提交问题,用户可以期望从包作者那里得到什么响应等。


示例代码

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:mime/mime.dart';
import 'package:stories_view/stories_view.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> _images = [
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ElephantsDream.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerBlazes.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerFun.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerMeltdowns.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/Sintel.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/SubaruOutbackOnStreetAndDirt.jpg',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/images/TearsOfSteel.jpg'
  ];

  final List<String> _videos = [
    'https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/SubaruOutbackOnStreetAndDirt.mp4',
    'https://storage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4'
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: StoriesView(
        storyPages: List.generate(4, (index) => index + 1).map((e) {
          var medias = ((_images..shuffle()).getRange(0, 3).toList() +
              (_videos..shuffle()).getRange(0, 3).toList());
          return StoryPage(
            items: medias.map((e) {
              var mime = lookupMimeType(e);
              if (mime?.contains("image") == true) {
                // 网络图片
                return StoryItem.networkImage(url: e);
              } else if (mime?.contains("video") == true) {
                // 网络视频
                return StoryItem.networkVideo(url: e);
              } else {
                // 默认容器
                return StoryItem(builder: (ctx) => Container());
              }
            }).toList(),
          );
        }).toList(),
      ),
    );
  }
}

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

1 回复

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


stories_view 是一个用于展示故事视图的 Flutter 插件,类似于 Instagram 或 Facebook 的故事功能。它允许你以全屏的方式展示一系列图片或视频,用户可以滑动浏览不同的故事。

1. 安装插件

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

dependencies:
  flutter:
    sdk: flutter
  stories_view: ^0.1.0  # 请使用最新版本

然后运行 flutter pub get 来安装插件。

2. 基本使用

下面是一个简单的示例,展示如何使用 stories_view 插件来展示故事。

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

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

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

class StoryScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stories View Example'),
      ),
      body: Center(
        child: StoriesView(
          stories: [
            StoryItem(
              type: StoryType.image,
              url: 'https://via.placeholder.com/150',
              duration: Duration(seconds: 5),
            ),
            StoryItem(
              type: StoryType.video,
              url: 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4',
              duration: Duration(seconds: 10),
            ),
            StoryItem(
              type: StoryType.image,
              url: 'https://via.placeholder.com/300',
              duration: Duration(seconds: 5),
            ),
          ],
          onComplete: () {
            print('All stories completed');
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

3. 参数说明

  • stories: 一个 StoryItem 列表,每个 StoryItem 代表一个故事项。
    • type: 故事的类型,可以是 StoryType.imageStoryType.video
    • url: 图片或视频的 URL。
    • duration: 每个故事的显示时长。
  • onComplete: 当所有故事播放完毕时的回调函数。

4. 自定义选项

stories_view 还提供了一些自定义选项,例如:

  • progressBarColor: 进度条的颜色。
  • progressBarHeight: 进度条的高度。
  • showProgressBar: 是否显示进度条。
  • onStoryShow: 当某个故事开始播放时的回调函数。
StoriesView(
  stories: [
    // Story items
  ],
  progressBarColor: Colors.blue,
  progressBarHeight: 4.0,
  showProgressBar: true,
  onStoryShow: (index) {
    print('Story $index is showing');
  },
  onComplete: () {
    print('All stories completed');
  },
);
回到顶部