carousel_widget.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:math';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_swiper_plus/flutter_swiper_plus.dart';
  5. import 'package:get_it/get_it.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:sport/bean/carousel.dart';
  8. import 'package:sport/bean/game.dart';
  9. import 'package:sport/pages/game/game_detail.dart';
  10. import 'package:sport/provider/game_model.dart';
  11. import 'package:sport/router/navigator_util.dart';
  12. import 'package:sport/widgets/image.dart';
  13. import 'package:sport/widgets/video.dart';
  14. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  15. import 'package:url_launcher/url_launcher.dart';
  16. class CarouselWidget extends StatelessWidget {
  17. final List<Carousel> items;
  18. const CarouselWidget({Key? key, required this.items}) : super(key: key);
  19. @override
  20. Widget build(BuildContext context) {
  21. var screen = MediaQuery.of(context).size;
  22. return Container(
  23. color: Colors.black,
  24. width: double.infinity,
  25. height: double.infinity,
  26. constraints: BoxConstraints(maxHeight: screen.shortestSide),
  27. child: items.isNotEmpty ? Swiper(
  28. indicatorLayout: PageIndicatorLayout.COLOR,
  29. pagination: SwiperPagination(
  30. alignment: Alignment.bottomRight,
  31. builder: DotSwiperPaginationBuilder(size: 6, activeSize: 6, color: Colors.white.withOpacity(0.5), activeColor: Theme.of(context).colorScheme.secondary),
  32. ),
  33. itemCount: items.length,
  34. itemBuilder: (context, index) {
  35. var item = items[index];
  36. Widget? child;
  37. if (item.type == "video") {
  38. child = VideoWidget(
  39. carousel: item,
  40. autoPlay: index != 0,
  41. );
  42. } else if (item.type == "image") {
  43. child = InkWell(
  44. onTap: () async {
  45. if (item.url.isNotEmpty == true) {
  46. if (item.url.startsWith("game")) {
  47. var uri = Uri.parse(item.url);
  48. GameModel gameModel = GetIt.I<GameModel>();
  49. GameInfoData? game = await gameModel.getGame((uri.queryParameters["id"] as int?) ?? 0);
  50. if (game != null) NavigatorUtil.goPage(context, (context) => GameDetailsPage(game));
  51. } else {
  52. launch(item.url);
  53. }
  54. }
  55. },
  56. child: CachedNetworkImage(
  57. imageUrl: item.cover ?? "",
  58. fit: BoxFit.cover,
  59. ),
  60. );
  61. }
  62. return Stack(
  63. fit: StackFit.expand,
  64. children: [
  65. if (child != null) child,
  66. if (item.outerUrl?.isNotEmpty == true && item.outerDesc?.isNotEmpty == true)
  67. Positioned(
  68. top: 0.0,
  69. right: 0.0,
  70. child: InkWell(
  71. onTap: () {
  72. if (item.outerUrl?.isNotEmpty == true) launch(item.outerUrl!);
  73. },
  74. child: Padding(
  75. padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
  76. child: Row(
  77. children: [
  78. Text(
  79. "${item.outerDesc}",
  80. style: Theme.of(context).textTheme.subtitle2!.copyWith(color: Colors.white),
  81. ),
  82. const SizedBox(
  83. width: 5,
  84. ),
  85. arrowRight(color: Colors.white)
  86. ],
  87. ),
  88. ),
  89. ))
  90. ],
  91. );
  92. }) :Container(),
  93. );
  94. }
  95. }