我试图创建一个相机应用程序的颤栗,但我不断遇到变焦问题。我设法使变焦代码通过按钮工作,但当我试图用GestureDetector替换这些按钮时,什么也不会发生。
下面是相机页面。我试图通过添加一个gestureDetector来检查我的debugLog是否在某个地方被调用,但是这没有输出任何信息,所以我认为问题在于调用检测器,而不一定是调用检测器中的代码。
class CameraPreviewPage extends StatefulWidget {
final CameraDescription camera;
CameraPreviewPage({required this.camera});
@override
_CameraPreviewPageState createState() => _CameraPreviewPageState();
}
class _CameraPreviewPageState extends State<CameraPreviewPage> {
late CameraController _cameraController;
late Future<void> _initializeCameraControllerFuture;
double zoom = 1.0;
double _scaleFactor = 1.0;
@override
void initState() {
super.initState();
_cameraController =
CameraController(widget.camera, ResolutionPreset.ultraHigh);
_initializeCameraControllerFuture = _cameraController.initialize();
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
FutureBuilder(
future: _initializeCameraControllerFuture,
builder: (context, snapshot) {
//The gestureDetector doesn't work
if (snapshot.connectionState == ConnectionState.done) {
return
GestureDetector(
behavior: HitTestBehavior.translucent,
onScaleStart: (details) {
zoom = _scaleFactor;
},
onScaleUpdate: (details) {
_scaleFactor = zoom * details.scale;
_cameraController.setZoomLevel(_scaleFactor);
debugPrint('Gesture updated');
},
child:CameraPreview(_cameraController));
} else {
return Center(child: CircularProgressIndicator());
}
},
),
SafeArea(
child: Scaffold(
backgroundColor: Colors.transparent,
floatingActionButton:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
FloatingActionButton(
heroTag: "FABZoomIn",
child: Icon(
Icons.zoom_in,
color: Colors.white,
),
onPressed: () {
if (zoom < 8) {
zoom = zoom + 1;
}
_cameraController.setZoomLevel(zoom);
},
backgroundColor: Colors.cyan,
),
SizedBox(
height: 10,
),
FloatingActionButton(
heroTag: "FABZoomOut",
child: Icon(
Icons.zoom_out,
color: Colors.white,
),
onPressed: () {
if (zoom > 1) {
zoom = zoom - 1;
}
_cameraController.setZoomLevel(zoom);
},
backgroundColor: Colors.red,
),
SizedBox(
height: 10,
),
FloatingActionButton(
heroTag: "FABCamera",
child: Icon(Icons.camera, color: Colors.white),
backgroundColor: Colors.green,
onPressed: () {
_takePicture(context);
})
]),
))
],
);
}
@override
void dispose() {
_cameraController.dispose();
super.dispose();
}
}
编辑:我也尝试使用onTap,看看这是否能让我更深入地了解可能出错的地方,但onTap也没有注册。
发布于 2021-05-31 06:01:01
之所以发生这种情况,是因为首先呈现Stack
的children
中的第一个children
,而在第一个child
之上呈现所有其他的children
。
因此,您的GestureDetector
永远不会触发,因为其他的孩子正在处理水龙头,并阻止GestureDetector
接收到任何点击。
只需将GestureDetector
移到children
列表的末尾即可。
Stack(
children: [
SafeArea(
....
),
GestureDetector(
....
),
],
)
如果您确实需要底层的CameraPreview
和顶部的SafeArea
,那么将CameraPreview
分离到另一个Container
中,并在children
列表的末尾将GestureDetector
作为一个单独的小部件。
Stack(
children: [
Container(
child: CameraPreview(),
),
SafeArea(
....
),
GestureDetector(
....
),
],
)
https://stackoverflow.com/questions/67770235
复制