Flutter动画过渡效果插件animation_transition的使用

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

Flutter动画过渡效果插件animation_transition的使用

捕获

banner

image

Animation_transition

简介

动画包用于在 Flutter 中进行页面和小部件之间的过渡。它包含开始和结束控制器以及动画重复器。要使用该包,只需将其添加到项目中。

文档

请阅读文档以了解如何使用它。

示例代码

// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:animation_transition/animation_transition.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(
      title: 'Animation transition',
      theme: ThemeData(useMaterial3: true),
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

int tokenI = 0;

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.amber,
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: ListView(
        children: [
          for (TransitionType t in TransitionType.values)
            TextButton(
                onPressed: () {
                  transitionAnimation.transitionPageRoute(
                    pageGo: AnimationData(
                      child: PageAnimation(
                        name: t.name,
                        type: t,
                      ),
                      //
                      transition: TransitionType.ZoomIn,
                      duration: Duration(milliseconds: 200),
                      onStart: (token) {
                        tokenI = token;
                      },
                    ),
                    context: context,
                  );
                },
                child: Text(t.name)),

          // 显示所有过渡效果
          TextButton(
              onPressed: () {
                transitionAnimation.transitionPageRoute(
                  pageGo: AnimationData(
                    child: AllTransicion(),
                    //
                    transition: TransitionType.ZoomIn,
                    duration: Duration(milliseconds: 200),
                    onStart: (token) {
                      tokenI = token;
                    },
                  ),
                  context: context,
                );
              },
              child: const Text(
                '所有过渡效果',
                style:
                    TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
              )),
        ],
      ),
    );
  }
}

class AllTransicion extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          ElevatedButton(
              onPressed: () {
                transitionAnimation.backPage(token: tokenI, context: context);
              },
              child: const Text('返回'))
        ],
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Wrap(
          spacing: 10,
          runSpacing: 10,
          children: [
            for (TransitionType t in TransitionType.values)
              transitionAnimation.start(
                data: AnimationData(
                  type: TypeTransition.repeat,
                  transition: t,
                  duration: const Duration(milliseconds: 1000),
                  child: Content(
                    name: t.name,
                  ),
                ),
              )
          ],
        ),
      ),
    );
  }
}

class PageAnimation extends StatelessWidget {
  String name;
  TransitionType type;
  PageAnimation({
    super.key,
    required this.name,
    required this.type,
  });

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          ElevatedButton(
              onPressed: () {
                transitionAnimation.backPage(token: tokenI, context: context);
              },
              child: const Text('返回'))
        ],
      ),
      body: Center(
        child: transitionAnimation.start(
          data: AnimationData(
            type: TypeTransition.repeat,
            transition: type,
            duration: const Duration(milliseconds: 1000),
            child: Content(
              name: name,
            ),
          ),
        ),
      ),
    );
  }
}

class Content extends StatelessWidget {
  String name;
  Content({super.key, required this.name});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(5),
      color: Colors.amber,
      height: 250,
      width: 250,
      child: Column(
        children: [
          Center(
            child: Text(name),
          ),
        ],
      ),
    );
  }
}

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

1 回复

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


当然,以下是一个关于如何在Flutter中使用animation_transition插件来实现动画过渡效果的代码示例。animation_transition 插件并不是一个官方的Flutter插件,但假设它是一个第三方库,用于简化动画过渡效果的管理。以下示例将展示如何假设使用这样一个库来实现基本的页面过渡动画。

首先,确保你已经在pubspec.yaml文件中添加了animation_transition依赖(注意:这里假设animation_transition是一个存在的包,实际上你需要替换为真实存在的类似功能的包,如fluttertoast等,这里仅为示例):

dependencies:
  flutter:
    sdk: flutter
  animation_transition: ^x.y.z  # 替换为实际版本号

然后运行flutter pub get来安装依赖。

接下来,假设animation_transition提供了一个简单的AnimatedTransition widget,用于包裹页面内容并实现动画效果。下面是一个基本的示例代码,展示如何使用这个假设的AnimatedTransition来实现页面间的过渡动画。

import 'package:flutter/material.dart';
import 'package:animation_transition/animation_transition.dart'; // 假设的导入路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
      routes: {
        '/second': (context) => SecondScreen(),
      },
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/second',
                arguments: SlideTransitionData(
                  alignment: Alignment.centerLeft,
                  duration: Duration(milliseconds: 500),
                ));
          },
          child: Text('Go to Second Screen'),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  final SlideTransitionData? transitionData;

  SecondScreen({Key? key, this.transitionData}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedTransition<SlideTransitionData>(
      transitionData: transitionData!,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Second Screen'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('Go Back'),
          ),
        ),
      ),
      builder: (context, animation, child) {
        return SlideTransition(
          position: animation,
          child: child,
        );
      },
    );
  }
}

class SlideTransitionData {
  final Alignment alignment;
  final Duration duration;

  SlideTransitionData({required this.alignment, required this.duration});
}

typedef AnimatedTransitionBuilder<T> = Widget Function(
    BuildContext context, Animation<double> animation, Widget? child);

class AnimatedTransition<T> extends StatefulWidget {
  final T transitionData;
  final Widget child;
  final AnimatedTransitionBuilder<T> builder;

  AnimatedTransition({
    required this.transitionData,
    required this.child,
    required this.builder,
  });

  @override
  _AnimatedTransitionState<T> createState() => _AnimatedTransitionState<T>();
}

class _AnimatedTransitionState<T> extends State<AnimatedTransition<T>>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(milliseconds: 500), // 这里可以根据transitionData调整
      vsync: this,
    )..repeat(reverse: true);

    _animation = 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 widget.builder(context, _animation, widget.child);
  }
}

注意

  1. 上述代码中的AnimatedTransitionSlideTransitionData是假设的实现,用于展示如何封装动画数据并在页面间传递。
  2. 实际上,Flutter社区中可能已经有类似的库存在,比如flutter_screenutilfluttertoast等,但专门用于动画过渡效果的库可能需要根据具体需求选择或实现。
  3. Flutter本身提供了强大的动画系统,通过AnimationControllerTweenAnimatedBuilder等类可以实现复杂的动画效果,而无需依赖第三方库。

希望这个示例能够帮助你理解如何在Flutter中实现动画过渡效果。如果有特定的动画过渡效果库,请查阅其文档以获取更详细的用法。

回到顶部