11import 'package:flutter/material.dart' ;
22import 'package:smart_select/smart_select.dart' ;
33import 'package:dio/dio.dart' ;
4+ import 'package:flutter_svg/flutter_svg.dart' ;
45
56class FeaturesOptionAsync extends StatefulWidget {
67 @override
@@ -11,7 +12,11 @@ class _FeaturesOptionAsyncState extends State<FeaturesOptionAsync> {
1112
1213 String _user;
1314 List _users = [];
14- bool _isLoading;
15+ bool _usersIsLoading;
16+
17+ List _country;
18+ List _countries = [];
19+ bool _countriesIsLoading;
1520
1621 @override
1722 Widget build (BuildContext context) {
@@ -23,9 +28,9 @@ class _FeaturesOptionAsyncState extends State<FeaturesOptionAsync> {
2328 value: _user,
2429 option: SmartSelectOptionConfig (
2530 _users,
26- value: (_item ) => _item ['email' ],
27- title: (_item ) => _item ['name' ]['first' ] + ' ' + _item ['name' ]['last' ],
28- subtitle: (_item ) => _item ['email' ],
31+ value: (data ) => data ['email' ],
32+ title: (data ) => data ['name' ]['first' ] + ' ' + data ['name' ]['last' ],
33+ subtitle: (data ) => data ['email' ],
2934 groupBy: 'gender' ,
3035 ),
3136 modal: SmartSelectModalConfig (useFilter: true ),
@@ -40,7 +45,7 @@ class _FeaturesOptionAsyncState extends State<FeaturesOptionAsync> {
4045 title: state.title,
4146 value: state.valueDisplay,
4247 isTwoLine: true ,
43- isLoading: _isLoading ,
48+ isLoading: _usersIsLoading ,
4449 leading: Builder (
4550 builder: (context) {
4651 String avatarUrl = state.valueObject != null
@@ -56,6 +61,30 @@ class _FeaturesOptionAsyncState extends State<FeaturesOptionAsync> {
5661 },
5762 onChange: (val) => setState (() => _user = val),
5863 ),
64+ Divider (indent: 20 ),
65+ SmartSelect (
66+ title: 'Country' ,
67+ value: _country,
68+ isTwoLine: true ,
69+ isMultiChoice: true ,
70+ option: SmartSelectOptionConfig (
71+ _countries,
72+ value: (data) => data['subregion' ] + ' - ' + data['name' ],
73+ title: (data) => data['name' ],
74+ subtitle: ['capital' ],
75+ groupBy: 'region' ,
76+ ),
77+ modal: SmartSelectModalConfig (useFilter: true ),
78+ choice: SmartSelectChoiceConfig (
79+ type: SmartSelectChoiceType .checkboxes,
80+ ),
81+ leading: Container (
82+ width: 40 ,
83+ height: 40 ,
84+ child: Icon (Icons .flag),
85+ ),
86+ onChange: (val) => setState (() => _country = val),
87+ ),
5988 Container (height: 7 ),
6089 ],
6190 );
@@ -66,19 +95,32 @@ class _FeaturesOptionAsyncState extends State<FeaturesOptionAsync> {
6695 super .initState ();
6796
6897 _getUsers ();
98+ _getCountries ();
6999 }
70100
71101 void _getUsers () async {
72102 try {
73- setState (() => _isLoading = true );
103+ setState (() => _usersIsLoading = true );
74104 String url = "https://randomuser.me/api/?inc=gender,name,nat,picture,email&results=25" ;
75105 Response res = await Dio ().get (url);
76- setState (() {
77- _users = res.data['results' ];
78- _isLoading = false ;
79- });
106+ setState (() => _users = res.data['results' ].cast <Map >());
107+ } catch (e) {
108+ print (e);
109+ } finally {
110+ setState (() => _usersIsLoading = false );
111+ }
112+ }
113+
114+ void _getCountries () async {
115+ try {
116+ setState (() => _countriesIsLoading = true );
117+ String url = "http://restcountries.eu/rest/v2/all?fields=name;capital;flag;region;subregion" ;
118+ Response res = await Dio ().get (url);
119+ setState (() => _countries = res.data.cast <Map >());
80120 } catch (e) {
81121 print (e);
122+ } finally {
123+ setState (() => _countriesIsLoading = false );
82124 }
83125 }
84126}
0 commit comments