公司APP項(xiàng)目的登錄頁有一個(gè)人機(jī)驗(yàn)證功能,需要實(shí)現(xiàn)在登錄按鈕下邊放置一個(gè)webview來加載驗(yàn)證頁面岸晦,在點(diǎn)擊登錄按鈕時(shí)需要將點(diǎn)擊事件穿透到下邊的webview,由webview處理人機(jī)驗(yàn)證睛藻。
在flutter中也可以實(shí)現(xiàn)這樣的事件穿透启上,需要使用IgnorePointer或者AbsorbPointer,這兩個(gè)widget的作用是都是忽略命中測(cè)試店印,區(qū)別是IgnorePointer不可以在子組件當(dāng)中接收命中測(cè)試事件冈在,AbsorbPointer可以接收命中測(cè)試。
flutter具體實(shí)現(xiàn):
使用IgnorePointer
return Listener(
child: Stack(
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints.tight(Size(ScreenUtil().screenWidth - 32, 52.0)),
child: WebView(
onWebViewCreated: (c){
_controller = c;
},
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
),
),
// IgnorePointer
IgnorePointer(
child: Container(
padding: EdgeInsets.zero,
height: 52,
width: ScreenUtil().screenWidth - 32,
child: CupertinoButton(
child: Text(
'登錄',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
onPressed: () {
//不會(huì)打印
print('點(diǎn)擊了登錄按鈕');
},
color: Colors.blue,
),
),
),
],
),
onPointerDown: (event){
print("點(diǎn)擊了整體區(qū)域");
},
);
使用AbsorbPointer
return Listener(
child: Stack(
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints.tight(Size(ScreenUtil().screenWidth - 32, 52.0)),
child: WebView(
onWebViewCreated: (c){
_controller = c;
},
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
),
),
// AbsorbPointer
AbsorbPointer(
absorbing: false,//需要設(shè)置這個(gè)屬性才可以使子組件接收命中測(cè)試
child: Container(
padding: EdgeInsets.zero,
height: 52,
width: ScreenUtil().screenWidth - 32,
child: CupertinoButton(
child: Text(
'登錄',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
onPressed: () {
//會(huì)打印
print('點(diǎn)擊了登錄按鈕');
},
color: Colors.blue,
),
),
),
],
),
onPointerDown: (event){
print("點(diǎn)擊了整體區(qū)域");
},
);