Android
Android Apps
development
flutter
flutter dart
flutter dependent drop down field
flutter development
flutter multilevel dependent drop down
iOS
ios apps
Create a dependent drop down field in Flutter
Icons made by Pixel perfect from www.flaticon.com
1. Start Visual Studio Code. Go to menu “View -> Command Palette” and select the “Flutter: New Project” option:
2. Create a folder called widgets inside the lib folder and
create a drop_down.dart file. This is where we are going to work:
5. So far so good right? It might look like a lot of
files/steps but this will be a structure of our app. Open the main_app.dart and
add a stateless widget that will return our main widget app:
6. Open the drop_down.dart and create our Main App widget.
We will create a form in our widget. Copy the following code, and as you can
see we are returning a SafeArea with a Form in the body:
7. Things are going well. You can even start the app right
now:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class DropDownApp extends StatefulWidget { | |
@override | |
createState() => _DropDownAppState(); | |
} | |
class _DropDownAppState extends State { | |
@override | |
Widget build(BuildContext context) { | |
// TODO: implement build | |
return SafeArea( | |
child: Scaffold( | |
appBar: AppBar( | |
title: Text('Dependable Dropdown Field'), | |
), | |
body: Container( | |
child: new Form( | |
), | |
), | |
), | |
); | |
} | |
} |
8. Lets add an Padding class, and inside it, create a child
with a new Container specifying the width, and another column that will have
our widget as children:
9. For purposes of this example, lets add a text field
inside the widget:
10. Now, before adding our dropdown fields, we will create
our models, that we will need to populate our dependable dropdown fields. I
will use in this example the Provinces and States of my home country, so that
each time you select a State in the first dropdown field, the second one will
only show the Provinces that belongs only to the State selected. Let’s create a
Models folder, and inside it a localization_model.dart file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
child: Padding( | |
padding: const EdgeInsets.only(top: 16.0, left: 28), | |
child: new Container( | |
width: 350, | |
child: Column( | |
children: [ | |
], | |
), | |
), | |
), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
new TextFormField( | |
keyboardType: TextInputType.emailAddress, | |
decoration: const InputDecoration( | |
icon: const Icon(Icons.email), | |
hintText: 'Type your name', | |
labelText: 'Name', | |
), | |
), | |
], |
Open that file, and create three classes:
- Localization (Main)
- States
- Provinces
Let’s build our Provinces Class first, like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Provinces { | |
final int id; | |
final String name; | |
final int stateId; | |
Provinces({this.id, this.name, this.stateId}); | |
factory Provinces.fromJson(Mapdynamic> parsedJson) { | |
return Provinces(id: parsedJson['id'], name: parsedJson['name'], stateId: parsedJson['state_id']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class States { | |
final int id; | |
final String name; | |
States({this.id, this.name}); | |
factory States.fromJson(Mapdynamic> parsedJson){ | |
return States(id: parsedJson['id'], name: parsedJson['name']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Localization { | |
final List provinces; | |
final List states; | |
Localization({this.provinces, this.states}); | |
factory Localization.fromJson(Mapdynamic> json) { | |
return Localization( | |
states: parseStates(json), | |
provinces: parseProvinces(json), | |
); | |
} | |
static List parseStates(statesJson) { | |
var slist = statesJson['states'] as List; | |
List statesList = | |
slist.map((data) => States.fromJson(data)).toList(); | |
return statesList; | |
} | |
static List parseProvinces(provincesJson) { | |
var plist = provincesJson['provinces'] as List; | |
List provincesList = | |
plist.map((data) => Provinces.fromJson(data)).toList(); | |
return provincesList; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assets: | |
- json/states.json |
Since you are in the pubspec, add the HTTP library as dependency:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http: ^0.12.0+2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:http/http.dart' as http; | |
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:flutter/services.dart'; | |
import 'package:drop_example/Models/localization_model.dart'; |
14. Create two variables: statesList and provincesList as
List variables, and also a tempList where the filtered provinces will be saved
temporarily:
Tip: Add the _state and _province variables to save the
selected options for each dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _DropDownAppState extends State { | |
List statesList = List(); | |
List provincesList = List(); | |
List tempList = List(); | |
String _state; | |
String _province; |
15. Create a Future function that will be called when we
need to get the contents of the JSON file:
16. Create another Future function that will use the
previous function to get the contents of the JSON file, decode it, and make it
an iterable list:
17. Override the initState method to initialize the
_populateDropdown function:
18. Add a new dropdown field, where we will map the states
that we got from the JSON and add each item to the dropdown list:
I am already adding the provinces filter in the onChanged
call back, so that each time the user changes state, the correct provinces will
be added to the other dropdown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future loadStatesProvincesFromFile() async { | |
return await rootBundle.loadString('json/states.json'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future _populateDropdown() async { | |
String getPlaces = await loadStatesProvincesFromFile(); | |
final jsonResponse = json.decode(getPlaces); | |
Localization places = new Localization.fromJson(jsonResponse); | |
setState(() { | |
statesList = places.states; | |
provincesList = places.provinces; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void initState() { | |
super.initState(); | |
this._populateDropdown(); | |
} |
20. Now, add the Provinces drop down field, using the tempList to
build its items:
And that’s it! You just created a dependable drop down field with Flutter!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new DropdownButton( | |
isExpanded: true, | |
icon: const Icon(Icons.gps_fixed), | |
items: tempList.map((item) { | |
return new DropdownMenuItem( | |
child: new Text(item.name), | |
value: item.id.toString(), | |
); | |
}).toList(), | |
onChanged: (newVal) { | |
setState(() { | |
_province = newVal; | |
}); | |
}, | |
value: _province, | |
hint: Text('Select a province'), | |
), |
Any questions or suggestions, just drop a comment!
You can get the code here: Dependable Drop Down Field with Flutter
Update:
By the way, the code has been updated. The DropDownFormField cannot be used for this specific purpose.
1 comment
How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter
Post a Comment