| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- import 'dart:async';
- import 'package:flutter/gestures.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:go_router/go_router.dart';
- import 'package:news_app/widget/my_txt.dart';
- import 'package:flutter_swiper_view/flutter_swiper_view.dart';
- import '../../constant/api_const.dart';
- import '../../constant/color_res.dart';
- import '../../gen/assets.gen.dart';
- import '../../http/http_util.dart';
- import '../../util/log.util.dart';
- import '../../util/shared_prefs_instance_util.dart';
- import '../../widget/load_image.dart';
- /// @author: bo.zeng
- /// @email: cnhbwds@gmail.com
- /// @date: 2025 2025/4/9 16:00
- /// @description:
- class SplashPage extends StatefulWidget {
- const SplashPage({super.key});
- @override
- State<SplashPage> createState() => _SplashPageState();
- }
- class _SplashPageState extends State<SplashPage> with SingleTickerProviderStateMixin {
- TapGestureRecognizer? aa;
- List<String> _splashImages = [];
- int _currentIndex = 0;
- late Timer _sliderTimer;
- Timer? _navigateTimer;
- bool _hasStartedNavigation = false;
- // 本地默认开屏图片(当网络不可用时使用)
- static const List<String> _defaultSplashImages = [
- 'assets/images/splash_bg.png', // 本地默认图片
- ];
- @override
- void initState() {
- super.initState();
- // 初始化使用本地默认图片
- _splashImages = _defaultSplashImages;
- // 启动轮播
- _startSlider();
- // 从后台获取开屏页配置
- _fetchSplashConfig();
- }
- /// 开始轮播
- void _startSlider() {
- _sliderTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
- if (mounted && _splashImages.isNotEmpty) {
- setState(() {
- _currentIndex = (_currentIndex + 1) % _splashImages.length;
- });
- }
- });
- }
- /// 从后台获取开屏页配置
- Future<void> _fetchSplashConfig() async {
- try {
- final response = await HttpUtil().get(apiSplashConfig);
- if (response != null && response['code'] == 200) {
- final data = response['data'];
- if (data != null) {
- // 支持多种数据格式
- List<String> fetchedImages = [];
- // 格式1: imageUrl 为单个图片
- if (data['imageUrl'] != null && data['imageUrl'].toString().isNotEmpty) {
- fetchedImages.add(data['imageUrl']);
- }
- // 格式2: images 为图片数组
- if (data is List) {
- for (var img in data) {
- if (img.toString().isNotEmpty) {
- fetchedImages.add(img['url']);
- }
- }
- }
- // 格式3: imageList 为图片数组
- if (data['imageList'] is List) {
- final images = data['imageList'] as List;
- for (var img in images) {
- if (img.toString().isNotEmpty) {
- fetchedImages.add(img.toString());
- }
- }
- }
- // 如果获取到了图片,更新显示
- if (fetchedImages.isNotEmpty && mounted) {
- setState(() {
- _splashImages = fetchedImages;
- _currentIndex = 0;
- });
- }
- }
- }
- } catch (e) {
- // 请求失败或无网络权限,继续使用本地默认图片,不做任何处理
- consoleLog('Splash fetch error (will use default): $e');
- }
- // 延迟 3-5 秒后执行跳转逻辑
- _startNavigationTimer();
- }
- /// 启动导航计时器
- void _startNavigationTimer() {
- if (_hasStartedNavigation) return;
- _hasStartedNavigation = true;
- _navigateTimer = Timer(const Duration(seconds: 4), () async {
- bool? first = await getIsFirst();
- // 确保页面未被销毁
- if (!mounted) return;
- if (first == true) {
- context.go('/main');
- } else {
- _showFirstDialog(context);
- }
- });
- }
- @override
- void dispose() {
- _sliderTimer.cancel();
- _navigateTimer?.cancel();
- aa?.dispose();
- super.dispose();
- }
- void _showFirstDialog(BuildContext context) {
- aa = TapGestureRecognizer();
- aa?.onTap = () {
- context.push("/user/privacy");
- };
- showDialog(
- context: context,
- builder: (context) {
- return AlertDialog(
- content: SizedBox(
- height: 200.h,
- width: 220.w,
- child: SingleChildScrollView(
- child: Column(
- children: [
- Text(
- "个人信息保护提示",
- style: TextStyle(fontSize: 14.sp, color: Colors.black),
- ),
- Text.rich(
- TextSpan(
- children: [
- TextSpan(
- text: "欢迎使用新消费传媒!我们将通过",
- style: TextStyle(
- fontSize: 12.sp,
- color: Colors.black,
- ),
- ),
- TextSpan(
- text: "《用户协议》",
- style: TextStyle(fontSize: 12.sp, color: Colors.blue),
- recognizer: aa,
- // recognizer: _tapRecognizer
- ),
- TextSpan(
- text: "和",
- style: TextStyle(
- fontSize: 12.sp,
- color: Colors.black,
- ),
- ),
- TextSpan(
- text: "《隐私政策》",
- style: TextStyle(fontSize: 12.sp, color: Colors.blue),
- recognizer: aa,
- ),
- TextSpan(
- text:
- "和帮助您了解我们为您提供的服务、"
- "我们如何处理个人信息以及您享有的权利。我们会严格按照相关法律法规要求,采取各种安全措施来保护您的个人信息。\n"
- "点击“同意”按钮,表示您已知情并同意以上协议和以下约定。\n"
- "1.为了保障软件的安全运行和账户安全我们会申请收集您的设备信息、IP地址WLAN MAC地址。\n"
- "2.上传或拍摄图片、视频,需要使用您的媒体影音、图片、视频、音频、相机、麦克风权限。\n"
- "3.我们可能会申请位置权限,用于为您推荐您可能感兴趣的内容。\n"
- "4.我们尊重您的选择权,您可以访问、修改、删除您的个人信息并管理您的授权,我们也为您提供注销、投诉渠道。",
- style: TextStyle(
- fontSize: 12.sp,
- color: Colors.black,
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- actions: [
- GestureDetector(
- onTap: () {
- saveIsFirst();
- context.go('/main');
- },
- child: Container(
- alignment: Alignment.center,
- padding: EdgeInsets.symmetric(vertical: 6.h),
- decoration: BoxDecoration(
- color: colorE71717,
- borderRadius: BorderRadius.circular(10.r),
- ),
- child: myTxt(text: "同意", color: Colors.white, fontSize: 14.sp),
- ),
- ),
- GestureDetector(
- onTap: () {
- SystemNavigator.pop();
- },
- child: Container(
- margin: EdgeInsets.only(top: 5.h),
- padding: EdgeInsets.symmetric(vertical: 6.h),
- alignment: Alignment.center,
- child: myTxt(text: "不同意", color: Colors.black, fontSize: 14.sp),
- ),
- ),
- ],
- );
- },
- );
- }
- @override
- Widget build(BuildContext context) {
- double w = MediaQuery.of(context).size.width;
- double h = MediaQuery.of(context).size.height;
- return Scaffold(
- body: SizedBox(
- width: w,
- height: h,
- child: Stack(
- children: [
- // 轮播图片
- if (_splashImages.length == 1)
- // 只有一张图片时直接显示
- _buildSingleImage()
- else
- // 多张图片时使用轮播
- _buildCarousel(w, h),
- // 指示器(多张图片时显示)
- if (_splashImages.length > 1)
- Positioned(
- bottom: 50.h,
- left: 0,
- right: 0,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: List.generate(
- _splashImages.length,
- (index) => AnimatedContainer(
- duration: const Duration(milliseconds: 300),
- margin: EdgeInsets.symmetric(horizontal: 4.w),
- width: _currentIndex == index ? 20.w : 8.w,
- height: 8.h,
- decoration: BoxDecoration(
- color: _currentIndex == index
- ? Colors.white
- : Colors.white.withOpacity(0.5),
- borderRadius: BorderRadius.circular(4.r),
- ),
- ),
- ),
- ),
- ),
- // 跳过按钮
- Positioned(
- top: 54.h,
- right: 20.w,
- child: GestureDetector(
- onTap: () {
- _navigateTimer?.cancel();
- _navigateToMain();
- },
- child: Container(
- padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
- decoration: BoxDecoration(
- color: Colors.black.withOpacity(0.3),
- borderRadius: BorderRadius.circular(20.r),
- ),
- child: myTxt(
- text: "跳过",
- color: Colors.white,
- fontSize: 14.sp,
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- );
- }
- /// 构建单张图片
- Widget _buildSingleImage() {
- final imageUrl = _splashImages.first;
- if (imageUrl.startsWith('http') || imageUrl.startsWith('https')) {
- return LoadImage(imageUrl, fit: BoxFit.cover);
- } else {
- return Image.asset(imageUrl.startsWith('assets') ? imageUrl : 'assets/$imageUrl', fit: BoxFit.cover);
- }
- }
- /// 构建轮播
- Widget _buildCarousel(double w, double h) {
- return Swiper(
- itemCount: _splashImages.length,
- index: _currentIndex,
- onIndexChanged: (index) {
- if (mounted) {
- setState(() {
- _currentIndex = index;
- });
- }
- },
- itemBuilder: (BuildContext context, int index) {
- final imageUrl = _splashImages[index];
- if (imageUrl.startsWith('http') || imageUrl.startsWith('https')) {
- return LoadImage(imageUrl, fit: BoxFit.cover);
- } else {
- return Image.asset(
- imageUrl.startsWith('assets') ? imageUrl : 'assets/$imageUrl',
- fit: BoxFit.cover,
- );
- }
- },
- autoplay: false, // 使用自定义定时器控制
- loop: true,
- viewportFraction: 1.0,
- scale: 1.0,
- );
- }
- /// 导航到主页
- void _navigateToMain() async {
- bool? first = await getIsFirst();
- if (!mounted) return;
- if (first == true) {
- context.go('/main');
- } else {
- _showFirstDialog(context);
- }
- }
- }
|