【问题标题】:Native Dialog for location setting on FlutterFlutter 上用于位置设置的本机对话框
【发布时间】:2020-06-13 17:37:48
【问题描述】:

有没有办法实现如下图所示的“位置对话框”设置,当应用需要 GPS 位置但找不到时会触发该对话框。点击 OK 将立即打开系统 GPS。这对用户来说似乎更方便,而不是把他们带到位置并手动打开。 是否可以在 Flutter 中实现这样的事情?

对话框的展开视图:

【问题讨论】:

  • 你看过广告pub.dev/packages/permission_handler 吗?
  • 不确定这对我有什么帮助。我已经拥有位置权限。当应用程序查找 GPS 位置但发现 GPS 已关闭,因此它会请求用户打开 GPS 时,就会出现这种情况。

标签: flutter


【解决方案1】:

感谢 Rajesh,回答 hereplugin 允许您添加此本机对话框以进行快速位置设置。

实现很简单:

import 'package:location/location.dart';

var location = Location();

Future _checkGps() async {
if(!await location.serviceEnabled()){
   location.requestService();
  }
}

@override
void initState() {
  super.initState();
  _checkGps();
}

【讨论】:

    【解决方案2】:

    在 Kotlin 中,试试这个代码:

    class HomeActivity : AppCompatActivity() {
        private val requestLocation = 199
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            enableLoc()
        }
        private fun enableLoc() {
            val mLocationRequest = LocationRequest.create()
            mLocationRequest.interval = 10000
            mLocationRequest.fastestInterval = 5000
          //  mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    
            val builder = LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest)
    
            val client = LocationServices.getSettingsClient(this)
            val task =
                client.checkLocationSettings(builder.build())
    
            task.addOnSuccessListener(this) {
                // All location settings are satisfied. The client can initialize
                // location requests here.
                // ...
            }
    
            task.addOnFailureListener(this) { e ->
                if (e is ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        e.startResolutionForResult(
                            this,
                            requestLocation
                        )
                    } catch (sendEx: SendIntentException) {
                        // Ignore the error.
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 1970-01-01
      • 2011-10-22
      • 2019-11-29
      • 2017-10-22
      • 2023-01-12
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      相关资源
      最近更新 更多