简体   繁体   English

Flutter:如何显示和隐藏 Lottie animation

[英]Flutter: How to show and hide the Lottie animation

I already success to show the Lottie like below:我已经成功地展示了 Lottie,如下所示:

在此处输入图像描述

But the question is, how to trigger to show and hide the Lottie with a button?但问题是,如何通过按钮触发显示和隐藏 Lottie? For example, the Lottie is not showing, but when I clicking Show Lottie button , the Lottie will show, and when I clicking Hide Lottie button , the Lottie will hide.例如,Lottie 没有显示,但是当我单击Show Lottie button时,Lottie 会显示,当我单击Hide Lottie button时,Lottie 会隐藏。

This is my full code:这是我的完整代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Lottie Flutter'),
        ),
        body: Container(
          width: double.infinity,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              SizedBox(
                width: 50,
                height: 50,
                child: Lottie.asset(
                  'assets/10219-notification-dot.json',
                ),
              ),
              Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {},
                    child: Text('Show Lottie'),
                  ),
                  RaisedButton(
                    onPressed: () {},
                    child: Text('Hide Lottie'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

I solved this with using setState and Visibility widget, this is my code:我使用setStateVisibility小部件解决了这个问题,这是我的代码:

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

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  var _isShow = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Lottie Flutter'),
        ),
        body: Container(
          width: double.infinity,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              SizedBox(
                width: 50,
                height: 50,
                child: Visibility(
                  visible: _isShow,
                  child: Lottie.asset(
                    'assets/10219-notification-dot.json',
                  ),
                ),
              ),
              Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {
                      setState(() {
                        _isShow = true;
                      });
                    },
                    child: Text('Show Lottie'),
                  ),
                  RaisedButton(
                    onPressed: () {
                      setState(() {
                        _isShow = false;
                      });
                    },
                    child: Text('Hide Lottie'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

use a showDialogue method.使用 showDialogue 方法。 child -> GestureDetector, which when tapped do a pop. child -> GestureDetector,当点击它时会弹出。

Code is below:代码如下:

showDialog(
           context: context,
           builder: (context) {
             return GestureDetector(
               onTap: () {
                 Navigator.pop(context);
               },
               child: Lottie.asset('assets/animations/success.json',
                   repeat: false, animate: true),
             );
           });
     });

You can use a bool and if else condition to show it.您可以使用 bool 和 if else 条件来显示它。 Below code can give an idea perhaphs.下面的代码可以给出一个想法。

  var show = true;
    ...
  class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
    ....
     show ?? Lottie.asset(
                      'assets/10219-notification-dot.json',
                    ),
    ....

    RaisedButton(
                        onPressed: () {
    setState(){show = true}
    },
                        child: Text('Show Lottie'),
                      ),
                      RaisedButton(
                        onPressed: () {
    setState(){show = false}
    },
                        child: Text('Hide Lottie'),
                      ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM