【发布时间】:2019-06-28 19:59:51
【问题描述】:
我从不久前开始学习颤振。我非常了解编码,但我仍然有一些问题要理解有状态以及将什么放入创建状态。按照教程,我制作了这个应用程序,它加载一个带有地震信息的 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);
}
`
【问题讨论】:
标签: listview mobile dart flutter