题纲:
- WillPopScope
- GestureDetector
1.WillPopScope拦截、监听返回事件
初始化方法,其中onWillPop参数类型是一个Future<bool>的方法.
onWillPop该方法是可以实现安卓手机实体返回键的拦截、监听
const WillPopScope({
Key key,
@required this.child,
@required this.onWillPop,
}) : assert(child != null),
super(key: key);
final Widget child;
final WillPopCallback onWillPop;
typedef WillPopCallback = Future<bool> Function();
//举个栗子
final _navKey = GlobalKey<NavigatorState>();
Future<bool>_onWillPop() async{
final b = await _navKey.currentState.maybePop();
//返回true:可以在主Router上返回
//fasel: 拦截返回事件
return Future.value(!b);
}
2.GestureDetector手势监听
类似于iOS中UIControl,监听各种手势交互。
GestureDetector(
onTap: ()=>{ setState(()=>{ _active = !_active }) },
onTapDown: (details) {
//参数details.globalPosition,代表指针和屏幕的绝对位置
print(details.globalPosition);
setState(()=>{ _hold = true });
}
onTapUp: (details)=>{ setState(()=>{ _hold = false }) },
onTapCancel: ()=>{setState(()=>{ _hold = false }) },
//使用时必须传一个widget。
child: Container(
alignment: Alignment.center,
child: Text('GestureDetector'),
width: 150,
height: 150
),
)
注:
我会把一些自己使用过的、好用的组件整理到这里帮助自己学习和记忆。