【问题标题】:How to add fade in/out on state transition如何在状态转换时添加淡入/淡出
【发布时间】:2019-11-05 12:33:31
【问题描述】:

默认情况下,当StatefulWidget 发生状态更改时,状态转换没有动画。有什么办法可以吃吗?例如,在转换时,我想淡出旧状态并淡入新状态。

我知道如何在路由之间添加过渡动画,但找不到在状态过渡之间执行此操作的方法。

举个例子,我们用 Flutter 默认的 app:

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.
    );
  }
}

因此,每次我单击按钮时,数字都会立即更改。我希望看到的是旧号码淡出,新号码淡入。

【问题讨论】:

  • AnimatedSwitcher 或 / 和 *Transition 小部件

标签: flutter dart


【解决方案1】:

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

【讨论】:

  • 谢谢,但不起作用。 AnimatedSwitcher(duration:constDuration(milliseconds:500),transitionBuilder:(Widgetchild,Animationanimation){returnFadeTransition(child:child,opacity:animation);},child:Icon(Icons.mic,size:24.0,color:颜色.red,),),
【解决方案2】:

这将取决于您如何管理它,它可以使用动画控制器并使用不透明度小部件的值,您需要添加 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),
      ),
    );
  }
}

正如我所说,如果你需要一个完整的动画和更多的控制,这是一个简单的方法来做一个淡入淡出/淡入淡出,你绝对应该使用 AnimationController。

【讨论】:

  • 您在示例中在哪里添加了“SingleTickerProviderStateMixin”?
  • 在示例中,我没有使用动画控制器,它不需要它,如果您需要像这样class _HomePageState extends State&lt;HomePage&gt; with SingleTickerProviderStateMixin 在您的类状态中添加它,但上面的示例代码不需要它.
猜你喜欢
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
相关资源
最近更新 更多