如何在状态转换时添加淡入/淡出

时间:2019-06-23 12:53:59

标签: flutter dart

默认情况下,当StatefulWidget上的状态发生变化时,状态转换时没有动画。有什么办法吃吗?例如,在过渡时,我要淡出旧状态并淡入新状态。

我知道如何在路线之间添加过渡动画,但是找不到在状态过渡之间进行过渡的方法。

有一个例子,让我们使用Flutter默认应用程序:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

因此,每次我单击按钮时,数字都会立即更改。我想看到的是旧数字逐渐消失,新数字逐渐消失。

2 个答案:

答案 0 :(得分:1)

这取决于您如何管理它,它可以使用animationController并使用不透明度窗口小部件的值来工作,您需要添加Mixin with SingleTickerProviderStateMixin才能使动画起作用。

这是您可以等待的等待时间之一,但是如果您只想要淡入淡出效果,则可以使用AnimatedOpacity。

像这样:

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

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

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;
  double _opacity = 1;

  void _incrementCounter() {
    setState(() => _opacity = 0);
    Future.delayed(Duration(milliseconds: 500),() =>
      setState(() {
        _counter++;
        _opacity = 1;
      }
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            AnimatedOpacity(
              opacity: _opacity,
              duration: Duration(milliseconds: 500),
              child: Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

这是一种进行fadeOur / FadeIn的简单方法,就像我说的那样,如果您需要完整的动画并对其进行更多控制,则绝对应该使用AnimationController。

答案 1 :(得分:0)

This answer,使用AnimatedOpacity可以正常工作。但是我发现使用AnimatedSwitcher更简单,更干净。

(...)
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'You have pushed the button this many times:',
          ),
          AnimatedSwitcher(
            duration: const Duration(milliseconds: 500),
            transitionBuilder: (Widget child, Animation<double> animation) {
              return FadeTransition(child: child, opacity: animation);
            },
            child: Text(
              '$_counter',
              key: ValueKey<int>(_counter),
              style: Theme.of(context).textTheme.display1,
            ),
          ),
        ],
      ),
    ),
(...)

有关AnimatedSwitcher的更多信息,请参见here