location_provider 1.0.0 copy "location_provider: ^1.0.0" to clipboard
location_provider: ^1.0.0 copied to clipboard

A lightweight Flutter geocoding utility package with address formatting, extensions, caching, and distance calculation support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:location_provider/location_provider.dart';

/// /main function start here
void main() {
  runApp(const MyApp());
}

/// MyApp started with stateless widget
class MyApp extends StatelessWidget {
  /// create the App
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Location Provider Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

/// All methods of location provider updated
class HomePage extends HookWidget {
  /// create homescreen
  const HomePage({super.key});

  Future<void> _reverseGeocode({
    required TextEditingController latController,
    required TextEditingController lngController,
    required ValueNotifier<String> result,
  }) async {
    final lat = double.tryParse(latController.text);
    final lng = double.tryParse(lngController.text);

    if (lat == null || lng == null) {
      result.value = 'Invalid coordinates';
      return;
    }

    try {
      final address = await LocationHelper.getAddress(
        lat,
        lng,
      );

      if (address == null) {
        result.value = 'No address found';
        return;
      }

      result.value = '''
📍 Full Address
${address.fullAddress}

📌 Short Address
${address.shortAddress}

🏙 City
${address.city}

🗺 State
${address.state}

🌍 Country
${address.country}
''';
    } catch (e) {
      result.value = e.toString();
    }
  }

  Future<void> _forwardGeocode({
    required TextEditingController addressController,
    required ValueNotifier<String> result,
  }) async {
    try {
      final location =
      await LocationHelper.getLocationFromAddress(
        addressController.text,
      );

      if (location == null) {
        result.value = 'No coordinates found';
        return;
      }

      result.value = '''
📍 Latitude
${location.lat}

📍 Longitude
${location.lng}

⏱ Timestamp
${location.timestamp}
''';
    } catch (e) {
      result.value = e.toString();
    }
  }

  Future<void> _extensionExample({
    required ValueNotifier<String> result,
  }) async {
    final coords = (
    lat: 28.6139,
    lng: 77.2090,
    );

    final city = await coords.city;
    final state = await coords.state;
    final country = await coords.country;
    final address = await coords.fullAddress;

    result.value = '''
✨ Extension API Example

🏙 City
$city

🗺 State
$state

🌍 Country
$country

📍 Address
$address
''';
  }

  void _distanceExample({
    required ValueNotifier<String> result,
  }) {
    final delhi = (
    lat: 28.6139,
    lng: 77.2090,
    );

    final mumbai = (
    lat: 19.0760,
    lng: 72.8777,
    );

    final distance =
    delhi.distanceBetween(mumbai);

    result.value = '''
📏 Distance Example

Delhi → Mumbai

${distance.toStringAsFixed(2)} km
''';
  }

  void _clearCache(
      ValueNotifier<String> result,
      ) {
    LocationHelper.clearCache();

    result.value = '✅ Cache cleared';
  }

  @override
  Widget build(BuildContext context) {
    final result =
    useState('Result will appear here');

    final latController =
    useTextEditingController(
      text: '28.6139',
    );

    final lngController =
    useTextEditingController(
      text: '77.2090',
    );

    final addressController =
    useTextEditingController(
      text: 'New Delhi',
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Location Provider Demo',
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [

          /// Reverse Geocoding
          const _SectionTitle(
            title: 'Reverse Geocoding',
          ),

          _InputField(
            controller: latController,
            label: 'Latitude',
          ),

          const SizedBox(height: 12),

          _InputField(
            controller: lngController,
            label: 'Longitude',
          ),

          const SizedBox(height: 12),

          _ActionButton(
            title: 'Get Address',
            onTap: () => _reverseGeocode(
              latController: latController,
              lngController: lngController,
              result: result,
            ),
          ),

          const SizedBox(height: 32),

          /// Forward Geocoding
          const _SectionTitle(
            title: 'Forward Geocoding',
          ),

          _InputField(
            controller: addressController,
            label: 'Address',
          ),

          const SizedBox(height: 12),

          _ActionButton(
            title: 'Get Coordinates',
            onTap: () => _forwardGeocode(
              addressController: addressController,
              result: result,
            ),
          ),

          const SizedBox(height: 32),

          /// Extensions
          _ActionButton(
            title: 'Extension API Example',
            onTap: () => _extensionExample(
              result: result,
            ),
          ),

          const SizedBox(height: 12),

          /// Distance
          _ActionButton(
            title: 'Distance Example',
            onTap: () => _distanceExample(
              result: result,
            ),
          ),

          const SizedBox(height: 12),

          /// Cache
          _ActionButton(
            title: 'Clear Cache',
            onTap: () => _clearCache(result),
          ),

          const SizedBox(height: 32),

          Container(
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              border: Border.all(),
              borderRadius:
              BorderRadius.circular(12),
            ),
            child: Text(
              result.value,
            ),
          ),
        ],
      ),
    );
  }
}

class _SectionTitle extends StatelessWidget {
  final String title;

  const _SectionTitle({
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(
        bottom: 12,
      ),
      child: Text(
        title,
        style: const TextStyle(
          fontSize: 18,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}

class _InputField extends StatelessWidget {
  final TextEditingController controller;
  final String label;

  const _InputField({
    required this.controller,
    required this.label,
  });

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: controller,
      decoration: InputDecoration(
        labelText: label,
        border: const OutlineInputBorder(),
      ),
    );
  }
}

class _ActionButton extends StatelessWidget {
  final String title;
  final VoidCallback onTap;

  const _ActionButton({
    required this.title,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onTap,
      child: Text(title),
    );
  }
}
3
likes
160
points
349
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A lightweight Flutter geocoding utility package with address formatting, extensions, caching, and distance calculation support.

Repository (GitHub)
View/report issues

Topics

#geocoding #location #reverse-geocoding #coordinates #address

License

MIT (license)

Dependencies

flutter, geocoding

More

Packages that depend on location_provider