我在android上构建了一个应用程序,它使用飞镖和颤音与一些Android摄像头功能交互。我对飞镖不是很有经验,我还在学习过程中。
class SwitchWidget extends StatefulWidget {
@override
SwitchWidgetClass createState() => new SwitchWidgetClass();
}每当我打电话给Torch.turnOn(),什么都没有发生,我也不知道为什么。我尝试过很多方法,但仍然一无所获。
我试图调用的函数来自于这个名为torch的套餐。这个包是用来打开和关闭安卓摄像头的手电筒的。
我使用的这个包的主文件有以下代码:
import 'dart:async';
import 'package:flutter/services.dart';
class Torch {
static const MethodChannel _channel = const MethodChannel('io.siteplan.flutterplugins/torch');
static Future turnOn() => _channel.invokeMethod('turnOn');
static Future turnOff() => _channel.invokeMethod('turnOff');
static Future<bool> get hasTorch async => await _channel.invokeMethod('hasTorch');
static Future flash(Duration duration) => turnOn().whenComplete(() => Future.delayed(duration, () => turnOff()));
}我在名为homepage.dart的自定义dart文件中编写了下面的代码。
import 'package:flutter/material.dart';
import 'package:torch/torch.dart';
/**
* HomePage StatefulWidget is here!
*/
class SwitchWidgetClass extends State {
bool switchControl = false;
var textHolder = 'Switch is OFF';
void toggleSwitch(bool value) {
if (switchControl == false) {
setState(() {
switchControl = true;
textHolder = 'Switch is ON';
});
print('Switch is ON');
Torch.turnOn();
} else {
setState(() {
switchControl = false;
textHolder = 'Switch is OFF';
});
print('Switch is OFF');
Torch.turnOff();
}
}
@override
Widget build(BuildContext context) {
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Transform.scale(
scale: 1.5,
child: Switch(
onChanged: toggleSwitch,
value: switchControl,
activeColor: Colors.blue,
activeTrackColor: Colors.green,
inactiveThumbColor: Colors.white,
inactiveTrackColor: Colors.grey,
)),
Text(
'$textHolder',
style: TextStyle(fontSize: 24),
)
]);
}
}我想要的是当我调用Torch.turnOn()方法时,它应该工作并打开相机手电筒。
发布于 2019-12-22 08:26:51
发布于 2019-12-22 11:43:41
尝试将此权限添加到Android中的Manifest文件中
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera.flash" /> https://stackoverflow.com/questions/59442554
复制相似问题