簡體   English   中英

如何在 Flutter 中將 Container 上的漣漪效果顯示為 IconButton 子項?

[英]How to show ripple effect on Container as IconButton child in Flutter?

標准 IconButton 不會對孩子產生任何連鎖反應:

IconButton(
 icon: Container(color: Colors.red,),
 onPressed: () {},
)

所以我嘗試用 Stack 修復它:

class MyIconButton extends StatelessWidget {
  const MyIconButton({Key key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
      return Container(
        height: 100,
        width: 100,
        child: Stack(
          fit: StackFit.loose,
          children: [
            Positioned.fill(
              child: Container(
                width: 40,
                height: 40,
                color: Colors.deepPurple, 
              ),
            ),
            Positioned.fill(
              child: Material(
                type: MaterialType.transparency,
                shape: const CircleBorder(),
                child: IconButton(
                  splashRadius: 50,
                  icon: Container(), 
                  onPressed: () {
                  },
                ),
              ),
            )
          ],
        ),
      );
    }
  }

但是這個小部件需要定義容器的大小,當我定義該大小時,我無法在該容器之外顯示漣漪效應。 我願意接受建議。

Ink小部件替換您的Container

IconButton(
  onPressed: () {},
  icon: Ink(color: Colors.red),
)

墨水

用於在 Material 小部件上繪制圖像和其他裝飾的便捷小部件,以便 InkWell 和 InkResponse 飛濺將呈現在它們之上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM