import 'package:flutter/material.dart'; import '../pages/news_detail_page.dart'; class SlideView extends StatefulWidget { var data; SlideView(data) { this.data = data; } @override State createState() { return SlideViewState(data); } } class SlideViewState extends State with SingleTickerProviderStateMixin { TabController? tabController; List? slideData; SlideViewState(data) { slideData = data; } @override void initState() { super.initState(); tabController = TabController(length: slideData == null ? 0 : slideData!.length, vsync: this); } @override void dispose() { tabController!.dispose(); super.dispose(); } Widget generateCard() { return Card( color: Colors.blue, child: Image.asset("images/ic_avatar_default.png", width: 20.0, height: 20.0,), ); } @override Widget build(BuildContext context) { List items = []; if (slideData != null && slideData!.length > 0) { for (var i = 0; i < slideData!.length; i++) { var item = slideData![i]; var imgUrl = item['imgUrl']; var title = item['title']; var detailUrl = item['detailUrl']; items.add(GestureDetector( onTap: () { // 点击跳转到详情 Navigator.of(context).push(MaterialPageRoute( builder: (ctx) => NewsDetailPage(id: detailUrl) )); }, child: Stack( children: [ Image.network(imgUrl), Container( width: MediaQuery.of(context).size.width, color: const Color(0x50000000), child: Padding( padding: const EdgeInsets.all(6.0), child: Text(title, style: TextStyle(color: Colors.white, fontSize: 15.0)), ) ) ], ), )); } } return TabBarView( controller: tabController, children: items, ); } }