Skip to content

Instantly share code, notes, and snippets.

@aqibgatoo
Last active April 4, 2019 10:21
Show Gist options
  • Save aqibgatoo/dd7ed15e778e657db512245c8199ec3d to your computer and use it in GitHub Desktop.
Save aqibgatoo/dd7ed15e778e657db512245c8199ec3d to your computer and use it in GitHub Desktop.
child: DropdownButtonHideUnderline(
child: StreamBuilder<List<Country>>(
initialData: [],
stream: bloc.countries,
builder: (context, countriesSnapshot) {
return StreamBuilder<Country>(
stream: bloc.country,
builder: (context, snapshot) {
return countriesSnapshot.hasData ? DropdownButton<Country>(
onChanged: bloc.setCountry,
hint: Text("Country"),
value: snapshot.data ,
items: countriesSnapshot.data
.toList()
.map<
DropdownMenuItem<
Country>>((country) =>
DropdownMenuItem<Country>(
value: country,
child: Text(country.name),
))
.toList(),
): CircularProgressIndicator();
});
}),
),
),
)
: Container(),
snapshot.data == 1
? Container(
margin: EdgeInsets.only(
bottom: screenAwareSize(10.0, context)),
child: InputDecorator(
decoration: InputDecoration(
filled: true,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor)),
),
child: DropdownButtonHideUnderline(
child: StreamBuilder<List<Region>>(
initialData: [],
stream: bloc.regions,
builder: (context, regionSnapshot) {
print("${regionSnapshot.data}");
return StreamBuilder<Region>(
stream: bloc.region,
builder: (context, snapshot) {
print("${snapshot.data}");
return regionSnapshot.hasData ? DropdownButton<Region>(
value: snapshot.data,
onChanged: bloc.setRegion,
hint: Text("State / Region"),
items: regionSnapshot.data
.map<DropdownMenuItem<Region>>(
(state) => DropdownMenuItem<
Region>(
value: state,
child:
Text(state?.name),
))
.toList(),
): Container();
});
}),
),
),
)
: Container(),
snapshot.data == 1
? Container(
margin: EdgeInsets.only(
bottom: screenAwareSize(10.0, context)),
child: InputDecorator(
decoration: InputDecoration(
filled: true,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor)),
),
child: DropdownButtonHideUnderline(
child: StreamBuilder<List<City>>(
initialData: [],
stream: bloc.cities,
builder: (context, citiesSnapshot) {
return StreamBuilder<City>(
stream: bloc.city,
builder: (context, snapshot) {
return DropdownButton<City>(
value: snapshot.data,
hint: Text("City"),
onChanged: bloc.setCity,
items: citiesSnapshot.data
.map<DropdownMenuItem<City>>(
(city) =>
DropdownMenuItem<City>(
value: city,
child:
Text(city.name),
))
.toList(),
);
});
}),
),
),
)
: Container(),
//bloc
class NationalityBloc implements BaseBloc {
final _countrySubject = BehaviorSubject<Country>();
final _regionSubject = BehaviorSubject<Region>();
final _citySubject = BehaviorSubject<City>();
final _regionsSubject = BehaviorSubject<List<Region>>();
final _citiesSubject = BehaviorSubject<List<City>>();
Observable<Country> get country => _countrySubject.stream;
Observable<Region> get region => _regionSubject.stream;
Observable<City> get city => _citySubject.stream;
Observable<List<Country>> countries;
Observable<List<Region>> get regions => _regionsSubject.stream;
Observable<List<City>> get cities => _citiesSubject.stream;
Function(Country) get setCountry => _countrySubject.sink.add;
Function(Region) get setRegion => _regionSubject.sink.add;
Function(City) get setCity => _citySubject.sink.add;
NationalityBloc() {
countries = Observable.fromFuture(api.getCountries());
_countrySubject.listen((country) {
_regionsSubject.sink.addStream(Observable<List<Region>>.fromFuture(
api.getStates(country.code)));
});
_regionSubject.listen((region) {
_citiesSubject.sink.addStream(Observable<List<City>>.fromFuture(
api.getCities(region.country, region.name)));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment