Flutter电视端轮播图插件flutter_swiper_tv的使用
Flutter电视端轮播图插件flutter_swiper的使用
flutter_swiper
适用于 Flutter 的最佳轮播组件,支持多种布局,无限循环。兼容 Android 和 iOS。
新功能:分页布局
我们正在使用这个项目 flutter_page_indicator。
新功能:PageTransformer
现在我们有了类似于 Android 的 PageTransformer
,只需将一个 transformer
设置到 Swiper
中,
它会返回一个已经变换过的组件。目前仅支持 DEFAULT
布局。
感谢 @FlutterRocks,你做得很好 👏。
我们正在使用这个项目 transformer_page_view。
新功能:布局
展示
路线图
更新日志
开始使用
安装
在你的 pubspec.yaml
文件中添加:
flutter_swiper: ^latest_version
然后运行:
flutter packages get
在项目的根目录下执行上述命令。
基本用法
创建一个新的项目:
flutter create myapp
编辑 lib/main.dart
如下:
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/350x150",
fit: BoxFit.fill,
);
},
itemCount: 3,
pagination: new SwiperPagination(),
control: new SwiperControl(),
),
);
}
}
构造函数
基本
参数 | 默认值 | 描述 |
---|---|---|
scrollDirection | Axis.horizontal | 如果为 Axis.horizontal ,滚动视图的子元素会在一行中水平排列,而不是一列中垂直排列。 |
loop | true | 设置为 false 可以禁用连续循环模式。 |
index | 0 | 初始幻灯片的索引号。 |
autoplay | false | 设置为 true 可以启用自动播放模式。 |
onIndexChanged | void onIndexChanged(int index) | 用户滑动或自动播放时调用的新索引。 |
onTap | void onTap(int index) | 用户点击 UI 时调用。 |
duration | 300.0 | 每次交易动画花费的毫秒数。 |
pagination | null | 设置 new SwiperPagination() 显示默认分页。 |
control | null | 设置 new SwiperControl() 显示默认控制按钮。 |
分页
分页继承自 SwiperPlugin
,SwiperPlugin
为 Swiper
提供额外的 UI。设置 new SwiperPagination()
显示默认分页。
参数 | 默认值 | 描述 |
---|---|---|
alignment | Alignment.bottomCenter | 改变此值可以将分页放在其他位置。 |
margin | const EdgeInsets.all(10.0) | 分页与父容器内侧的距离。 |
builder | SwiperPagination.dots | 有两个默认样式 SwiperPagination.dots 和 SwiperPagination.fraction ,都可以自定义。 |
如果你想自定义自己的分页,你可以这样做:
new Swiper(
...,
pagination: new SwiperCustomPagination(
builder: (BuildContext context, SwiperPluginConfig config) {
return new YourOwnPaginatipon();
}
)
);
控制按钮
控制也继承自 SwiperPlugin
,设置 new SwiperControl()
显示默认控制按钮。
参数 | 默认值 | 描述 |
---|---|---|
iconPrevious | Icons.arrow_back_ios | 显示 < 控制按钮的图标数据。 |
iconNext | Icons.arrow_forward_ios | 显示 > 的图标数据。 |
color | Theme.of(context).primaryColor | 控制按钮的颜色。 |
size | 30.0 | 控制按钮的大小。 |
padding | const EdgeInsets.all(5.0) | 控制按钮的填充。 |
控制器
Controller
用于控制 Swiper
的索引,开始或停止自动播放。可以通过 new SwiperController()
创建一个控制器,并保存实例以便后续使用。
方法 | 描述 |
---|---|
void move(int index, {bool animation: true}) | 移动到指定索引,是否带有动画。 |
void next({bool animation: true}) | 移动到下一个。 |
void previous({bool animation: true}) | 移动到前一个。 |
void startAutoplay() | 开始自动播放。 |
void stopAutoplay() | 停止自动播放。 |
自动播放
参数 | 默认值 | 描述 |
---|---|---|
autoplayDelay | 3000 | 自动播放延迟时间(毫秒)。 |
autoplayDisableOnInteraction | true | 如果设置为 true ,则在用户滑动时禁用自动播放。 |
内置布局
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
viewportFraction: 0.8,
scale: 0.9,
)
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
itemWidth: 300.0,
layout: SwiperLayout.STACK,
)
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
itemWidth: 300.0,
itemHeight: 400.0,
layout: SwiperLayout.TINDER,
)
创建自定义动画非常简单:
new Swiper(
layout: SwiperLayout.CUSTOM,
customLayoutOption: new CustomLayoutOption(
startIndex: -1,
stateCount: 3
).addRotate([
-45.0 / 180,
0.0,
45.0 / 180
]).addTranslate([
new Offset(-370.0, -40.0),
new Offset(0.0, 0.0),
new Offset(370.0, -40.0)
]),
itemWidth: 300.0,
itemHeight: 200.0,
itemBuilder: (context, index) {
return new Container(
color: Colors.grey,
child: new Center(
child: new Text("$index"),
),
);
},
itemCount: 10
)
CustomLayoutOption
用于描述动画。很容易指定 Swiper
中每个项目的状态。
new CustomLayoutOption(
startIndex: -1, // 第一个项目的索引
stateCount: 3 // 数组长度
).addRotate([ // 每个项目的旋转角度
-45.0 / 180,
0.0,
45.0 / 180
]).addTranslate([ // 每个项目的偏移量
new Offset(-370.0, -40.0),
new Offset(0.0, 0.0),
new Offset(370.0, -40.0)
])
代码示例
new ConstrainedBox(
child: new Swiper(
outer: false,
itemBuilder: (c, i) {
return new Wrap(
runSpacing: 6.0,
children: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((i) {
return new SizedBox(
width: MediaQuery.of(context).size.width / 5,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new SizedBox(
child: new Container(
child: new Image.network(
"https://fuss10.elemecdn.com/c/db/d20d49e5029281b9b73db1c5ec6f9jpeg.jpeg%3FimageMogr/format/webp/thumbnail/!90x90r/gravity/Center/crop/90x90"
),
),
height: MediaQuery.of(context).size.width * 0.12,
width: MediaQuery.of(context).size.width * 0.12,
),
new Padding(
padding: new EdgeInsets.only(top: 6.0),
child: new Text("$i"),
)
],
),
);
}).toList(),
);
},
pagination: new SwiperPagination(
margin: new EdgeInsets.all(5.0)
),
itemCount: 10,
),
constraints: new BoxConstraints.loose(new Size(screenWidth, 170.0))
),
更多关于Flutter电视端轮播图插件flutter_swiper_tv的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter电视端轮播图插件flutter_swiper_tv的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter电视端使用flutter_swiper_tv
插件来实现轮播图的代码示例。这个插件是flutter_swiper
的电视端版本,专为大屏设备优化。
首先,你需要在pubspec.yaml
文件中添加flutter_swiper_tv
依赖:
dependencies:
flutter:
sdk: flutter
flutter_swiper_tv: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下方式使用flutter_swiper_tv
来实现轮播图功能:
import 'package:flutter/material.dart';
import 'package:flutter_swiper_tv/flutter_swiper_tv.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter TV Carousel Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final List<String> imageUrls = [
'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('Flutter TV Carousel Demo'),
),
body: Center(
child: Swiper(
itemBuilder: (BuildContext context, int index) {
return Image.network(
imageUrls[index],
fit: BoxFit.cover,
);
},
itemCount: imageUrls.length,
autoplay: true,
autoplayDelay: 2000,
autoplayDisableOnInteraction: false,
pagination: SwiperPagination(
builder: DotSwiperPaginationBuilder(),
),
// 针对电视端优化的一些参数
scrollDirection: Axis.horizontal,
loop: true,
// 可以根据需要调整以下参数以适应电视端
// 比如增加滑动速度,调整动画时间等
scrollPhysics: BouncingScrollPhysics(),
),
),
);
}
}
代码说明:
- 依赖导入:首先,我们导入了
flutter_swiper_tv
包。 - 数据准备:在
MyHomePage
类中,我们定义了一个包含图片URL的列表imageUrls
。 - Swiper组件:
itemBuilder
:用于构建每个轮播项,这里我们简单地使用了Image.network
来加载网络图片。itemCount
:轮播项的数量。autoplay
:是否自动播放。autoplayDelay
:自动播放的间隔时间(毫秒)。autoplayDisableOnInteraction
:用户交互后是否停止自动播放。pagination
:分页指示器,这里使用了默认的圆点指示器。scrollDirection
:滚动方向,这里设置为水平滚动。loop
:是否循环播放。scrollPhysics
:滚动物理效果,这里使用了BouncingScrollPhysics
,你可以根据需要调整。
电视端优化:
对于电视端,你可能需要调整一些参数以适应大屏幕和遥控器操作,比如增加自动播放的间隔时间、调整动画效果等。此外,确保你的图片资源适合大屏显示,并保持较高的分辨率。
这个示例提供了一个基本的实现,你可以根据实际需求进一步定制和优化。