Flutter下拉刷新插件rive_pull_to_refresh的使用
Flutter下拉刷新插件rive_pull_to_refresh的使用
rive_pull_to_refresh
简介
-
使用Rive、图像、GIF等自定义刷新指示器。
-
快速集成rive。
-
提供回调以处理下拉刷新动作:
- 客户端下拉或上拉时的值。
- 停止滚动。
- 关闭头部。
-
可定制:
- 浮动、头部模式。
- 拉动百分比以激活刷新。
- 持续时间、曲线等。
planet-header |
planet-floating |
planet-bottom |
---|---|---|
![]() |
![]() |
![]() |
liquid |
bow |
space |
---|---|---|
![]() |
![]() |
![]() |
lipid |
liquid_v1 |
---|---|
![]() |
![]() |
警告
- 您必须了解一些Rive知识。如果不懂,可以使用示例中的现有Rive文件(无法从Flutter编辑颜色)。
Flutter
1. 添加依赖
在您的包的pubspec.yaml
文件中添加以下内容:
rive_pull_to_refresh: ^1.0.4+6
2. 安装依赖
您可以从命令行安装包:
使用dart
:
dart pub get
使用Flutter
:
flutter pub get
3. 导入库
在您的Dart文件中导入:
import 'package:rive_pull_to_refresh/rive_pull_to_refresh.dart';
4. 设置物理属性
BouncingScrollPhysics可能会影响插件的正常工作,因此请设置滚动小部件的物理属性:
ListView.builder(
physics: const ClampingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
// 其他代码...
)
Rive
检查Rive编辑器以了解状态机和输入:
示例使用Rive文件
您可以从以下来源获取Rive文件:
感谢所有贡献者。
支持该插件(可选)
如果您发现此插件有用,可以通过点赞或购买咖啡来支持它。非常感谢!
示例代码
import 'package:flutter/material.dart';
import 'package:rive_pull_to_refresh_example/pages/bow.dart';
import 'package:rive_pull_to_refresh_example/pages/gif.dart';
import 'package:rive_pull_to_refresh_example/pages/image.dart';
import 'package:rive_pull_to_refresh_example/pages/liquid.dart';
import 'package:rive_pull_to_refresh_example/pages/liquid_v1.dart';
import 'package:rive_pull_to_refresh_example/pages/planet.dart';
import 'package:rive_pull_to_refresh_example/pages/pull_rf_lipid.dart';
import 'package:rive_pull_to_refresh_example/pages/space_reload.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
routes: <String, WidgetBuilder>{
Planet.route: (BuildContext context) {
return const Planet();
},
Liquid.route: (BuildContext context) {
return const Liquid();
},
LiquidV1.route: (BuildContext context) {
return const LiquidV1();
},
MainPage.route: (BuildContext context) {
return const MainPage();
},
Gif.route: (BuildContext context) {
return const Gif();
},
SpaceReload.route: (BuildContext context) {
return const SpaceReload();
},
PullRflipid.route: (BuildContext context) {
return const PullRflipid();
},
ImageR.route: (BuildContext context) {
return const ImageR();
},
Bow.route: (BuildContext context) {
return const Bow();
},
},
);
}
}
class MainPage extends StatefulWidget {
static const String route = "/";
const MainPage({super.key});
[@override](/user/override)
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Rive Pull To Refresh"),
),
body: SafeArea(
child: Column(
children: [
AppButton(
title: "Planet",
onPress: () => Navigator.pushNamed(context, Planet.route),
),
AppButton(
title: "Liquid",
onPress: () => Navigator.pushNamed(
context,
Liquid.route,
),
),
AppButton(
title: "LiquidV1",
onPress: () => Navigator.pushNamed(
context,
LiquidV1.route,
),
),
AppButton(
title: "Gif",
onPress: () => Navigator.pushNamed(
context,
Gif.route,
),
),
AppButton(
title: "Space",
onPress: () => Navigator.pushNamed(
context,
SpaceReload.route,
),
),
AppButton(
title: "Lipid",
onPress: () => Navigator.pushNamed(
context,
PullRflipid.route,
),
),
AppButton(
title: "Image",
onPress: () => Navigator.pushNamed(
context,
ImageR.route,
),
),
AppButton(
title: "Bow",
onPress: () => Navigator.pushNamed(
context,
Bow.route,
),
)
],
),
),
);
}
}
class AppButton extends StatelessWidget {
const AppButton({this.title = "", this.onPress, super.key});
final String title;
final Function()? onPress;
[@override](/user/override)
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: InkWell(
onTap: onPress,
child: Container(
color: Colors.blueAccent,
height: 50,
width: double.infinity,
child: Center(child: Text(title)),
),
),
);
}
}
更多关于Flutter下拉刷新插件rive_pull_to_refresh的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter下拉刷新插件rive_pull_to_refresh的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用rive_pull_to_refresh
插件的示例代码。这个插件允许你实现下拉刷新的功能,并且提供了自定义动画的能力。
首先,你需要在你的pubspec.yaml
文件中添加rive_pull_to_refresh
依赖:
dependencies:
flutter:
sdk: flutter
rive_pull_to_refresh: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤使用rive_pull_to_refresh
:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:rive_pull_to_refresh/rive_pull_to_refresh.dart';
- 创建一个自定义的
RiveRefreshIndicator
(如果你需要自定义动画):
class CustomRiveRefreshIndicator extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 这里你可以加载你的Rive文件并创建动画
// 假设你有一个名为`refresh.riv`的Rive文件
final riveFile = RiveFile.asset('assets/refresh.riv');
final animationController = rive.Artboard.find('YourArtboardName')!.controller;
return RiveRefreshIndicator(
riveFile: riveFile,
animationName: 'YourAnimationName',
controller: animationController,
onStateChanged: (state) {
// 你可以根据状态改变来控制动画
if (state == RefreshState.idle) {
animationController.reset(rive.Animation.loopForever);
} else if (state == RefreshState.draggingDown || state == RefreshState.settlingToRefresh) {
animationController.play('PullDown');
} else if (state == RefreshState.refreshing) {
animationController.play('Loading');
} else if (state == RefreshState.settlingToIdle) {
animationController.play('Reset');
}
},
);
}
}
- 在你的
Scaffold
中使用SmartRefresher
:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rive Pull To Refresh Example'),
),
body: SmartRefresher(
enablePullDown: true,
enablePullUp: false, // 如果你不需要上拉加载更多,可以设置为false
header: CustomRiveRefreshIndicator(), // 使用自定义的Rive动画
onRefresh: () async {
// 这里是你的刷新逻辑
await Future.delayed(Duration(seconds: 2));
// 刷新完成后调用
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('刷新完成')));
},
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item $index'),
);
},
),
),
);
}
}
在这个示例中,我们创建了一个自定义的RiveRefreshIndicator
,并将其传递给SmartRefresher
的header
属性。我们还添加了一个简单的刷新逻辑,当触发下拉刷新时,会显示一个SnackBar表示刷新完成。
请注意,你需要有一个有效的Rive文件(.riv
)和相应的动画来使这个示例正常工作。你可以使用Rive的设计工具来创建和导出你的动画。
希望这个示例对你有帮助!如果你有任何进一步的问题,请随时提问。