asia_view.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:fuwei/data/country_entity.dart';
  5. import 'package:fuwei/home/item_country.dart';
  6. import 'package:fuwei/utils/common_util.dart';
  7. import 'package:fuwei/utils/dio_util.dart';
  8. class CountryDataWidget extends StatefulWidget {
  9. CountryDataWidget(this.id, {super.key});
  10. final int id;
  11. @override
  12. State<CountryDataWidget> createState() => _CountryDataWidgetState();
  13. }
  14. class _CountryDataWidgetState extends State<CountryDataWidget>
  15. with AutomaticKeepAliveClientMixin {
  16. List<CountryEntity> list = [];
  17. var margin = 0.0;
  18. @override
  19. void initState() {
  20. super.initState();
  21. if (Util.isWeb()) {
  22. margin = 0.2.sw;
  23. } else {
  24. margin = 0.1.sw;
  25. }
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Padding(
  30. padding: EdgeInsets.only(left: margin, right: margin),
  31. child: FutureBuilder(
  32. future: NetworkUtil().getCountryList(widget.id),
  33. builder: (context, snapshot) {
  34. if (snapshot.connectionState == ConnectionState.done) {
  35. if (snapshot.hasData) {
  36. if (list.isEmpty) {
  37. list.addAll(snapshot.data!);
  38. }
  39. if (Util.isWeb()) {
  40. return GridView(
  41. gridDelegate:
  42. const SliverGridDelegateWithFixedCrossAxisCount(
  43. crossAxisCount: 5,
  44. childAspectRatio: 1,
  45. ),
  46. children: List.generate(list.length, (index) {
  47. return CountryItemWidget(list[index]);
  48. }),
  49. );
  50. } else {
  51. return GridView(
  52. gridDelegate:
  53. const SliverGridDelegateWithFixedCrossAxisCount(
  54. crossAxisCount: 2,
  55. childAspectRatio: 1.6,
  56. ),
  57. children: List.generate(list.length, (index) {
  58. return CountryItemWidget(list[index]);
  59. }),
  60. );
  61. }
  62. } else {
  63. return Text("Error:${snapshot.error}");
  64. }
  65. } else {
  66. return Container(
  67. alignment: Alignment.center,
  68. child: const CircularProgressIndicator(),
  69. );
  70. }
  71. }),
  72. );
  73. }
  74. @override
  75. bool get wantKeepAlive => true;
  76. }