asia_view.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:hengyi/data/country_entity.dart';
  5. import 'package:hengyi/home/item_country.dart';
  6. import 'package:hengyi/utils/dio_util.dart';
  7. class CountryDataWidget extends StatefulWidget {
  8. CountryDataWidget(this.id, {super.key});
  9. final int id;
  10. @override
  11. State<CountryDataWidget> createState() => _CountryDataWidgetState();
  12. }
  13. class _CountryDataWidgetState extends State<CountryDataWidget> {
  14. List<CountryEntity> list = [];
  15. @override
  16. void initState() {
  17. super.initState();
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Padding(
  22. padding: EdgeInsets.only(left: 200.w, right: 200.w),
  23. child: FutureBuilder(
  24. future: NetworkUtil().getCountryList(widget.id),
  25. builder: (context, snapshot) {
  26. if (snapshot.connectionState == ConnectionState.done) {
  27. if (snapshot.hasData) {
  28. list.addAll(snapshot.data!);
  29. return GridView(
  30. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  31. crossAxisCount: 4,
  32. childAspectRatio: 1,
  33. ),
  34. children: List.generate(list.length, (index) {
  35. return CountryItemWidget(list[index]);
  36. }),
  37. );
  38. } else {
  39. return Text("Error:${snapshot.error}");
  40. }
  41. } else {
  42. return Container(
  43. alignment: Alignment.center,
  44. child: const CircularProgressIndicator(),
  45. );
  46. }
  47. }),
  48. );
  49. }
  50. }