SlideView.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:flutter/material.dart';
  2. import '../pages/news_detail_page.dart';
  3. class SlideView extends StatefulWidget {
  4. var data;
  5. SlideView(data) {
  6. this.data = data;
  7. }
  8. @override
  9. State<StatefulWidget> createState() {
  10. return SlideViewState(data);
  11. }
  12. }
  13. class SlideViewState extends State<SlideView> with SingleTickerProviderStateMixin {
  14. TabController? tabController;
  15. List? slideData;
  16. SlideViewState(data) {
  17. slideData = data;
  18. }
  19. @override
  20. void initState() {
  21. super.initState();
  22. tabController = TabController(length: slideData == null ? 0 : slideData!.length, vsync: this);
  23. }
  24. @override
  25. void dispose() {
  26. tabController!.dispose();
  27. super.dispose();
  28. }
  29. Widget generateCard() {
  30. return Card(
  31. color: Colors.blue,
  32. child: Image.asset("images/ic_avatar_default.png", width: 20.0, height: 20.0,),
  33. );
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. List<Widget> items = [];
  38. if (slideData != null && slideData!.length > 0) {
  39. for (var i = 0; i < slideData!.length; i++) {
  40. var item = slideData![i];
  41. var imgUrl = item['imgUrl'];
  42. var title = item['title'];
  43. var detailUrl = item['detailUrl'];
  44. items.add(GestureDetector(
  45. onTap: () {
  46. // 点击跳转到详情
  47. Navigator.of(context).push(MaterialPageRoute(
  48. builder: (ctx) => NewsDetailPage(id: detailUrl)
  49. ));
  50. },
  51. child: Stack(
  52. children: [
  53. Image.network(imgUrl),
  54. Container(
  55. width: MediaQuery.of(context).size.width,
  56. color: const Color(0x50000000),
  57. child: Padding(
  58. padding: const EdgeInsets.all(6.0),
  59. child: Text(title, style: TextStyle(color: Colors.white, fontSize: 15.0)),
  60. )
  61. )
  62. ],
  63. ),
  64. ));
  65. }
  66. }
  67. return TabBarView(
  68. controller: tabController,
  69. children: items,
  70. );
  71. }
  72. }