Flutter高级故事视图插件story_view_advance的使用
Flutter 高级故事视图插件 story_view_advance
的使用
story_view_advance
是一个用于在应用中展示故事的 Flutter 插件。它类似于 WhatsApp 和 Instagram 中的故事功能,并且可以嵌入到 ListView
或 Column
中,就像 Google 新闻应用一样。此外,该插件还支持手势暂停、倒回和前进功能。
特性
- 静态图像、GIF 和视频支持(启用缓存)
- 手势用于暂停、倒回和前进
- 每个故事项的字幕
- 每个故事项的动画进度指示器
- 全屏或内联显示
- 有用的回调以执行元功能,包括垂直滑动手势
安装
要使用此插件,在 pubspec.yaml
文件中添加 story_view
作为依赖项:
dependencies:
story_view: ^版本号
使用方法
首先,导入包:
import 'package:story_view_advance/story_view_advance.dart';
基础用法
使用 StoryView
添加故事到屏幕或视图层次结构中。StoryView
需要一个包含 StoryItem
的列表,每个 StoryItem
描述了每页故事要显示的内容、持续时间等。这为你提供了自定义每页故事的自由度。
简单示例
class Home extends StatelessWidget {
final StoryController controller = StoryController();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Delicious Ghanaian Meals"),
),
body: Container(
margin: EdgeInsets.all(8),
child: ListView(
children: <Widget>[
Container(
height: 300,
child: StoryView(
controller: controller,
storyItems: [
StoryItem.text(
title: "Hello world!\nHave a look at some great Ghanaian delicacies. I'm sorry if your mouth waters. \n\nTap!",
backgroundColor: Colors.orange,
roundedTop: true,
),
StoryItem.inlineImage(
url: "https://image.ibb.co/cU4WGx/Omotuo-Groundnut-Soup-braperucci-com-1.jpg",
caption: Text(
"Omotuo & Nkatekwan; You will love this meal if taken as supper.",
style: TextStyle(
color: Colors.white,
backgroundColor: Colors.black54,
fontSize: 17,
),
),
),
StoryItem.inlineImage(
url: "https://media.giphy.com/media/5GoVLqeAOo6PK/giphy.gif",
caption: Text(
"Hektas, sektas and skatad",
style: TextStyle(
color: Colors.white,
backgroundColor: Colors.black54,
fontSize: 17,
),
),
)
],
onStoryShow: (storyItem, index) {
print("Showing a story");
},
onComplete: () {
print("Completed a cycle");
},
progressPosition: ProgressPosition.bottom,
repeat: false,
inline: true,
),
),
Material(
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => MoreStories()));
},
child: Container(
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.vertical(bottom: Radius.circular(8))),
padding: EdgeInsets.symmetric(vertical: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.arrow_forward, color: Colors.white),
SizedBox(width: 16),
Text(
"View more stories",
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
),
],
),
),
);
}
}
更多故事示例
class MoreStories extends StatefulWidget {
[@override](/user/override)
_MoreStoriesState createState() => _MoreStoriesState();
}
class _MoreStoriesState extends State<MoreStories> {
final storyController = StoryController();
[@override](/user/override)
void dispose() {
storyController.dispose();
super.dispose();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("More"),
),
body: StoryView(
storyItems: [
StoryItem.text(
title: "I guess you'd love to see more of our food. That's great.",
backgroundColor: Colors.blue,
),
StoryItem.text(
title: "Nice!\n\nTap to continue.",
backgroundColor: Colors.red,
textStyle: TextStyle(
fontFamily: 'Dancing',
fontSize: 40,
),
),
StoryItem.pageImage(
url: "https://image.ibb.co/cU4WGx/Omotuo-Groundnut-Soup-braperucci-com-1.jpg",
caption: Text(
"Still sampling",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
textAlign: TextAlign.center,
),
controller: storyController,
),
StoryItem.pageImage(
url: "https://media.giphy.com/media/5GoVLqeAOo6PK/giphy.gif",
caption: Text(
"Working with gifs",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
textAlign: TextAlign.center,
),
controller: storyController),
StoryItem.pageImage(
url: "https://media.giphy.com/media/XcA8krYsrEAYXKf4UQ/giphy.gif",
caption: Text(
"Hello, from the other side",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
textAlign: TextAlign.center,
),
controller: storyController,
),
StoryItem.pageImage(
url: "https://media.giphy.com/media/XcA8krYsrEAYXKf4UQ/giphy.gif",
caption: Text(
"Hello, from the other side2",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
textAlign: TextAlign.center,
),
controller: storyController,
),
],
onStoryShow: (storyItem, index) {
print("Showing a story");
},
onComplete: () {
print("Completed a cycle");
},
progressPosition: ProgressPosition.top,
repeat: false,
controller: storyController,
),
);
}
}
控制器、加载器和 GIF 支持
为了提升用户体验,可以在图片加载时暂停故事。为此,创建一个全局的 StoryController
实例,并在创建 StoryItem
时传递该控制器实例。
final controller = StoryController();
[@override](/user/override)
Widget build(context) {
List<StoryItem> storyItems = [
StoryItem.text(...),
StoryItem.pageImage(...),
StoryItem.pageImage(...),
StoryItem.pageVideo(
...,
controller: controller,
)
]; // 你的故事列表
return StoryView(
storyItems,
controller: controller, // 在这里传递控制器
repeat: true, // 故事是否永远滑动
onStoryShow: (s) {notifyServer(s);},
onComplete: () {},
onVerticalSwipeComplete: (direction) {
if (direction == Direction.down) {
Navigator.pop(context);
}
} // 若要禁用垂直滑动手势,忽略此参数。
// 适用于内联故事视图。
);
}
更多关于Flutter高级故事视图插件story_view_advance的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter高级故事视图插件story_view_advance的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用story_view_advance
插件的一个基本示例。这个插件通常用于创建类似于Instagram和Snapchat的故事视图界面。假设你已经通过pubspec.yaml
文件添加了story_view_advance
依赖项。
首先,确保你的pubspec.yaml
文件中包含以下依赖项:
dependencies:
flutter:
sdk: flutter
story_view_advance: ^最新版本号 # 请替换为实际发布的最新版本号
然后运行flutter pub get
来安装依赖。
接下来,我们可以创建一个简单的示例来展示如何使用story_view_advance
插件。
import 'package:flutter/material.dart';
import 'package:story_view_advance/story_view_advance.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: StoryViewScreen(),
);
}
}
class StoryViewScreen extends StatefulWidget {
@override
_StoryViewScreenState createState() => _StoryViewScreenState();
}
class _StoryViewScreenState extends State<StoryViewScreen> {
List<String> storyImages = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Story View Advance Example'),
),
body: Center(
child: StoryViewAdvance(
storyList: storyImages.map((url) => NetworkImage(url)).toList(),
onPageChanged: (int index) {
print('Current page index: $index');
},
onStoryEnd: () {
print('Story has ended');
},
pageController: PageController(initialPage: 0),
indicatorColor: Colors.white,
indicatorActiveColor: Colors.red,
indicatorSize: 10.0,
indicatorSpacing: 10.0,
indicatorPadding: 10.0,
progressColor: Colors.grey.withOpacity(0.5),
isAutoPlay: true,
autoPlayInterval: Duration(seconds: 3),
isLoop: false,
aspectRatio: 16 / 9,
placeholder: CircularProgressIndicator(),
),
),
);
}
}
在这个示例中,我们做了以下几件事:
- 导入必要的包:我们导入了
flutter/material.dart
和story_view_advance
包。 - 创建主应用:
MyApp
是一个简单的MaterialApp
,它包含一个StoryViewScreen
作为主页。 - 创建故事视图屏幕:
StoryViewScreen
是一个有状态的Widget,它维护了一个故事图片的列表。 - 使用
StoryViewAdvance
组件:storyList
:包含网络图片的URL列表,这些图片将被用作故事的内容。onPageChanged
:当用户翻页时调用的回调函数。onStoryEnd
:当用户看完所有故事时调用的回调函数。pageController
:控制分页的PageController
。indicatorColor
、indicatorActiveColor
、indicatorSize
、indicatorSpacing
和indicatorPadding
:自定义分页指示器的样式。progressColor
:加载图片时的进度条颜色。isAutoPlay
和autoPlayInterval
:控制是否自动播放故事以及自动播放的间隔时间。isLoop
:控制故事是否循环播放。aspectRatio
:控制故事的宽高比。placeholder
:在图片加载时显示的占位符。
这个示例提供了一个基础框架,你可以根据自己的需求进一步定制和扩展。确保你替换storyImages
列表中的URL为你自己的图片URL。