flutter中的点击事件

点击水波纹效果

FlatButton;

FlatButton(
          //长按时颜色
          highlightColor:Colors.red,
          //点击的水波纹颜色
          splashColor: Colors.blue,
          onPressed: onPressed,
          child: Text(text,
              style: TextStyle(
                  fontSize:20,
                  fontWeight: FontWeight.w400)),
          shape: RoundedRectangleBorder(
            side: BorderSide(
                color:  Colors.black, style: BorderStyle.solid, width: 1),
          ),
        ))

InkWell:
这里有个问题,InkWell子布局添加颜色后点击就无法展示水波纹了,子布局将父布局覆盖了。

    InkWell(
        onTap: () {},
        highlightColor: Colors.blue,
        splashColor: Colors.red,
        child: Container(
          color: Colors.white,
          height: 100,
          width: 100,
          child: Text('点我'),
        ),
      )

点击区域问题

GestureDetector包裹子控件,点击空白区域点击无效解决方案:

  1. 设置GestureDetector的behavior属性:
    如:
 GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: callback,
              child: Container(
                padding: EdgeInsets.only(right: ScreenUtil().setHeight(30)),
                alignment: Alignment.centerRight,
                child: Image.asset(
                  LocalIcons.start_right,
                  width: ScreenUtil().setWidth(18),
                  height: ScreenUtil().setHeight(36),
                ),
              ),
            ),
  1. 给空白区域设置透明颜色。
    如:
GestureDetector(
              onTap: callback,
              child: Container(
                color: Colours.transparent,
                padding: EdgeInsets.only(right: ScreenUtil().setHeight(30)),
                alignment: Alignment.centerRight,
                child: Image.asset(
                  LocalIcons.start_right,
                  width: ScreenUtil().setWidth(18),
                  height: ScreenUtil().setHeight(36),
                ),
              ),
            ),

点击事件默认触发问题

RaisedButton(onPressed: _onclick()),

... 

_onclick(){}
  1. 当一个RaisedButton:点击事件传入一个方法时,出现按钮处于不可点击状态,报错但不会崩溃;

error:

When the exception was thrown, this was the stack:
flutter: #0      Element.markNeedsBuild. (package:flutter/src/widgets/framework.dart:3670:11)
flutter: #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3685:6)
flutter: #2      State.setState (package:flutter/src/widgets/framework.dart:1161:14)
flutter: #3      __ValueListenableDelegate&SingleValueDelegate&_ListenableDelegateMixin.startListening. (package:provider/src/listenable_provider.dart:134:36)
flutter: #4      _CountProvider&Object&ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier

解决方式1:需要在(){}

RaisedButton(
     onPressed: (){
          _onclick();
        }),

... 

_onclick(){}

解决方式2:

RaisedButton(
     onPressed: _onclick ,
... 

你可能感兴趣的:(flutter)