当我们在开发遵循 Material Design 规范应用的时候,我们可能会需要为某个 Widgets 的点击加入涟漪效果。
Flutter 提供了 InkWell Widget 来实现这个功能。
步骤
创建一个想要点击的 Widget
用
InkWellWidget 包裹它,并设置回调函数, 就可以显示涟漪动画了。
// The InkWell Wraps our custom flat button WidgetInkWell(// When the user taps the button, show a snackbaronTap: () {Scaffold.of(context).showSnackBar(SnackBar(content: Text('Tap'),));},child: Container(padding: EdgeInsets.all(12.0),child: Text('Flat Button'),),);
完整例子
import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {final title = 'InkWell Demo';return MaterialApp(title: title,home: MyHomePage(title: title),);}}class MyHomePage extends StatelessWidget {final String title;MyHomePage({Key key, this.title}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text(title),),body: Center(child: MyButton()),);}}class MyButton extends StatelessWidget {@overrideWidget build(BuildContext context) {// The InkWell Wraps our custom flat button Widgetreturn InkWell(// When the user taps the button, show a snackbaronTap: () {Scaffold.of(context).showSnackBar(SnackBar(content: Text('Tap'),));},child: Container(padding: EdgeInsets.all(12.0),child: Text('Flat Button'),),);}}

当前内容版权归 flutter-io.cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 flutter-io.cn .