我正在创建一个按钮的叠加层onClick,在下面的代码中可以正常工作,但是我想通过在其外部单击来关闭该叠加层。
参考代码:
我使用 OverlayEntry
使用偏移量来设置覆盖位置,该偏移量可以在粘贴在六个按钮中的任何一个上时使用。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: GesturePositionDetector(),
);
}
}
class GesturePositionDetector extends StatelessWidget {
OverlayState overlayState;
OverlayEntry _overlayEntry;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_getButtons([1, 2, 3], context),
_getButtons([4, 5, 6], context),
_getButtons([7, 8, 9], context)
],
)),
);
}
Widget _getButtons(List<int> labels, BuildContext context) {
var listOfButtons = List<Widget>();
labels.forEach((int label) {
listOfButtons.add(_getButtonView('Button $label', context, label));
});
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: listOfButtons);
}
Widget _getButtonView(String label, BuildContext context, int index) {
return GestureDetector(
child: Container(
height: 50,
width: 150,
margin : EdgeInsets.fromLTRB(15, 25, 15, 20),
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Center(
child: Text(
label,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
onTapDown: (details) {
onTap(details, context, index);
},
);
}
onTap(TapDownDetails details, context, int index) {
var size = MediaQuery.of(context).size;
var offset = details.globalPosition;
_overlayEntry?.remove();
overlayState = Overlay.of(context);
_overlayEntry = new OverlayEntry(
builder: (BuildContext context) => Positioned(
left: offset.dx + 300 >= size.width ? offset.dx - 300 : offset.dx,
top: offset.dy + 200 >= size.height ? offset.dy - 200 : offset.dy,
child: Material(
color: Colors.transparent,
child: Container(
width: 300,
height: 200,
child: Container(
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 5.0,
),
]),
margin: EdgeInsets.symmetric(horizontal: 20),
padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
alignment: WrapAlignment.center,
direction: Axis.vertical,
spacing: 10,
children: <Widget>[
Text(
'Main Text',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 25),
),
Text('sub text 1'),
Text('sub text 2'),
Text('sub text 3'),
Text('sub text 4')
],
))),
)));
overlayState.insert(_overlayEntry);
}
}
答案 0 :(得分:2)
用手势检测器包裹身体,在身体轻击时,轻击事件将触发,在轻击事件方法中,如下面的代码所示,关闭覆盖层
代码:
body: GestureDetector(
onTap: (){
_overlayEntry?.remove();
},
child : Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,