main_tab_view.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:douyin_demo/providers/recommend_provider.dart';
  2. import 'package:douyin_demo/views/swiper_main.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_swiper/flutter_swiper.dart';
  5. import 'package:provider/provider.dart';
  6. import 'center_image.dart';
  7. class MainTabView extends StatelessWidget {
  8. const MainTabView({Key? key}) : super(key: key);
  9. @override
  10. Widget build(BuildContext context) {
  11. RecommendProvider provider = Provider.of<RecommendProvider>(context);
  12. return TabBarView(
  13. controller: provider.controller,
  14. children: [
  15. SwiperMain(
  16. type: "关注",
  17. ),
  18. SwiperMain(
  19. type: "推荐",
  20. ),
  21. ],
  22. );
  23. }
  24. }
  25. /// 自定义滑动tab
  26. class SwiperMain extends StatefulWidget {
  27. SwiperMain({Key? key, required this.type}) : super(key: key);
  28. final String type;
  29. _SwiperMainState createState() => _SwiperMainState();
  30. }
  31. class _SwiperMainState extends State<SwiperMain>
  32. with AutomaticKeepAliveClientMixin {
  33. @override
  34. Widget build(BuildContext context) {
  35. RecommendProvider provider = Provider.of<RecommendProvider>(context);
  36. List<MainInfo> infos = <MainInfo>[];
  37. if (widget.type == "followed") {
  38. infos = provider.followed;
  39. } else {
  40. infos = provider.infos;
  41. }
  42. return Swiper(
  43. loop: false,
  44. scrollDirection: Axis.vertical,
  45. itemCount: infos.length,
  46. itemBuilder: (context, index) {
  47. MainInfo curData = infos[index];
  48. return Container(
  49. decoration: BoxDecoration(color: Colors.black),
  50. child: Stack(children: [
  51. CenterImage(
  52. videoPath: curData.videoPath,
  53. ),
  54. Home(),
  55. ]),
  56. );
  57. },
  58. );
  59. }
  60. @override
  61. bool get wantKeepAlive => true;
  62. }