Flutter Geolocator-询问是否被拒绝,设备位置,滚动刷新

时间:2019-12-07 11:16:13

标签: flutter

这是我的LocationWidget的代码:

import 'package:flutter/material.dart';
import 'package:android_intent/android_intent.dart';
import 'package:geolocator/geolocator.dart';
import 'package:location/location.dart' as prefix0;
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CurrentLocationWidget extends StatefulWidget {

  final GlobalKey<ScaffoldState> scaffoldKey;
  CurrentLocationWidget({Key key, this.scaffoldKey}) : super(key: key);

  @override
  _LocationState createState() => _LocationState();
}

class _LocationState extends State<CurrentLocationWidget> {

  final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

  Position _currentPosition;
  String _currentAddress = '';

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  @override
  void initState() {
    _getCurrentLocation();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<GeolocationStatus>(
        future: geolocator.checkGeolocationPermissionStatus(),
        builder: (BuildContext context, AsyncSnapshot<GeolocationStatus> snapshot) {
          if (snapshot.data == GeolocationStatus.disabled || snapshot.data == null)
            {
              return Align(alignment: Alignment.centerLeft, child: Icon(Icons.location_off),);
            }
          else if (snapshot.data == GeolocationStatus.denied)
          {
            return Align(alignment: Alignment.centerLeft, child: Icon(Icons.location_off),);
          }
          else{
            return Center(
              child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Icon(Icons.location_on),
                    Text(_currentAddress)
                  ]
              ),
            );
          }
        });
  }

  _getCurrentLocation() async{
    geolocator
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
      });
      _getAddressFromLatLng();
    }).catchError((e) {
      print(e);
    });
  }

  _getAddressFromLatLng() async {
    try {
      //Logging current location to shared preferences
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      sharedPreferences.setDouble("latitude", _currentPosition.latitude);
      sharedPreferences.setDouble("longitude", _currentPosition.longitude);


      List<Placemark> p = await geolocator.placemarkFromCoordinates(
          _currentPosition.latitude, _currentPosition.longitude);

      Placemark place = p[0];

      setState(() {
        _currentAddress =
        "${place.thoroughfare}";
      });

      // Update sign in and sign up locations
      if(sharedPreferences.getBool("SignUp")) {
        await updateUserSignUpLocation(context, _currentPosition.latitude, _currentPosition.longitude);
      }
      if(sharedPreferences.getBool("SignIn")) {
        await updateUserSignInLocation(context, _currentPosition.latitude, _currentPosition.longitude);
      }
    } catch (e) {
      print(e);
    }
  }

  Future<Preferences> updateUserSignUpLocation(BuildContext context, double latitude, double longitude) async {
    Api _api = Provider.of(context);
    UserTokenModel useTokenModelValue = await _api.getAccessToken();
    return await _api.updateUserSignUpData(useTokenModelValue.accessToken, latitude, longitude);
  }

  Future<Preferences> updateUserSignInLocation(BuildContext context, double latitude, double longitude) async {
    Api _api = Provider.of(context);
    UserTokenModel useTokenModelValue = await _api.getAccessToken();
    return await _api.updateUserSignInData(useTokenModelValue.accessToken, latitude, longitude);
  }

}

我该如何解决以下问题:

  1. 如果客户具有“ DENIED”许可,则使用基于位置的功能,然后我要再次请求许可。

  2. 如果权限为“允许”,我要检查设备位置是否为ON->如果为OFF,则要打开设备设置。

  3. 当前在屏幕上滚动,如果权限被拒绝,则在安装了位置相关的窗口小部件时滚动屏幕时,它将再次请求该权限。我该如何阻止?

0 个答案:

没有答案