我有这段代码,其中包含父小部件Homepage和子小部件CountryList。在 中CountryList,我创建了一个函数,该函数使用 API 来获取国家/地区列表。我想RefreshIndicator在应用程序中启用一个,所以我不得不修改Homepage小部件并添加小部件的GlobalKey访问getCountryData()功能CountryList。在RefreshIndicator已经完成了它的工作做好。但现在的问题是,当我RefreshIndicator在应用程序中拉取和使用时,该getCountryData()函数被调用,但即使在列表中显示所有数据后,圆形微调器也不会运行(如屏幕截图所示)。
那么,有人可以建议我一种让旋转器运行的方法吗?main.dart包含Homepage小部件的代码如下:
import 'package:flutter/material.dart';
import 'country_list.dart';
GlobalKey<dynamic> globalKey = GlobalKey();
void main() => runApp(MaterialApp(home: Homepage()));
class Homepage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("List of countries"), actions: <Widget>[
IconButton(icon: Icon(Icons.favorite), onPressed: (){},)
],),
body: RefreshIndicator(child: CountryList(key:globalKey), onRefresh: (){globalKey.currentState.getCountryData();},),
);
}
}
Run Code Online (Sandbox Code Playgroud)
country_list.dart包含CountryList小部件的代码是:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_svg/flutter_svg.dart';
class CountryList extends StatefulWidget {
CountryList({Key key}) : super(key: key);
@override
_CountryListState createState() => _CountryListState();
}
class _CountryListState extends State<CountryList> {
List<dynamic> _countryData;
bool _loading = false;
@override
void initState() {
// TODO: implement initState
super.initState();
this.getCountryData();
}
Future<String> getCountryData() async {
setState(() {
_loading = true;
});
var response =
await http.get(Uri.encodeFull("https://restcountries.eu/rest/v2/all"));
var decodedResponse = json.decode(response.body);
setState(() {
_countryData = decodedResponse;
_loading = false;
});
}
@override
Widget build(BuildContext context) {
return _loading?Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[CircularProgressIndicator(), Padding(padding: EdgeInsets.all(5.0),), Text("Loading data...", style: TextStyle(fontSize: 20.0),)],)):ListView.builder(
itemCount: _countryData.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: ListTile(
leading: SvgPicture.network(_countryData[index]['flag'], width: 60.0,),
title: Text(_countryData[index]['name']),
trailing: IconButton(
icon: Icon(Icons.favorite_border),
onPressed: () {},
),
),
);
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
Tay*_*aym 10
您需要在return此处添加:
Future<String> getCountryData() async {
setState(() {
_loading = true;
});
var response =
await http.get(Uri.encodeFull("https://restcountries.eu/rest/v2/all"));
var decodedResponse = json.decode(response.body);
setState(() {
_countryData = decodedResponse;
_loading = false;
});
return 'success';
}
Run Code Online (Sandbox Code Playgroud)
和这里:
body: RefreshIndicator(
child: CountryList(key: globalKey),
onRefresh: () {
return globalKey.currentState.getCountryData();
},
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6723 次 |
| 最近记录: |