Skip to content

Instantly share code, notes, and snippets.

@fabian71
Created November 19, 2019 23:07
Show Gist options
  • Select an option

  • Save fabian71/b2fdb4579e2d85887ebd1e4c9d8d25fa to your computer and use it in GitHub Desktop.

Select an option

Save fabian71/b2fdb4579e2d85887ebd1e4c9d8d25fa to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
class Register2 extends StatefulWidget {
@override
_RegisterState2 createState() => _RegisterState2();
}
class _RegisterState2 extends State<Register2> {
Estados _current;
//final String uri = 'https://jsonplaceholder.typicode.com/users';
final String uri = 'http://www.deltainternet.net.br/api/v1.0/states';
Future<List<Estados>> _fetchUsers() async {
var response = await http.get(uri);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Estados> listOfUsers = items.map<Estados>((json) {
return Estados.fromJson(json);
}).toList();
return listOfUsers;
} else {
throw Exception('Failed to load internet');
}
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Text(''),
iconTheme: IconThemeData(
color: Colors.purple,
),
),
body: Container(
color: Colors.white24,
child: SingleChildScrollView(
child: Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
FutureBuilder<List<Estados>>(
future: _fetchUsers(),
builder: (BuildContext context,
AsyncSnapshot<List<Estados>> snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator();
return DropdownButton<Estados>(
items: snapshot.data
.map((data) => DropdownMenuItem<Estados>(
child: Text(data.name),
value: data,
))
.toList(),
onChanged: (Estados value) {
setState(() {
_current = value;
});
},
isExpanded: true,
//value: _current,
hint: Text('Selecione o estado'),
);
}),
SizedBox(height: 20.0),
_current != null
? Text("Estado: " + _current.name)
: Text("Nenum estado selecionado"),
],
),
),
)),
);
}
}
class Estados {
int id;
String name;
Estados({
this.id,
this.name,
});
factory Estados.fromJson(Map<String, dynamic> json) {
return Estados(
id: json['id'],
name: json['name'],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment