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.
 
 
 
 
 

55 line
1.3 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:location/location.dart';
  4. class GetLocationWidget extends StatefulWidget {
  5. const GetLocationWidget({Key key}) : super(key: key);
  6. @override
  7. _GetLocationState createState() => _GetLocationState();
  8. }
  9. class _GetLocationState extends State<GetLocationWidget> {
  10. final Location location = Location();
  11. LocationData _location;
  12. String _error;
  13. Future<void> _getLocation() async {
  14. setState(() {
  15. _error = null;
  16. });
  17. try {
  18. final LocationData _locationResult = await location.getLocation();
  19. setState(() {
  20. _location = _locationResult;
  21. });
  22. } on PlatformException catch (err) {
  23. setState(() {
  24. _error = err.code;
  25. });
  26. }
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Column(
  31. crossAxisAlignment: CrossAxisAlignment.start,
  32. children: <Widget>[
  33. Text(
  34. 'Location: ' + (_error ?? '${_location ?? "unknown"}'),
  35. style: Theme.of(context).textTheme.body2,
  36. ),
  37. Row(
  38. children: <Widget>[
  39. RaisedButton(
  40. child: const Text('Get'),
  41. onPressed: _getLocation,
  42. )
  43. ],
  44. ),
  45. ],
  46. );
  47. }
  48. }