为什么可见性在颤振中不起作用?

Abi*_*san 1 visibility flutter

我想通过单击 AppBar 的 来隐藏我的图像容器IconButton。但我的 AppBarIconButton不起作用。 我想,我的代码犯了一个错误......

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

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    bool viewVisible = true;

  /*  void showWidget() {
      setState(() {
        viewVisible = true;
      });
      print("Pressed");
    }
*/
    void hideWidget() {
      setState(() {
        viewVisible = false;
        print("Work");
      });
    }


    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: Text("Icon & Image Shadow Demo"),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.arrow_drop_down_circle),
                onPressed: hideWidget,
              )
            ],
          ),
          body: Stack(
            children: <Widget>[
             ListView(
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                          ),
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                            shadowColor: Colors.black,
                          ),
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                            shadowColor: Colors.black,
                            showShadow: false,
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.0,
                      ),
                      Visibility(
                        maintainAnimation: true,
                        maintainSize: true,
                        maintainState: true,

                        visible: viewVisible,
                        child: Container(
                          decoration: BoxDecoration(
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.8),
                                spreadRadius: 5,
                                blurRadius: 3,
                                offset:
                                    Offset(5, 7), // changes position of shadow
                              ),
                            ],
                          ),
                          child: Image.asset("assets/images/download.png"),
                        ),
                      )
                    ],
                  ),
                ],
              )
            ],

          )),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Khe*_*rel 5

移到bool viewVisible = true;构建方法之外,最好也移动 hideWidget,否则你会在每次构建时一次又一次地声明该函数

class _HomePageState extends State<HomePage> {

  bool viewVisible = true;


  void hideWidget() {
    setState(() {
      viewVisible = false;
      print("Work");
    });
  }


  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: Text("Icon & Image Shadow Demo"),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.arrow_drop_down_circle),
                onPressed: hideWidget,
              )
            ],
          ),
          body: Stack(
            children: <Widget>[
             ListView(
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                          ),
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                            shadowColor: Colors.black,
                          ),
                          IconShadowWidget(
                            Icon(
                              Icons.add_circle,
                              color: Colors.red,
                              size: 100.0,
                            ),
                            shadowColor: Colors.black,
                            showShadow: false,
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.0,
                      ),
                      Visibility(
                        maintainAnimation: true,
                        maintainSize: true,
                        maintainState: true,

                        visible: viewVisible,
                        child: Container(
                          decoration: BoxDecoration(
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.8),
                                spreadRadius: 5,
                                blurRadius: 3,
                                offset:
                                    Offset(5, 7), // changes position of shadow
                              ),
                            ],
                          ),
                          child: Image.asset("assets/images/download.png"),
                        ),
                      )
                    ],
                  ),
                ],
              )
            ],

          )),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)