You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter实现蓝色按钮仅首次点击递增计数器且保留切换可见性

解决方案

要实现蓝色按钮仅在首次被点击时递增计数器,同时保留按钮切换可见性的功能,只需添加一个状态变量标记蓝色按钮是否已被点击过,在点击逻辑中根据标记决定是否递增计数器即可:

  1. 新增布尔类型状态变量hasBlueButtonBeenClicked,初始值设为false,用于记录蓝色按钮是否已被点击过
  2. 在蓝色按钮的onTap回调中,仅当hasBlueButtonBeenClicked为false时递增计数器,并将标记设为true;无论是否递增计数器,都执行切换按钮可见性的逻辑

修改后的完整代码如下:

class Test extends StatefulWidget {
  const Test({Key? key}) : super(key: key);

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  int counter = 0;
  bool blueVisibility = true;
  bool redVisibility = false;
  // 新增变量:标记蓝色按钮是否已被点击过
  bool hasBlueButtonBeenClicked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: Container(
                    child: Visibility(
                      visible: blueVisibility,
                      child: InkWell(
                        onTap: () {
                          setState(() {
                            // 仅首次点击时递增计数器
                            if (!hasBlueButtonBeenClicked) {
                              counter++;
                              hasBlueButtonBeenClicked = true;
                            }
                            // 保留切换按钮可见性的逻辑
                            redVisibility = true;
                            blueVisibility = false;
                          });
                        },
                        child: Container(
                          margin: EdgeInsets.all(10),
                          color: Colors.blue,
                          height: 80,
                          width: 50,
                        ),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Visibility(
                      visible: redVisibility,
                      child: InkWell(
                        onTap: () {
                          setState(() {
                            redVisibility = false;
                            blueVisibility = true;
                          });
                        },
                        child: Container(
                          margin: EdgeInsets.all(10),
                          color: Colors.red,
                          height: 80,
                          width: 50,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: 50,
            width: 50,
            color: Colors.blueGrey,
            alignment: Alignment.center,
            child: Text(
              '$counter',
              style: TextStyle(
                fontSize: 40,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

修改后,蓝色按钮仅首次点击会触发计数器递增,后续点击(从红色按钮切换回来后)只会切换按钮可见性,不会再改变计数器数值。

内容的提问来源于stack exchange,提问作者Nomad09

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 15:05:30