Flutter动画效果插件fade_and_translate的使用

发布于 1周前 作者 bupafengyu 来自 Flutter

Flutter动画效果插件fade_and_translate的使用

fade_and_translate 是一个用于在淡入淡出的同时平移子组件的 Flutter 小部件。它可以通过控制可见性来实现平滑的过渡效果。

使用

首先,确保你已经添加了 fade_and_translate 依赖项到你的 pubspec.yaml 文件中:

dependencies:
  flutter:
    sdk: flutter
  fade_and_translate: ^最新版本号

然后导入所需的包:

import 'package:fade_and_translate/fade_and_translate.dart';

示例代码

以下是一个完整的示例代码,展示了如何使用 FadeAndTranslate 小部件:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'fade_and_translate Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'fade_and_translate Demo'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  /// 每个组件过渡前的延迟时间。
  static const Duration _delay = const Duration(milliseconds: 40);

  /// 组件过渡时的平移距离。
  static const Offset _translate = Offset(0.0, -24.0);

  bool _visible = true;

  /// 切换组件的可见性。
  void _toggle() {
    _visible = !_visible;
    setState(() {});
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _toggle,
        child: Icon(_visible ? Icons.visibility_off : Icons.visibility),
      ),
      body: ListView(
        padding: const EdgeInsets.only(
          top: 12.0,
          left: 20.0,
          right: 20.0,
        ),
        children: [
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay * 0 : _delay * 5,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.red, label: 'Red'),
          ),
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay : _delay * 4,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.orange, label: 'Orange'),
          ),
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay * 2 : _delay * 3,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.yellow, label: 'Yellow'),
          ),
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay * 3 : _delay * 2,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.green, label: 'Green'),
          ),
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay * 4 : _delay,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.blue, label: 'Blue'),
          ),
          FadeAndTranslate(
            autoStart: true,
            delay: _visible ? _delay * 5 : _delay * 0,
            translate: _translate,
            visible: _visible,
            child: MyWidget(color: Colors.purple, label: 'Purple'),
          ),
        ],
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  const MyWidget({
    @required this.color,
    @required this.label,
  })  : assert(color != null),
        assert(label != null);

  final Color color;

  final String label;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Container(
      height: 60.0,
      margin: const EdgeInsets.symmetric(vertical: 8.0),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(left: BorderSide(color: color, width: 6.0)),
        boxShadow: [
          BoxShadow(
            color: Colors.black45,
            offset: Offset(2.0, 2.0),
            blurRadius: 2.0,
          ),
        ],
      ),
      child: Align(
        alignment: Alignment.centerLeft,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12.0),
          child: Text(
            label,
            style: TextStyle(
              color: color,
              fontSize: 22.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter动画效果插件fade_and_translate的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动画效果插件fade_and_translate的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用fade_and_translate插件来实现动画效果的代码示例。fade_and_translate插件允许你轻松地在Flutter应用中实现淡入淡出和平移动画。

首先,确保你已经在pubspec.yaml文件中添加了fade_and_translate依赖:

dependencies:
  flutter:
    sdk: flutter
  fade_and_translate: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来获取依赖。

接下来是一个完整的示例代码,展示了如何使用fade_and_translate插件来实现一个简单的动画效果:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fade and Translate Animation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<Offset> _animation;
  late Animation<double> _opacityAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(reverse: true);

    _animation = Tween<Offset>(
      begin: Offset.zero,
      end: const Offset(1.0, 0.0),
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    ));

    _opacityAnimation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    ));
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Fade and Translate Animation Demo'),
      ),
      body: Center(
        child: FadeAndTranslateAnimation(
          animation: _animation,
          opacity: _opacityAnimation,
          child: Container(
            width: 100,
            height: 100,
            color: Colors.blue,
            child: Center(
              child: Text(
                'Animate Me',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个Flutter应用,其中包含一个动画效果的容器。FadeAndTranslateAnimation组件接收两个动画对象:一个是位置动画_animation,另一个是透明度动画_opacityAnimation

  • AnimationController控制动画的持续时间和循环行为。
  • Tween定义了动画的起始和结束值。
  • CurvedAnimation用于定义动画的曲线,这里我们使用了Curves.easeInOut

FadeAndTranslateAnimation组件会根据提供的动画值来执行相应的淡入淡出和平移动画。

注意:由于fade_and_translate插件的具体API可能会根据版本有所不同,请参考该插件的官方文档以确保代码兼容。如果fade_and_translate插件的API不支持直接传递Animation<Offset>Animation<double>,你可能需要按照该插件的API文档进行调整。

回到顶部