country_list.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:fuwei/data/continent_entity.dart';
  4. import 'package:fuwei/home/asia_view.dart';
  5. import 'package:fuwei/utils/dio_util.dart';
  6. import '../utils/common_util.dart';
  7. class CountryListWidget extends StatefulWidget {
  8. const CountryListWidget({super.key});
  9. @override
  10. State<CountryListWidget> createState() => _CountryListWidgetState();
  11. }
  12. class _CountryListWidgetState extends State<CountryListWidget>
  13. with SingleTickerProviderStateMixin {
  14. TabController? _tabController;
  15. List<ContinentEntity> tabs = [];
  16. var tabBarTextSize = 0.0;
  17. var topMargin = 0.0;
  18. @override
  19. void initState() {
  20. super.initState();
  21. if (Util.isWeb()) {
  22. tabBarTextSize = 30.sp;
  23. topMargin = 60.h;
  24. } else {
  25. tabBarTextSize = 50.sp;
  26. topMargin = 20.h;
  27. }
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Scaffold(
  32. body: FutureBuilder(
  33. future: NetworkUtil().getContinentList(),
  34. builder: (context, snapshot) {
  35. if (snapshot.connectionState == ConnectionState.done) {
  36. if (snapshot.hasData) {
  37. if (tabs.isEmpty) {
  38. tabs.addAll(snapshot.data!);
  39. _tabController ??=
  40. TabController(length: tabs.length, vsync: this);
  41. }
  42. return Column(
  43. children: [
  44. Container(
  45. margin: EdgeInsets.only(top: topMargin),
  46. child: TabBar(
  47. isScrollable: true,
  48. labelColor: Colors.black,
  49. unselectedLabelColor: Colors.grey,
  50. controller: _tabController,
  51. tabs: tabs
  52. .map((e) => Tab(
  53. child: Text(
  54. e.partName,
  55. style: TextStyle(
  56. fontSize: tabBarTextSize,
  57. fontWeight: FontWeight.bold),
  58. ),
  59. ))
  60. .toList()),
  61. ),
  62. Expanded(
  63. child: TabBarView(
  64. controller: _tabController,
  65. children: tabs
  66. .map((e) => CountryDataWidget(e.id))
  67. .toList()))
  68. ],
  69. );
  70. } else {
  71. return Text("Error:${snapshot.error}");
  72. }
  73. } else {
  74. return Container(
  75. alignment: Alignment.center,
  76. child: const CircularProgressIndicator(),
  77. );
  78. }
  79. }),
  80. );
  81. }
  82. }