在设计应遵循Material Design指南的应用程序时,我们希望在点击时将水波动画添加到Widgets。
Flutter提供了InkWellWidget来实现这个效果。
步骤
- 创建一个可点击的Widget。
- 将它包裹在一个
InkWell中来管理点击回调和水波动画。
// The InkWell Wraps our custom flat button Widgetnew InkWell(// When the user taps the button, show a snackbaronTap: () {Scaffold.of(context).showSnackBar(new SnackBar(content: new Text('Tap'),));},child: new Container(padding: new EdgeInsets.all(12.0),child: new Text('Flat Button'),),);
完整的例子
import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {final title = 'InkWell Demo';return new MaterialApp(title: title,home: new MyHomePage(title: title),);}}class MyHomePage extends StatelessWidget {final String title;MyHomePage({Key key, this.title}) : super(key: key);@overrideWidget build(BuildContext context) {return new Scaffold(appBar: new AppBar(title: new Text(title),),body: new Center(child: new MyButton()),);}}class MyButton extends StatelessWidget {@overrideWidget build(BuildContext context) {// The InkWell Wraps our custom flat button Widgetreturn new InkWell(// When the user taps the button, show a snackbaronTap: () {Scaffold.of(context).showSnackBar(new SnackBar(content: new Text('Tap'),));},child: new Container(padding: new EdgeInsets.all(12.0),child: new Text('Flat Button'),),);}}
