搜索插件、代码仓与 APP 分类

motion 封面
插件详情v2.0.3

motion

一个华丽的控件,可将其子控件应用陀螺仪或悬停状态下的运动效果。

AndroidiOSWeb
近 30 天下载
869
喜欢数
289
Pub 分数
150

安装插件

flutter pub add motion

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

运动包通过将动态的、基于陀螺仪或悬停的运动效果应用于其子部件,增强了用户体验。它支持响应设备移动或光标位置的交互式和沉浸式动画,非常适合在移动和网络平台上创建引人入胜的用户界面。该包轻量级,设置简单,可无缝集成到Flutter项目中。它支持Android和iOS设备以及网络浏览器,适用于跨平台开发。非常适合为按钮、卡片或背景添加微妙的深度感和响应性。

适合场景

  • 交互式应用界面
  • 陀螺仪响应式用户界面元素
  • 基于悬停的动画(网页端)
  • 增强卡片或按钮的反馈
  • 创建沉浸式登录页面

主要特点

  • 陀螺仪驱动的运动
  • 基于悬停的动画支持
  • 跨平台兼容性
  • 与现有控件的简单集成
  • 流畅、高性能的动画

适合对象

  • 寻求交互式 UI 的开发人员
  • 希望获得动态视觉反馈的设计人员
  • 面向移动和 Web 的项目
  • 追求现代、引人入胜的用户体验的应用程序
  • 原型设计响应式动态效果

使用建议

  • 可能会影响低端设备的性能
  • 需要设备传感器以启用陀螺仪模式
  • Web 支持取决于浏览器功能
  • 除了基本运动参数外,自定义选项有限
  • 不适合静态或极简设计

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Motion for Flutter widgets

pub package

This package adds a new Motion widget that applies a gyroscope-based effect to Flutter widgets. On desktop or when the gyroscope in not available, the effect is based on the pointer's hovering.

Check out the live demo !

"Demo of the Motion plugin"

To see examples of the following effect on a device or simulator:

cd example/
flutter run --release

How to use

First, add the dependency to your pubspec.yaml file:

dependencies:
  motion: ^<latest-version>

Then, initialize the Motion plugin at Dart runtime, before running your Flutter app :

Future<void> main() async {

  /// Initialize the plugin.
  await Motion.instance.initialize();

  /// Run your app.
  runApp(...);

}

Finally, wrap your target widget as child of a Motion widget. You may optionally provide a MotionController instance that will hold individual widget's transformations (useful for pointer based events). The simplest usage of this widget is the following :


import 'package:motion/motion.dart';

...

return Motion(child: myWidget);

Custom behavior

Elevation

To remain consistent with the Material design language, you can pass an elevation parameter to the widget by using the Motion.elevated constructor.

It will influence the offset, opacity and blurriness of the shadow. Possible values for elevation range between 0 and 100.

"Elevations examples"

Comparing different elevation values

Shadow

The shadow is optional and depends, when using the Motion.elevated constructor, on the elevation value. The higher the elevation, the blurrier and the lower from behind the widget the shadow will get. The amplitude of the shadow's movement also depends on the widget's elevation. This aims to achieve the same behavior as Material design language's notion of elevation.

"Shadow effect comparison"

Comparing with and without the shadow effect

By default, the shadow is enabled but you can disable it by constructing the Motion widget with shadow: null or by using the Motion.only constructor.

Glare

The glare effect is also optional. It is a very subtle gradient overlay that confers a reflective feel to the widget. This effect is not rendered on Safari iOS due to limitations on gradients performances.

"Glare effect comparison"

Comparing with and without the glare effect

Also enabled by default, you can disable this effect by constructing the Motion widget with glare: false or by using the Motion.only constructor.

Custom effects configurations

You can provide custom configurations for the glare, shadow and translation effects by passing GlareConfiguration, ShadowConfiguration and TranslationConfiguration to the Motion and Motion.only constructors. When omitted, the default values will be used, unless if you use the Motion.only constructor which only applies the tilt effect by default.

Damping

By default, the widget will naturally rotate back to its original position if the phone stops rotating. This intends to improve the user experience by preserving the widget's legibility as much as possible. This property is only applied to gyroscope events and is ignored in pointer events.

The damping property of your MotionController allows you to fine tune or disable this behavior. By providing a value between 0 and 1, you can change the speed at which the widget rotates back to its original position. Explicitly providing null disables this effect.

The default value is 0.2.

Update interval

By default, the sensor's update and pointer events intervals are set to 60 frames per second.

Higher values will result in more accurate motion data and thus smoother motion of the widgets, but will also have an increased impact on performances.

Ideally, the update interval should match Flutter's recommended 60 FPS (frames per seconds). However, a performance compromise may be required on certain older devices. In that case, you could use the standard 30 FPS or 24 FPS, the latter being the lowest frame rate required to make motion appear natural to the human eye.

The best practice for setting the sensor rate is to do it once, when initializing your app, like so :

void main() {

  /// Initialize the plugin.
  await Motion.instance.initialize();

  /// Globally set the sensors sampling rate to 60 frames per second.
  Motion.instance.setUpdateInterval(60.fps);

  /// Run your app.
  runApp(...);

}

Filter quality

The Motion widget implementation wraps the child with a matrix-based Transform widget. On most platforms, it is a good practice to have it set to FilterQuality.medium or FilterQuality.high. You may specify it directly from the widget's constructor :

  Motion(
    filterQuality: FilterQuality.high,
    child: ...
  )

However, please note that due to limitations on Safari iOS, it is always enforced to null in this specific browser.

Some developers have reported issues when using FilterQuality.high on the Web in certain Flutter 3 versions. You may need to check kIsWeb and use a different value, depending on the versions of Flutter you're using.

The default value is FilterQuality.high.

Permission

This implementation is optional. You may skip but Motion widgets will have no effect on any iOS 13+ Safari browser.

Starting from iOS 13, the Safari browser requires to call DeviceMotionEvent.requestPermission() to listen to devicemotion events. This permission must be requested upon user gesture, otherwise the Promise will automatically be rejected.

To detect if the permission is required, you can check the Motion.instance.isPermissionRequired property after calling Motion.instance.initialize().

If required, you can either call Motion.requestPermission() after presenting the user a rationale dialog or after pressing a button. An implementation can be found in the example app.

No other platform permission to access sensor data is implemented for now.

Contribution Guide

If you are having any problem with the Motion package, you can file an issue on the package repo's issue tracker.

Please make sure that your concern hasn't already been addressed in the "Closed" section. Although we try to take care of every issue in a timely manner, please always remember that open source maintainers owe you nothing.

Whether you're running into an error you think you could fix or if you want other features — if you're unhappy with the outcome of your proposal, please do not republish this package under a different name. Instead, you should depend on your own GitHub fork of the plugin without affecting every other developers who may not share your point of view.

Thank you for your understanding !

Credits

This package was developed with ♥ by @mrcendre.

Thanks to @sebstianbuechler and @ekasetiawans for their contributions !

所属分类

依赖

依赖清单 3 项

  • motion_platform_interface^2.0.2
  • motion_web^2.0.2
  • vector_math^2.4.3

源码仓库

使用此插件的项目

暂无相关数据。