Это JSON файл
[
{
"id": 1,
"country": "United States",
"population": 328200000
},
{
"id": 2,
"country": "Germany",
"population": 83020000
},
{
"id": 3,
"country": "United Kingdom",
"population": 66650000
}
]
Я хочу, чтобы после нажатия RaisedButton
он сортировался по порядку («Название страны» => уменьшение; «Население страны» => по алфавиту) ( предварительный просмотр изображения ). Как это сделать?
Это главный файл:
import 'dart:convert';
import 'dart:core';
import 'package:ask/country.dart';
import 'package:ask/country_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Country> _country = [];
@override
void initState() {
CountryServices.getCountry().then((value) {
setState(() {
_country = value;
});
});
super.initState();
}
Widget build(BuildContext context) {
return new MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Country')),
body: Container(
child: Column(children: <Widget>[
Row(children: <Widget>[
Expanded(
flex: 1,
child: RaisedButton(
child: Text('Country Name'),
onPressed: () {}, // Sort alphabetically
)),
Expanded(
flex: 1,
child: RaisedButton(
child: Text('Country Population'),
onPressed: () {}, // Sort in decreasing order
))]),
Container(
child: ListView.builder(
shrinkWrap: true,
itemCount: _country.length,
itemBuilder: (context, index) {
return _listCountry(index);
}))
]))));
}
_listCountry(index) {
Country country = _country[index];
return Row(
children: <Widget>[
Expanded(
flex: 1,
child: Text(
country.country,
textAlign: TextAlign.center,
)),
Expanded(
flex: 1,
child: Text('${country.population}', textAlign: TextAlign.center)),
],
);
}
}