代码之家  ›  专栏  ›  技术社区  ›  TSR

颤振条件动画

  •  0
  • TSR  · 技术社区  · 7 年前

    我有一个 Stack Text FadeTransition 对象在堆栈之外,我有5个 RaisedButton 文本 1的不透明度为 1 其余的都有 0 不透明度单击按钮时,它映射的文本的不透明度将变为 1. 剩下的就变成 . 为此,我创建了5个不同的 AnimationController 我不确定这是在颤振中这样做的正确方法。 我相信一定有更简单的方法。 hasViewedText1 这是真的。)

    enter image description here

    import 'dart:core';
    import 'package:flutter/material.dart';
    
    class Test extends StatefulWidget {
      @override
      _State createState() {
        return _State();
      }
    }
    
    class _State extends State<Test> with TickerProviderStateMixin {
      AnimationController _animationController1;
      AnimationController _animationController2;
      AnimationController _animationController3;
      AnimationController _animationController4;
      AnimationController _animationController5;
    
      @override
      void initState() {
        super.initState();
        _animationController1 = new AnimationController(
          vsync: this,
          duration: new Duration(seconds: 1),
        );
        _animationController2 = new AnimationController(
          vsync: this,
          duration: new Duration(seconds: 1),
        );
        _animationController3 = new AnimationController(
          vsync: this,
          duration: new Duration(seconds: 1),
        );
        _animationController4 = new AnimationController(
          vsync: this,
          duration: new Duration(seconds: 1),
        );
        _animationController5 = new AnimationController(
          vsync: this,
          duration: new Duration(seconds: 1),
        );
        _everyThingBackward();
        _animationController1.forward();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: new AppBar(
            elevation: 0.5,
            title: new Text(
              "Testing views",
              style: Theme.of(context).textTheme.title,
            ),
          ),
    //      body: _buildBodyAnimationTest(),
    //      body:  _buildTuto(),
    //      body: _builtLayoutBuilder(),
          body: _builtLayoutConditionalAnimation(),
        );
      }
    
    
      Widget _builtLayoutConditionalAnimation() {
        return new Column(
          children: <Widget>[
            new Column(
              children: <Widget>[
                new RaisedButton(child: new Text("Button 1"), onPressed: () {
                  _everyThingBackward();
                  _animationController1.forward();
                }),
                new RaisedButton(child: new Text("Button 2"), onPressed: () {
                  _everyThingBackward();
                  _animationController2.forward();
                }),
                new RaisedButton(child: new Text("Button 3"), onPressed: () {
                  _everyThingBackward();
                  _animationController3.forward();
                }),
                new RaisedButton(child: new Text("Button 4"), onPressed: () {
                  _everyThingBackward();
                  _animationController4.forward();
                }),
                new RaisedButton(child: new Text("Button 5"), onPressed: () {
                  _everyThingBackward();
                  _animationController5.forward();
                }),
              ],
            ),
            new Stack(
              children: <Widget>[
                FadeTransition(opacity: _animationController1,child: new Text('Text 1 is clicked')),
                FadeTransition(opacity: _animationController2,child: new Text('Text 2 is clicked')),
                FadeTransition(opacity: _animationController3,child: new Text('Text 3 is clicked')),
                FadeTransition(opacity: _animationController4,child: new Text('Text 4 is clicked')),
                FadeTransition(opacity: _animationController5,child: new Text('Text 5 is clicked')),
              ],
            ),
          ],
        );
      }
    
      void _everyThingBackward() {
        _animationController1.reverse();
        _animationController2.reverse();
        _animationController3.reverse();
        _animationController4.reverse();
        _animationController5.reverse();
    
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Jordan Davies    7 年前

    AnimatedSwitcher 小装置, link to docs

    以下是一个完整的示例:

    import 'package:flutter/material.dart';
    
    void main() => runApp(App());
    
    class App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: SafeArea(child: Center(child: Test())),
          ),
        );
      }
    }
    
    class Test extends StatefulWidget {
      @override
      _TestState createState() => _TestState();
    }
    
    class _TestState extends State<Test> {
      Widget _child = Text(
        "No Button Tapped!",
        key: UniqueKey(),
      );
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: <Widget>[
            RaisedButton(
              child: Text("Button 1"),
              onPressed: () {
                setState(() {
                  _child = Text(
                    "Button 1 Tapped!",
                    key: UniqueKey(),
                  );
                });
              },
            ),
            RaisedButton(
              child: Text("Button 2"),
              onPressed: () {
                setState(() {
                  _child = Text(
                    "Button 2 Tapped!",
                    key: UniqueKey(),
                  );
                });
              },
            ),
            RaisedButton(
              child: Text("Button 3"),
              onPressed: () {
                setState(() {
                  _child = Text(
                    "Button 3 Tapped!",
                    key: UniqueKey(),
                  );
                });
              },
            ),
            AnimatedSwitcher(
              duration: Duration(milliseconds: 200),
              child: _child,
            ),
          ],
        );
      }
    }
    

    https://medium.com/flutter-community/what-do-you-know-about-aniamtedswitcher-53cc3a4bebb8