Я начал изучать флаттер с недавних пор.Я довольно хорошо понимаю кодирование, но у меня все еще есть некоторая проблема, чтобы понять Stateful и что поместить в состояние создания.После урока я сделал это приложение, которое загружает JSON с информацией о землетрясении и отображает в ListView с полосой прокрутки на стороне.Теперь я хотел бы сделать виджет с сохранением состояния и onrefresh для обновления значений из json.Вот мой код (без сохранения состояния) `
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
Map _quakes;
List _features; // oggesto list delle features
void main() async {
_quakes = await getQuakes();
_features = _quakes["features"];
runApp(new MaterialApp(
theme: new ThemeData(
accentColor: Colors.red,
),
debugShowCheckedModeBanner: false,
color: Colors.red,
title: "Terremoti",
home: new Quakes(),
));
}
class Quakes extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Tutti i Terremoti ultime 24h"),
centerTitle: true,
backgroundColor: Colors.red,
),
body: new Center(
child: new Scrollbar(
child: RefreshIndicator(
onRefresh: getQuakes,
child: ListView.builder(
itemCount: _features.length,
padding: const EdgeInsets.all(15.0),
itemBuilder: (BuildContext context, position) {
if (position.isOdd)
return new SizedBox(
height: 10.0,
child: new Center(
child: new Container(
margin: new EdgeInsetsDirectional.only(
start: 1.0, end: 1.0),
height: 2.5,
color: Colors.red,
),
),
);
final index = position ~/ 2;
var format = new DateFormat("dd MMM, yyyy, \n" + "HH:mm:ss");
//var dateString = format.format(format);
var date = format.format(
new DateTime.fromMillisecondsSinceEpoch(
_features[index]["properties"]["time"],
isUtc: true));
//creando le righe della listview
return new ListTile(
title: new Text(
"Magnitudo: ${_features[index]["properties"]["mag"]} \n $date",
style: new TextStyle(
fontSize: 21.0,
color: Colors.green,
fontWeight: FontWeight.w700),
),
subtitle: new Text(
"Luogo: ${_features[index]["properties"]["place"]}",
style: new TextStyle(
fontSize: 18.0,
),
),
);
}),
),
)),
);
}
}
Future<Map> getQuakes() async {
String apiUrl =
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson";
http.Response response = await http.get(apiUrl);
return json.decode(response.body);
}
`