首页 > 解决方案 > Flutter:如何显示和隐藏 Lottie 动画

问题描述

我已经成功地展示了 Lottie,如下所示:

在此处输入图像描述

但问题是,如何通过按钮触发显示和隐藏 Lottie?例如,Lottie 没有显示,但是当我点击 时Show Lottie button,Lottie 会显示,而当我点击 时Hide Lottie button,Lottie 会隐藏。

这是我的完整代码:

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'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

标签: flutterlottie

解决方案


我通过使用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'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

推荐阅读