home_page.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_provider_demo/pages/addpage_page.dart';
  3. import 'package:flutter_provider_demo/provider/car_provider.dart';
  4. import 'package:flutter_provider_demo/views/all_cars.dart';
  5. import 'package:flutter_provider_demo/views/ready_cars.dart';
  6. import 'package:flutter_provider_demo/views/unready_cars.dart';
  7. import 'package:provider/provider.dart';
  8. /// Description: home page
  9. /// Time : 09/03/2023 Sunday
  10. /// Author : liuyuqi.gov@msn.cn
  11. class HomePage extends StatefulWidget {
  12. const HomePage({super.key});
  13. static bool hasData = false;
  14. @override
  15. State<HomePage> createState() => _HomePageState();
  16. }
  17. class _HomePageState extends State<HomePage>
  18. with SingleTickerProviderStateMixin {
  19. late TabController tabController;
  20. @override
  21. void initState() {
  22. super.initState();
  23. tabController = TabController(length: 3, vsync: this);
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. Provider.of<CarProvider>(context).getCars();
  28. if (Provider.of<CarProvider>(context).count > 0) {
  29. HomePage.hasData = true;
  30. }
  31. return Scaffold(
  32. appBar: AppBar(
  33. title: const Text('Your Cars'),
  34. actions: <Widget>[
  35. IconButton(
  36. icon: const Icon(Icons.add),
  37. onPressed: () {
  38. Navigator.of(context).push(
  39. MaterialPageRoute(builder: (context) => const AddcardPage()));
  40. },
  41. ),
  42. ],
  43. bottom: TabBar(
  44. controller: tabController,
  45. tabs: const <Widget>[
  46. Tab(text: 'UnReady'),
  47. Tab(text: 'All'),
  48. Tab(text: 'Ready'),
  49. ],
  50. ),
  51. ),
  52. body: Provider.of<CarProvider>(context).count > 0
  53. ? TabBarView(
  54. controller: tabController,
  55. children: const <Widget>[
  56. AllCarsTab(),
  57. UnReadyCarsTab(),
  58. ReadyCarsTab()
  59. ],
  60. )
  61. : const Center(
  62. child: Text(
  63. '',
  64. style: TextStyle(fontSize: 20.0),
  65. ),
  66. ),
  67. );
  68. }
  69. }