import 'package:douyin_demo/providers/recommend_provider.dart'; import 'package:douyin_demo/views/swiper_main.dart'; import 'package:flutter/material.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:provider/provider.dart'; import 'center_image.dart'; class MainTabView extends StatelessWidget { const MainTabView({Key? key}) : super(key: key); @override Widget build(BuildContext context) { RecommendProvider provider = Provider.of(context); return TabBarView( controller: provider.controller, children: [ SwiperMain( type: "关注", ), SwiperMain( type: "推荐", ), ], ); } } /// 自定义滑动tab class SwiperMain extends StatefulWidget { SwiperMain({Key? key, required this.type}) : super(key: key); final String type; _SwiperMainState createState() => _SwiperMainState(); } class _SwiperMainState extends State with AutomaticKeepAliveClientMixin { @override Widget build(BuildContext context) { RecommendProvider provider = Provider.of(context); List infos = []; if (widget.type == "followed") { infos = provider.followed; } else { infos = provider.infos; } return Swiper( loop: false, scrollDirection: Axis.vertical, itemCount: infos.length, itemBuilder: (context, index) { MainInfo curData = infos[index]; return Container( decoration: BoxDecoration(color: Colors.black), child: Stack(children: [ CenterImage( videoPath: curData.videoPath, ), Home(), ]), ); }, ); } @override bool get wantKeepAlive => true; }