Flutter InkWell 设置圆角背景色带波纹效果

1,558 阅读1分钟

Flutter InkWell 设置圆角背景色带波纹效果

screenshots.gif

InkWell 的 child 的 Container 设置了颜色的话,InkWell 的波纹效果就不起作用了。 想要实现设置颜色后带有波纹效果,需要特殊处理。

如图:

  • 第一个按钮:设置颜色后不再有波纹效果
  • 第二个按钮:不设置颜色会有波纹效果
  • 第三个按钮:实现设置颜色后有波纹效果

按钮三的代码:

Material(
  child: Ink(
    decoration: BoxDecoration(
      color: Colors.green,
      borderRadius: BorderRadius.circular(radius),
    ),
    child: InkWell(
      borderRadius: BorderRadius.circular(radius),
      onTap: () {
        // 处理
      },
      child: Container(
        padding: padding,
        constraints: BoxConstraints.loose(const Size(400, 36)),
        child: const Text('InkWell Container 圆角设置颜色 有波纹效果'),
      ),
    ),
  ),
)

完整示例代码:

import 'package:flutter/material.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final bool hasBorder = true;
  final double radius = 8;

  final padding = const EdgeInsets.all(8.0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            InkWell(
              onTap: () {
                // 处理
              },
              child: Container(
                padding: padding,
                decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.circular(radius),
                ),
                constraints: BoxConstraints.loose(const Size(400, 36)),
                child: const Text('InkWell Container 圆角设置颜色 无波纹效果'),
              ),
            ),
            const SizedBox(height: 8),
            InkWell(
              borderRadius: BorderRadius.circular(radius),
              onTap: () {
                // 处理
              },
              child: Container(
                padding: padding,
                constraints: BoxConstraints.loose(const Size(400, 36)),
                child: const Text('InkWell Container 圆角未设置颜色 有波纹效果'),
              ),
            ),
            const SizedBox(height: 8),
            Material(
              child: Ink(
                decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.circular(radius),
                ),
                child: InkWell(
                  borderRadius: BorderRadius.circular(radius),
                  onTap: () {
                    // 处理
                  },
                  child: Container(
                    padding: padding,
                    constraints: BoxConstraints.loose(const Size(400, 36)),
                    child: const Text('InkWell Container 圆角设置颜色 有波纹效果'),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

这个帖子的内容更详细:
flutter中使用InkWell给任意Widget添加点击事件_早起的年轻人的博客-CSDN博客_flutter 点击事件