gpt4 book ai didi

dart - 如何使 RefreshIndicator 消失?

转载 作者:IT王子 更新时间:2023-10-29 06:57:41 28 4
gpt4 key购买 nike

我的这段代码包含父小部件 Homepage 和子小部件 CountryList。在 CountryList 中,我创建了一个使用 API 获取国家列表的函数。我想在应用程序中启用 RefreshIndicator,所以我不得不修改 Homepage 小部件并添加 GlobalKey 以访问 getCountryData() CountryList 小部件的功能。 RefreshIndicator 已经很好地完成了它的工作。但现在的问题是,当我在应用程序中拉取并使用 RefreshIndicator 时,会调用 getCountryData() 函数,但即使在显示列表中的所有数据之后,圆形微调器不运行(如屏幕截图所示)。 App Screenshot

那么,有人可以向我建议一种让微调器运转的方法吗?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();},),
);
}
}

包含 CountryList 小部件的 country_list.dart 代码是:

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: () {},
),
),
);
},
);
}
}

最佳答案

您需要在此处添加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';
}

这里:

body: RefreshIndicator(
child: CountryList(key: globalKey),
onRefresh: () {
return globalKey.currentState.getCountryData();
},
),

关于dart - 如何使 RefreshIndicator 消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355691/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com