笔记类型,所以排版不是很好。

要求:
在Flutter中,通过点击图片,显示和隐藏组件。
Container中的内容如下:

Container(
      margin: const EdgeInsets.only(
         left: 12.0, right: 12.0),
      child: GestureDetector(
                child: Image.asset(
                          "assets/images/cat.jpg",
                          width: 24.0,
                         height: 24.0,
                         ),
                         // onTap引用_onTap,内容在下面.
                onTap: _onTap
       ),
),

_onTap函数,注意,这里不需要使用SharedPreferences.

// 定义isOff
  bool isOff=false;
  
  _onTap()  async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    isOff = prefs.getBool("isOff");
    print("当前的状态为" + isOff.toString());
    setState(() {
      isOff = !isOff;
      prefs.setBool("isOff", isOff);
    });
  }

需要操作的内容:

              Offstage(
                offstage: isOff,
                child: Container(
                  height: 100,
                  color: Colors.red,
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                color: Colors.green,
                height: 100.0,
                child: Offstage(
                  offstage: isOff,
                  child:
                  Text("这里可以显示吗?"),

                ),

好了,基本上这样就实现了。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐