rrubick
V2EX  ›  Flutter

请教一个简单的 flutter 问题,顺便求一个 flutter 交流群

  •  
  •   rrubick · May 23, 2022 · 3974 views
    This topic created in 1632 days ago, the information mentioned may be changed or developed.

    想学习 flutter 写一个小 app ,主页刚开始就碰到问题了:

    想在 Row 上加一个点击事件

    return Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text(widget.title),
            ),
            body: Container(
              padding: EdgeInsets.all(15),//这里想做一个 padding 但是未生效
              color: Colors.red,
              child: GestureDetector(
                onTap: () {
                  print('object');
                },
                child: Container(
                  child: Row(
                    children: [Text('默认'), Spacer(), Icon(Icons.arrow_right)],
                  ),
                ),
              ),
            )
            // This trailing comma makes auto-formatting nicer for build methods.
            );
    
    

    但是实际效果是只有点击 Text 和 Icon 的位置才会触发,不知道为啥。。。

    求一个 flutter 交流群,可以指点一下

    19 replies  •  2022-07-22 17:31:53 +08:00
    FrankFang128
    FrankFang128
    May 23, 2022
    1
    那你要用 Guesture 之类的组件
    rrubick
    rrubick
    OP
    May 23, 2022
    2
    @FrankFang128 #1
    是 GestureDetector 吗?我加到 Row 的父 View Container 上了,但是不是 Container 上所有位置都能触发,费解
    monkeyzsf
    monkeyzsf
    May 23, 2022
    3
    GestureDetector 包裹的 Container 加上 color 方便确认大小,我试了,点击整条 Container 都可以响应 onTap 事件,你确认是不是手指太粗点不到容器?
    monkeyzsf
    monkeyzsf
    May 23, 2022
    4
    你的代码,我试了 MacOS 、ChromeWeb 都没问题
    tribute
    tribute
    May 23, 2022❤️1
    5
    有一个 opaque 参数,好像叫这个
    lijy91
    lijy91
    May 23, 2022
    6
    欢迎加入 LeanFlutter 的讨论群 https://github.com/leanflutter
    isRealLeven
    isRealLeven
    May 23, 2022❤️1
    7
    GestureDetector 中属性 behavior: HitTestBehavior.opaque,
    rrubick
    rrubick
    OP
    May 23, 2022
    8
    @monkeyzsf #4
    我试了一下 ChromeWeb 也不可以啊。您别点击字或者 icon ,点击 Row 的中间没反应
    rrubick
    rrubick
    OP
    May 23, 2022
    9
    @lijy91 #6
    已加群
    ArcherDunes
    ArcherDunes
    May 26, 2022
    10
    诚招 Flutter 工程师,使用 IM SDK 开发聊天类 APP ,要求经验丰富,有实际上架产品,有原生开发经验加分。
    Alexrx
    Alexrx
    May 31, 2022
    11
    就是 7l 说的原因 具体的就是 flutter 默认一般都是 deferToChild,能不能通过命中测试取决于子组件,点中间空白的地方 hittest 没找到可以响应的子组件,row 自己本身 hittestself 也是 false 所以就没得效果 可以用 inkwell 代替 这个类是基于 gesture 封装的 内部默认 opaque
    rrubick
    rrubick
    OP
    Jun 1, 2022
    12
    @tion126x #11
    能否继承 row 并重写 hittest 来达到响应呢?
    version398919
    version398919
    Jun 10, 2022❤️1
    14
    1. padding 生效了,只是同一个 Container 的 color 不会收到 padding 影响。
    2. 把 GestureDetector 改成 InkWell 就行

    注:你包裹 Row 的 Container 是没意义的,可以去掉。Row 里的 Spacer 也可以去掉,用 MainAxisAlignment.spaceBetween 可以做到同样的效果。
    rrubick
    rrubick
    OP
    Jun 11, 2022
    15
    @version398919 #14
    flutter 界面稍微一写就一大段,请问有优雅的写法吗?
    Alexrx
    Alexrx
    Jun 18, 2022
    16
    @magic3584 可以的 但是 flutter 继承比较麻烦 一般不会这样写
    dabbit
    dabbit
    Jul 7, 2022
    17
    @magic3584 查查 lint ,可以规范写法(但是里面有些规则挺繁复的,按需自己编辑)。另外就是多写咯,写得多就知道怎么写能简单点。如果以前是写安卓的,可以试试这个 https://pub.flutter-io.cn/packages/flutter_constraintlayout ,我自己没试过,但是按照作者的说法,是值得一试的。
    gracewangabc
    gracewangabc
    Jul 8, 2022
    18
    诚招远程 Flutter ,16-45K ,Telegram:@somebody_abc, 微信:@somebody_abcde
    akaHenry
    akaHenry
    Jul 22, 2022
    19
    > flutter 技术群:

    - https://discord.com/invite/MnDA9pfWAW


    > 可以使用 niku 简写样式.

    https://github.com/saltyAom/niku
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   911 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 20:30 · PVG 04:30 · LAX 12:30 · JFK 15:30
    ♥ Do have faith in what you're doing.