【发布时间】:2020-10-11 14:25:11
【问题描述】:
一旦用户启用 GPS/定位服务,我想运行下面写的 deviceInfo 函数。目前它仅在检查位置服务是否启用时运行。如果它被禁用,那么它会显示警报对话框。
如您所见,我使用的是 If 条件。如果位置被禁用,则显示警报对话框,否则继续下一步。
问题是如果用户启用位置服务,那么我如何执行在 else 条件下编写的代码。
这里是代码。
Future<void> deviceInfo() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print(androidInfo.androidId);
print('Is Physical Device: ${androidInfo.isPhysicalDevice}');
physicaldevice = androidInfo.isPhysicalDevice;
if(physicaldevice == true){
var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
if(isGpsEnabled == false){
setState(() {
visible = false;
});
_showAlert(context);
print(isGpsEnabled);
}else{
------ Some Codes if GPS is enabled ------
}
这是用于 GPS 启用消息的 AlertBox
void _showAlert(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Error"),
content: Text("Location Service is disabled. Please enable it."),
actions: <Widget>[
FlatButton(child: Text('Ok'),
onPressed: () {
final AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
intent.launch();
Navigator.of(context, rootNavigator: true).pop();
})],
)
);
}
简单来说,我想在启用 GPS 后重新运行 DeviceInfo 功能。
【问题讨论】:
标签: flutter