Flutter过渡动画插件trastition_ap的使用

Flutter过渡动画插件trastition_ap的使用

这个包用于管理页面之间的过渡动画。

示例

以下是一个简单的示例,展示了如何使用 Trastition_ap 包来实现页面之间的过渡动画。

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        floatingActionButton: FloatingActionButton(onPressed: () {
          // 使用 Trastition_ap 进行页面跳转
          Navigator.push(
            context,
            Trastition_ap(
              page: Page2(), // 目标页面
              transitionDuration: Duration(seconds: 0), // 动画持续时间
            ),
          );
        }),
        appBar: AppBar(
          title: const Text('Material App Bar'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page 2'),
      ),
      floatingActionButton: FloatingActionButton(onPressed: () {}),
      body: const Center(
        child: Text(
          'Page 2!',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

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

1 回复

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


trastition_ap 并不是 Flutter 官方或社区中广泛使用的过渡动画插件。可能是你提到的插件名称有误,或者是一个不太知名的插件。在 Flutter 中,实现过渡动画通常可以使用以下几种方式:

  1. 内置的过渡动画:Flutter 提供了多种内置的过渡动画,如 PageRouteBuilderHeroAnimatedContainerAnimatedOpacity 等。

  2. 第三方插件:有一些流行的第三方插件可以帮助你实现复杂的过渡动画,例如 flutter_spinkitanimationssimple_animations 等。

  3. 自定义动画:你可以使用 AnimationControllerTween 来自定义过渡动画。

内置过渡动画示例

以下是一个使用 PageRouteBuilder 实现页面过渡动画的示例:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstScreen(),
    );
  }
}

class FirstScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('First Screen')),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.push(
              context,
              PageRouteBuilder(
                pageBuilder: (context, animation, secondaryAnimation) => SecondScreen(),
                transitionsBuilder: (context, animation, secondaryAnimation, child) {
                  var begin = Offset(1.0, 0.0);
                  var end = Offset.zero;
                  var curve = Curves.ease;

                  var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

                  return SlideTransition(
                    position: animation.drive(tween),
                    child: child,
                  );
                },
              ),
            );
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: ElevatedButton(
          child: Text('Go back'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

使用第三方插件

如果你想要使用第三方插件来实现过渡动画,可以尝试 flutter_spinkitanimations。以下是一个使用 flutter_spinkit 的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LoadingScreen(),
    );
  }
}

class LoadingScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Loading Screen')),
      body: Center(
        child: SpinKitFadingCircle(
          color: Colors.blue,
          size: 50.0,
        ),
      ),
    );
  }
}

自定义动画

如果你想完全自定义动画,可以使用 AnimationControllerTween。以下是一个简单的自定义动画示例:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: AnimatedContainerExample(),
    );
  }
}

class AnimatedContainerExample extends StatefulWidget {
  [@override](/user/override)
  _AnimatedContainerExampleState createState() => _AnimatedContainerExampleState();
}

class _AnimatedContainerExampleState extends State<AnimatedContainerExample> {
  double _width = 100.0;
  double _height = 100.0;

  void _animateContainer() {
    setState(() {
      _width = _width == 100.0 ? 200.0 : 100.0;
      _height = _height == 100.0 ? 200.0 : 100.0;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Animated Container')),
      body: Center(
        child: AnimatedContainer(
          width: _width,
          height: _height,
          color: Colors.blue,
          duration: Duration(seconds: 1),
          curve: Curves.easeInOut,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _animateContainer,
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}
回到顶部