Flutter动画切换插件easy_animated_indexed_stack的使用

Flutter动画切换插件easy_animated_indexed_stack的使用

简介

Easy Animated Indexed Stack 是一个针对 Flutter 默认的 IndexedStack 的替代品,它允许在不同小部件或屏幕之间进行简单的动画切换。

演示

演示

开始使用

首先,在你的 pubspec.yaml 文件中添加依赖:

dependencies:
  flutter:
    sdk: flutter
  # 添加此行
  easy_animated_indexed_stack:

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

使用方法

如上所述,该包提供了一个针对 Flutter 默认 IndexedStack 小部件的直接替换。这意味着你可以简单地将你的 IndexedStack 使用替换为 EasyAnimatedIndexedStack 实例。例如:

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

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  [@override](/user/override)
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  int selectedIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // 简单地将 `IndexedStack` 替换为 `EasyAnimatedIndexedStack`
        body: EasyAnimatedIndexedStack(
          // `selectedIndex` 是一个状态变量(整数)
          index: selectedIndex,
          // 自定义动画以满足你的需求
          animationBuilder: (context, animation, child) {
            return RotationTransition(
              turns: Tween<double>(begin: 1, end: 0).animate(animation),
              child: child,
            );
          },
          curve: Curves.easeInOut,
          duration: const Duration(milliseconds: 700),
          children: [
            ScreenA(onTap: () => setState(() => selectedIndex = 1)),
            ScreenB(onTap: () => setState(() => selectedIndex = 2)),
            ScreenC(onTap: () => setState(() => selectedIndex = 0)),
          ],
        ),
      ),
    );
  }
}

class ScreenA extends StatelessWidget {
  const ScreenA({super.key, required this.onTap});

  final VoidCallback onTap;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: InkWell(
          onTap: onTap,
          child: const Text('Screen A (Tap to change)'),
        ),
      ),
    );
  }
}

class ScreenB extends StatelessWidget {
  const ScreenB({super.key, required this.onTap});

  final VoidCallback onTap;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: InkWell(
          onTap: onTap,
          child: const Text('Screen B (Tap to change)'),
        ),
      ),
    );
  }
}

class ScreenC extends StatelessWidget {
  const ScreenC({super.key, required this.onTap});

  final VoidCallback onTap;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: InkWell(
          onTap: onTap,
          child: const Text('Screen C (Tap to change)'),
        ),
      ),
    );
  }
}

代码解释

  1. 导入必要的包

    import 'package:easy_animated_indexed_stack/easy_animated_indexed_stack.dart';
    import 'package:flutter/material.dart';
    
  2. 创建主应用类

    class MainApp extends StatefulWidget {
      const MainApp({super.key});
    
      [@override](/user/override)
      State<MainApp> createState() => _MainAppState();
    }
    
  3. 管理选中的索引

    class _MainAppState extends State<MainApp> {
      int selectedIndex = 0;
    
  4. 构建应用布局

    [@override](/user/override)
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: EasyAnimatedIndexedStack(
            index: selectedIndex,
            animationBuilder: (context, animation, child) {
              return RotationTransition(
                turns: Tween<double>(begin: 1, end: 0).animate(animation),
                child: child,
              );
            },
            curve: Curves.easeInOut,
            duration: const Duration(milliseconds: 700),
            children: [
              ScreenA(onTap: () => setState(() => selectedIndex = 1)),
              ScreenB(onTap: () => setState(() => selectedIndex = 2)),
              ScreenC(onTap: () => setState(() => selectedIndex = 0)),
            ],
          ),
        ),
      );
    }
    
  5. 定义各个屏幕类

    class ScreenA extends StatelessWidget {
      const ScreenA({super.key, required this.onTap});
    
      final VoidCallback onTap;
    
      [@override](/user/override)
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: InkWell(
              onTap: onTap,
              child: const Text('Screen A (Tap to change)'),
            ),
          ),
        );
      }
    }
    
    class ScreenB extends StatelessWidget {
      const ScreenB({super.key, required this.onTap});
    
      final VoidCallback onTap;
    
      [@override](/user/override)
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: InkWell(
              onTap: onTap,
              child: const Text('Screen B (Tap to change)'),
            ),
          ),
        );
      }
    }
    
    class ScreenC extends StatelessWidget {
      const ScreenC({super.key, required this.onTap});
    
      final VoidCallback onTap;
    
      [@override](/user/override)
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: InkWell(
              onTap: onTap,
              child: const Text('Screen C (Tap to change)'),
            ),
          ),
        );
      }
    }
    

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

1 回复

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


当然,以下是一个关于如何使用 easy_animated_indexed_stack 插件在 Flutter 中实现动画切换的代码示例。这个插件非常适用于需要在多个页面或视图之间进行平滑动画过渡的场景。

首先,你需要在你的 pubspec.yaml 文件中添加 easy_animated_indexed_stack 依赖:

dependencies:
  flutter:
    sdk: flutter
  easy_animated_indexed_stack: ^latest_version  # 请替换为最新版本号

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

接下来是一个完整的 Flutter 应用示例,展示了如何使用 easy_animated_indexed_stack 来实现动画切换:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Easy Animated Indexed Stack Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Easy Animated Indexed Stack Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SizedBox(
              height: 300,
              child: EasyAnimatedIndexedStack(
                index: _currentIndex,
                duration: Duration(milliseconds: 500),
                alignment: Alignment.center,
                crossFadeMode: CrossFadeMode.crossFade,
                children: <Widget>[
                  Container(
                    color: Colors.red,
                    child: Center(child: Text('Page 1')),
                  ),
                  Container(
                    color: Colors.green,
                    child: Center(child: Text('Page 2')),
                  ),
                  Container(
                    color: Colors.blue,
                    child: Center(child: Text('Page 3')),
                  ),
                ],
              ),
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List<Widget>.generate(3, (index) {
                return ElevatedButton(
                  onPressed: () {
                    setState(() {
                      _currentIndex = index;
                    });
                  },
                  child: Text('Page ${index + 1}'),
                );
              }),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖导入:在 pubspec.yaml 中添加 easy_animated_indexed_stack 依赖。
  2. 主应用结构
    • MyApp 是根组件,设置了应用的主题和主页。
    • MyHomePage 是一个有状态的组件,包含一个 Scaffold,其中包含应用栏和主体内容。
  3. 动画切换
    • 使用 EasyAnimatedIndexedStack 组件来包裹多个页面(这里用不同颜色的容器表示)。
    • index 属性用于指定当前显示的页面索引。
    • duration 属性用于设置动画持续时间。
    • crossFadeMode 属性用于设置交叉淡入淡出的模式(这里使用 crossFade)。
  4. 页面切换按钮
    • 使用 RowList.generate 创建了三个按钮,每个按钮对应一个页面。
    • 点击按钮时,调用 setState 方法更新 _currentIndex,从而触发动画切换。

运行这段代码,你将看到一个带有三个页面的应用,可以通过点击下面的按钮在页面之间进行平滑的动画切换。

回到顶部