flutter app untuk unitstock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

61 line
1.6 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:location/location.dart';
  3. class ServiceEnabledWidget extends StatefulWidget {
  4. const ServiceEnabledWidget({Key key}) : super(key: key);
  5. @override
  6. _ServiceEnabledState createState() => _ServiceEnabledState();
  7. }
  8. class _ServiceEnabledState extends State<ServiceEnabledWidget> {
  9. final Location location = Location();
  10. bool _serviceEnabled;
  11. Future<void> _checkService() async {
  12. final bool serviceEnabledResult = await location.serviceEnabled();
  13. setState(() {
  14. _serviceEnabled = serviceEnabledResult;
  15. });
  16. }
  17. Future<void> _requestService() async {
  18. if (_serviceEnabled == null || !_serviceEnabled) {
  19. final bool serviceRequestedResult = await location.requestService();
  20. setState(() {
  21. _serviceEnabled = serviceRequestedResult;
  22. });
  23. if (!serviceRequestedResult) {
  24. return;
  25. }
  26. }
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Column(
  31. crossAxisAlignment: CrossAxisAlignment.start,
  32. children: <Widget>[
  33. Text('Service enabled: ${_serviceEnabled ?? "unknown"}',
  34. style: Theme.of(context).textTheme.body2),
  35. Row(
  36. children: <Widget>[
  37. Container(
  38. margin: const EdgeInsets.only(right: 42),
  39. child: RaisedButton(
  40. child: const Text('Check'),
  41. onPressed: _checkService,
  42. ),
  43. ),
  44. RaisedButton(
  45. child: const Text('Request'),
  46. onPressed: _serviceEnabled == true ? null : _requestService,
  47. )
  48. ],
  49. )
  50. ],
  51. );
  52. }
  53. }