账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    flutter 里 GestureDetector 中的onTap如和调用函数并且传入参数?
    39
    0

    想做一个点击下拉变成input框的搜索组件

    代码是这样的

    Widget build(BuildContext context) {
        return GestureDetector(
          onTap: handle_drop,
          child: Container(
            child: _isDrop?SearchInput():FocusButton(),
          ),
        );
      }

    通过_isDrop来判断是渲染input还是渲染展开按钮
    onTap是点击调用handle_drop

    bool _isDrop = false;
    void handle_drop() {
        setState(() {
          _isDrop = !_isDrop;
        });
      }

    我想改成这样

    class _SearchButtonState extends State<SearchButton> {
      bool _isDrop = false;
      void handle_drop(bool boolean) {
        setState(() {
          _isDrop = boolean;
        });
      }
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: handle_drop(true),
          child: Container(
            child: _isDrop?SearchInput():FocusButton(),
          ),
        );
      }
    }

    然后onTap就报错了

    The expression here has a type of 'void', and therefore cannot be used.

    我要怎么做才能onTap的时候带着参数值?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 酒⊙吧老手 普通会员 1楼
      在Flutter中,`GestureDetector`的`onTap`回调函数可以直接绑定一个带有参数的函数。以下是一个示例: ```dart class MyWidget extends StatelessWidget { void myFunction(String param1, int param2) { // 在这里处理点击事件并使用传入的参数 print('param1: $param1, param2: $param2'); } @override Widget build(BuildContext context) { return GestureDetector( onTap: () { // 调用函数并传入参数 myFunction('Hello', 123); }, child: Container( // ... ), ); } } ``` 在这个例子中,当用户点击`GestureDetector`包裹的组件时,将会调用`myFunction`函数,并传入预设的字符串'Hello'和整数123作为参数。
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部