flutter InkWell与水波纹

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget 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);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          MyInkWellButton(),
          Builder(
            //如果 build 函数返回一个 Scaffold 对象,则由于 Scaffold 对象是这个 Widget 的子对象,
            // 所以使用这个 build 的 BuildContext 参数是不能查找到 ScaffoldState 对象的,
            // 这个时候,通过在 Scaffold 中使用一个 Builder 来提供一个新的 BuildConext :
            // Create an inner BuildContext so that the onPressed methods
            // can refer to the Scaffold with Scaffold.of().
            builder: (BuildContext context) {
              return Center(
                child: RaisedButton(
                  child: Text('RaisedButton'),
                  onPressed: () {
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: Text('Tap RaisedButton'),
                    ));
                  },
                ),
              );
            },
          ),
          Builder(
            //如果 build 函数返回一个 Scaffold 对象,则由于 Scaffold 对象是这个 Widget 的子对象,
            // 所以使用这个 build 的 BuildContext 参数是不能查找到 ScaffoldState 对象的,
            // 这个时候,通过在 Scaffold 中使用一个 Builder 来提供一个新的 BuildConext :
            // Create an inner BuildContext so that the onPressed methods
            // can refer to the Scaffold with Scaffold.of().
            builder: (BuildContext context) {
              return Center(
                child: FlatButton(
                  child: Text('FlatButton'),
                  onPressed: () {
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: Text('Tap FlatButton'),
                    ));
                  },
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}

class MyInkWellButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //触摸水波效果:
    //1.创建一个可点击的Widget。
    //2.将它包裹在一个InkWell中来管理点击回调和水波动画。
    return InkWell(
      onTap: () {
        Scaffold.of(context).showSnackBar(SnackBar(
          content: Text('Tap MyInkWellButton'),
        ));
      },
      child: Container(
        padding: EdgeInsets.all(12.0),
        child: Text('MyInkWellButton'),
      ),
    );
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值