address_search_field 1.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

Address Search Field #

A text field that displays an address search box to find a location by typing a reference and gets nearby addresses. Selecting the desired address returns an object with the latitude, longitude, and full address of the place. It uses location, geolocator, geocoder plugins.

NOTE: This package was made with Flutter 1.17 and Dart 2.8, make sure you have your environment within the version range.

environment:
  sdk: ">=2.7.0 <3.0.0"
  flutter: ">=1.10.0"

Getting Started #

To use this plugin, add address_search_field as a dependency in your pubspec.yaml file. For example:

dependencies:
  address_search_field: ^1.4.1

Permissions #

Android #

On Android you'll need to add either the ACCESS_COARSE_LOCATION or the ACCESS_FINE_LOCATION permission to your Android Manifest file (located under android/app/src/main). To do so add one of the following two lines as direct children of the <manifest> tag:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Also you have to add internet permission

<uses-permission android:name="android.permission.INTERNET"/>

iOS #

On iOS you'll need to add the NSLocationWhenInUseUsageDescription to your Info.plist file (located under ios/Runner) in order to access the device's location. Simply open your Info.plist file and add the following:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Permission to get your location</string>

Usage #

Import the package:

import 'package:address_search_field/address_search_field.dart';
  • This widget is a search box to write a reference about a place and find an address.
AddressSearchBox(
  controller: TextEditingController(),
  country: String,
  city: String,
  hintText: String,
  noResultText: String,
  exceptions: <String>[],
  coordForRef: bool,
  onDone: (AddressPoint point) {},
  onCleaned: () {},
);
ParametersTypeDescription
controllerOptionalThe controller allows you to interact with the text in the field.
countryRequired, Not NullYou have to set in which country to search.
cityOptionalYou may set in which city to search.
hintTextRequired, Not NullText suggestion according to your language in the search box.
noResultsTextRequired, Not NullMessage according to your language when the search box doesn't find anything.
exceptionsOptionalResults you don't want to show.
coordForRefOptionalIt's false by default. With the value true, it will use what the user typed in the search box as a valid Address.
onDoneOptionalWhen the search stops, it gives you an AddressPoint object with confirmation if a place has been found, full address, and coordinates.
onCleanedOptionalWhen the search box is closed with an empty text field, this function is executed.
  • This widget is a TextField that displays aAddressSearchBox by tapping it. It has 3 more parameters to personalize the TextField.
AddressSearchField(
  controller: TextEditingController(),
  decoration: InputDecoration(),
  style: TextStyle(),
  barrierDismissible: bool,
  country: String,
  city: String,
  hintText: String,
  noResultsText: String,
  exceptions: <String>[],
  coordForRef: bool,
  onDone: (AddressPoint point) {},
  onCleaned: () {},
);
ParametersTypeDescription
decorationOptionalThe decoration to show around the text field.
styleOptionalThe style to use for the text being edited.
barrierDismissibleOptionalIt's true by default. With the value false, it won't close the search box when you tap outside it.
  • When the address search is complete, you can get an AddressPoint object that provides the following values:
onDone: (AddressPoint point) {
  bool found = point.found;
  String address = point.address;
  String country = point.country;
  double latitude = point.latitude;
  double longitude = point.longitude;
}

There are three possible outcomes:

  1. If no search has been performed then the object will have null values ​​in its address, latitude and longitude variables.
  2. If a place has been found from the reference, all the variables will be initialized.
  3. If the desired location has not been found and the reference entered by the user is selected, there will be no latitude and longitude values ​​and the address will be the user's reference since nothing was found.

NOTE: You can also find an address by it's coordinates using await AddressPoint.fromPoint(latitude, longitude). If the address is not found, it will return null.

() async {
  AddressPoint point = await AddressPoint.fromPoint(latitude, longitude);
  if (point != null) print(point.address);
}
  • This plugin also has an async static function called initLocationService to verify and request location permissions. You can use optional callbacks when location service is not enabled or if permission is not granted.
() async => await initLocationService(
  noServiceEnabled: () {},
  noPermissionGranted: () {},
);

License #

MIT License

1.4.1 #

  • Fix possible bug from useless conditional

1.4.0 #

  • Remake of AddressSearchTextField Plugin due to the update of Flutter 1.17 and Dart 2.8
  • LocationService class is now an async static function called initLocationService
  • Updated the README.md for a better explanation of plugin.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:address_search_field/address_search_field.dart';
import 'package:toast/toast.dart'; // External library

void main() {
  runApp(MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
      visualDensity: VisualDensity.adaptivePlatformDensity,
    ),
    home: PageOne(),
  ));
}

class PageOne extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageOneState();
}

class _PageOneState extends State<PageOne> {
  TextEditingController controller = TextEditingController();
  String text = "";

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Container(
          width: size.width * 0.80,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              AddressSearchField(
                controller: controller,
                country: "Ecuador",
                city: "Esmeraldas",
                hintText: "Dirección",
                noResultsText: "No hay resultados...",
                exceptions: [
                  "Esmeraldas, Ecuador",
                  "Esmeraldas Province, Ecuador",
                  "Ecuador"
                ],
                onDone: (AddressPoint point) async {
                  AddressPoint point2;
                  if (point.latitude != null)
                    point2 = await AddressPoint.fromPoint(
                      latitude: point.latitude,
                      longitude: point.longitude,
                    );
                  setState(() {
                    text = "${point.toString()}\n\n${point2.toString()}";
                  });
                  Navigator.of(context).pop();
                },
                onCleaned: () => print("clean"),
              ),
              Container(
                margin: EdgeInsets.symmetric(vertical: 30.0),
                child: Text(text),
              ),
              FlatButton(
                onPressed: () => Navigator.push(context,
                    MaterialPageRoute(builder: (context) => PageTwo())),
                child: Text("Page Two"),
                color: Colors.blue,
              )
            ],
          ),
        ),
      ),
    );
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: AddressSearchBox(
        country: "Ecuador",
        city: "Esmeraldas",
        hintText: "Dirección",
        noResultsText: "No hay resultados...",
        exceptions: [
          "Esmeraldas, Ecuador",
          "Esmeraldas Province, Ecuador",
          "Ecuador"
        ],
        onDone: (AddressPoint point) {
          // I use toast dependency to prettier show the result
          Toast.show(
            point.toString(),
            context,
            duration: Toast.LENGTH_LONG,
            gravity: Toast.BOTTOM,
          );
        },
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  address_search_field: ^1.4.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:address_search_field/address_search_field.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform ios

Because of import path [package:address_search_field/address_search_field.dart, package:geolocator/geolocator.dart, package:google_api_availability/google_api_availability.dart] that declares support for platforms: android

Package does not support Flutter platform linux

Because of import path [package:address_search_field/address_search_field.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:address_search_field/address_search_field.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:address_search_field/address_search_field.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:address_search_field/address_search_field.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [address_search_field] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
geocoder ^0.2.1 0.2.1
geolocator ^5.3.1 5.3.2+2
location ^3.0.2 3.0.2
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
equatable 1.2.0
flutter_web_plugins 0.0.0
google_api_availability 2.0.4
http_parser 3.1.4
js 0.6.2
location_permissions 3.0.0+1
location_platform_interface 1.0.0
location_web 1.0.0
meta 1.1.8
path 1.7.0
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test