У меня есть приложение, которое получает данные заказа от API и показывает их в виде списка. Есть 4 статуса (хорошо, ожидание, ожидание и завершение), и я хочу показать любой статус цветом, но я не знаю, как бы я это сделал.
Есть идеи?
Мой код: Создание объектов:
class Hist {
final String codPedido;
final String status;
final String statusDescricao;
final String usuario;
final String placa;
final String box;
final String tipoVeiculo;
final String dtCadastro;
final String hrCadastro;
Hist(
{this.codPedido,
this.status,
this.statusDescricao,
this.usuario,
this.placa,
this.box,
this.tipoVeiculo,
this.dtCadastro,
this.hrCadastro});
factory Hist.fromJson(Map<String, dynamic> json) {
return Hist(
codPedido: json['cod_pedido'],
status: json['status'],
statusDescricao: json['status_descricao'],
usuario: json['usuario'],
placa: json['placa'],
box: json['box'],
tipoVeiculo: json['tipo_veiculo'],
dtCadastro: json['dt_cadastro'],
hrCadastro: json['hr_cadastro']);
}
}
Строитель:
Widget build(BuildContext context) {
return FutureBuilder<List<Hist>>(
future: _fetchHist(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Hist> data = snapshot.data;
return _histListView(data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return Center(
child: Container(
height: 100,
width: 100,
margin: EdgeInsets.all(5),
child: CircularProgressIndicator(
strokeWidth: 2.0,
valueColor: AlwaysStoppedAnimation(Colors.green),
),
),
);
},
);
}
Часть выборки:
Future<List<Hist>> _fetchHist(BuildContext context) async {
//Classe para carregar os pedidos da api
//
final prefs = await SharedPreferences.getInstance(); //pega o usuário logado
final key = 'usuario';
final value = prefs.getString(key);
print('saved tester $value');
String usu = value; //fim
//Carrega os itens da api
Response response = await Dio().post(
server,
data: {"usuario": usu});
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.data);
print(response.data);
return jsonResponse.map((job) => new Hist.fromJson(job)).toList();
} else {
throw Exception('Falha ao carregar histórico');
}
}
список и части листов:
ListView _histListView(data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return _tile(
data[index].codPedido + " - " + data[index].hrCadastro,
"Status: " +
data[index].statusDescricao +
"\nBox: " +
data[index].box +
" " +
data[index].tipoVeiculo +
" " +
data[index].placa,
Icons.settings,
context,
index);
});
}
ListTile _tile(String title, String subtitle, IconData icon,
BuildContext context, int counter) =>
ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: Text(subtitle),
leading: Icon(
icon,
color: Colors.green[500],
),
onTap: () {
_fetchPed(title);
for (int i = 0; i < dataHolder.length; i++) {
pedList.add(Ped.fromJson(dataHolder[0]));
}