Flattern: Flip-Animation

Die Übersetzung wurde im Rahmen des Kurses " Flutter Mobile Developer " erstellt .



Wir laden alle Ankömmlinge zum zweiten Tag des zweitägigen Online-Intensivkurses
„Wir erstellen eine Anwendung auf Flutter für Web, iOS und Android“ ein . Wir schreiben die Anwendung weiter (dies wird eine Netzwerkanwendung sein: Laden einer Liste von Entitäten + Filtern dieser Entitäten + Erstellen eines Einstellungsblocks in der Anwendung, in dem Sie das Anwendungsthema (Farben) ändern können). Begleiten Sie uns!






Als ich das Widget zum ersten Mal sah AnimationSwitcher



, dachte ich, ich könnte es umdrehen, indem ich seinen Rücken öffne.





Ich habe mich geirrt: AnimationSwitcher



Ermöglicht ... das Wechseln zwischen verschiedenen Widgets mit der von Ihnen angegebenen Animation (die Standardanimation ist ein Überblendungsübergang). Diese Komponente ist für diesen Zweck zu vielseitig.





Ich musste sorgfältig lesen ...

, , .





flutter, -, animated_card_switcher, , , , .





:









  • AnimationSwitcher





  • .









, .





, , , , AnimationSwitcher



, (, , ).





Wie ein Stück Kuchen!
!

, :





Widget __buildLayout({Key key, String faceName, Color backgroundColor}) {
  return Container(
    key: key,
    decoration: BoxDecoration(
      shape: BoxShape.rectangle,
      borderRadius: BorderRadius.circular(20.0),
      color: backgroundColor,
    ),
    child: Center(
      child: Text(faceName.substring(0, 1), style: TextStyle(fontSize: 80.0)),
    ),
  );
      
      



, :





Widget _buildFront() {
  return __buildLayout(
    key: ValueKey(true),
    backgroundColor: Colors.blue,
    faceName: "F",
  );
}

Widget _buildRear() {
  return __buildLayout(
    key: ValueKey(false),
    backgroundColor: Colors.blue.shade700,
    faceName: "R",
  );
}
      
      



AnimationSwitcher

AnimationSwitcher



.





StatefulWidget



build



, , .





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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  bool _displayFront;
  bool _flipXAxis;

  @override
  void initState() {
    super.initState();
    _displayFront = true;
    _flipXAxis = true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.widget.title),
        centerTitle: true,
      ),
      body: Center(
        child: Container(
          constraints: BoxConstraints.tight(Size.square(200.0)),
          child: _buildFlipAnimation(),
      ),
    );
  }
}
      
      



_build Flip Animation



, .





:





Widget _buildFlipAnimation() {
  return GestureDetector(
    onTap: () => setState(() =>_showFrontSide = !_showFrontSide),
    child: AnimatedSwitcher(
      duration: Duration(milliseconds: 600),
      child: _showFrontSide ? _buildFront() : _buildRear(),
    ),
  );
}
      
      



, , , . , .





Zumindest passiert etwas.
, - .

Y. , AnimationSwitcher



, transitionBuilder



.





, : 180° (pi). AnimatedBuidler



Transform



.





Widget __transitionBuilder(Widget widget, Animation<double> animation) {
  final rotateAnim = Tween(begin: pi, end: 0.0).animate(animation);
  return AnimatedBuilder(
    animation: rotateAnim,
    child: widget,
    builder: (context, widget) {
      return Transform(
        transform: Matrix4.rotationY(value),
        child: widget,
        alignment: Alignment.center,
      );
    },
  );
}
      
      



, , . , .





Der letzte Teil erscheint zu schnell.
.

, .





:





  • : .





  • .





layoutBuilder



AnimationSwitcher



.





layoutBuilder: (widget, list) => Stack(children: [widget, ...list]),
      
      



, , pi/2 0.0. () .





Widget __transitionBuilder(Widget widget, Animation<double> animation) {
  final rotateAnim = Tween(begin: pi, end: 0.0).animate(animation);
  return AnimatedBuilder(
    animation: rotateAnim,
    child: widget,
    builder: (context, widget) {
      final isUnder = (ValueKey(_showFrontSide) != widget.key);
      final value = isUnder ? min(rotateAnim.value, pi / 2) : rotateAnim.value;
      return Transform(
        transform: Matrix4.rotationY(value),
        child: widget,
        alignment: Alignment.center,
      );
    },
  );
}
      
      



, ! , , "tilt" () .





Wir brauchen Tiefe ... Süße, kleine, flauschige Tiefe.
... .

"" 0,0 . , , . , 0,2, -0,2.





, Matrix4, .





Widget __transitionBuilder(Widget widget, Animation<double> animation) {
  final rotateAnim = Tween(begin: pi, end: 0.0).animate(animation);
  return AnimatedBuilder(
    animation: rotateAnim,
    child: widget,
    builder: (context, widget) {
      final isUnder = (ValueKey(_showFrontSide) != widget.key);
      var tilt = ((animation.value - 0.5).abs() - 0.5) * 0.003;
      tilt *= isUnder ? -1.0 : 1.0;
      final value = isUnder ? min(rotateAnim.value, pi / 2) : rotateAnim.value;
      return Transform(
        transform: Matrix4.rotationY(value)..setEntry(3, 0, tilt),
        child: widget,
        alignment: Alignment.center,
      );
    },
  );
}
      
      



Matrix4 : https://medium.com/flutter-community/advanced-flutter-matrix4-and-perspective-transformations-a79404a0d828.





, , AnimationSwitcher.





Kurven sind immer besser!
!

:





Widget _buildFlipAnimation() {
  return GestureDetector(
    onTap: _switchCard,
    child: AnimatedSwitcher(
      duration: Duration(milliseconds: 4600),
      transitionBuilder: __transitionBuilder,
      layoutBuilder: (widget, list) => Stack(children: [widget, ...list]),
      child: _showFrontSide ? _buildFront() : _buildRear(),
      switchInCurve: Curves.easeInBack,
      switchOutCurve: Curves.easeOutBack,
    ),
  );
}
      
      



.





Oh nein….
….

, , , . .





Animation im Multi-Frame-Modus ...

, flipped



, .





switchInCurve: Curves.easeInBack,
switchOutCurve: Curves.easeInBack.flipped,
      
      



Sloooow Mooooo (jetzt perfekt)
Sloooow Mooooo ( )

, : 30 ( ), ( ).





, . , (), . , , , 6 , 1 2 ...





Kopieren Sie dieses Beispiel und fügen Sie es mit Bedacht ein :)
:)

. , ( ) “ ”, - , !





, , .





Flutter Twitter






"Flutter Mobile Developer"





- « Flutter Web, iOS Android»








All Articles