繁体   English   中英

使用 FlatButton 隐藏可见性 Flutter

[英]Hide Visibility with FlatButton Flutter

如何更改 Flutter 中的可见性属性? 我自己尝试了一些东西,但没有任何效果。 State Widget 已设置为 StateFullWidget。 有人能帮我吗?

class _StartGameState extends State<StartGame> {

@override
  Widget build(BuildContext context) {
    bool _visibleStartGame = true;
    return Container(
      child: Column(
        children: [
          Visibility(
            visible: _visibleStartGame,//This has to been set to false.
            child: FlatButton(
              onPressed: () {
                setState(() {
                  _visibleStartGame = false;
                  print(_visibleStartGame);
                });
              },
              child: Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                color: Color.fromRGBO(0, 0, 0, .5),
                child: Center(
                  child: Text(
                    'Touch to start',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
          Expanded(
            child: Text('Hello, World!'),
          )
        ],
      ),
    );
  }
}

您需要在构建 function 之外创建变量。 因为 build function 每次调用 setState 时都会调用,然后您的变量将再次设置为 true。 此代码将起作用

class _StartGameState extends State<StartGame> {
bool _visibleStartGame = true;
@override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Visibility(
            visible: _visibleStartGame,//This has to been set to false.
            child: FlatButton(
              onPressed: () {
                setState(() {
                  _visibleStartGame = false;
                  print(_visibleStartGame);
                });
              },
              child: Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                color: Color.fromRGBO(0, 0, 0, .5),
                child: Center(
                  child: Text(
                    'Touch to start',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
          Expanded(
            child: Text('Hello, World!'),
          )
        ],
      ),
    );
  }
}

暂无
暂无

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

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