【问题标题】:Flutter - Inkwell's ripple with FlatButton as child doesnt workFlutter - Inkwell 与 FlatButton 作为孩子的涟漪不起作用
【发布时间】:2021-02-02 08:12:42
【问题描述】:

我有以下代码,我想要实现的是我希望我的 FlatButton 具有原生涟漪效果。默认情况下,颤振材质会产生飞溅效果,感觉不像原生。当我按下按钮时,我试图用墨水瓶包裹我的 FlatButton 以某种方式解决这个问题,它忽略了我的墨水瓶小部件。我该如何解决这个问题?

InkWell(
        onTap: () => true,
        splashFactory: InkRipple.splashFactory,
        child: FlatButton(
          color: Theme.of(context).colorScheme.primary,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          child: Text("Im a text"),
          onPressed: () {},
        ));

【问题讨论】:

    标签: flutter


    【解决方案1】:

    所以问题在于有一个函数被传递给FlatButtononPressed

    您只需将null 传递给FlatButtononPressed,触发器就会转到InkWellonTap

    试试这个:

    
    InkWell(
            onTap: () => true,
            splashFactory: InkRipple.splashFactory,
            child: FlatButton(
              color: Theme.of(context).colorScheme.primary,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
              child: Text("Im a text"),
              onPressed: null,
            ));
    

    更新: 在 Material 小部件中添加 InkWell 并将 FlatButton 替换为 Container 小部件

    Material(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          color: Theme.of(context).colorScheme.primary,
          child: InkWell(
            onTap: () => true,
            splashFactory: InkRipple.splashFactory,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
              child: Text("Im a text"),
            ),
          ),
        );
    

    【讨论】:

    • 我试过这个,但后来我失去了“颜色:Theme.of(context).colorScheme.primary”的颜色,按钮变得透明
    • 好吧,那你为什么需要 InkWell 中的 FlatButton?
    • InkWell with splashFactory: InkRipple.splashFactory 参数添加原生 android 波纹动画。这就是为什么我需要使用墨水瓶。 FlatButton 有自定义颤动动画
    • 尝试删除 FlatButton 并将文本放入 Container 小部件中,然后将 InkWell 包装在 Material 小部件中。查看更新答案。
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 2020-02-05
    相关资源
    最近更新 更多