setting_page.dart 4.72 KB
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:wow_english/common/utils/video_rendering_preference.dart';
import 'package:wow_english/common/widgets/we_app_bar.dart';
import 'package:wow_english/utils/toast_util.dart';

import '../../../route/route.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return SettingPageState();
  }
}

class SettingPageState extends State<SettingPage> {
  String? _version;
  String? _buildNum;
  bool _usePlatformView = false;
  @override
  void initState() {
    super.initState();
    _usePlatformView = VideoRenderingPreference.usesPlatformView;
    _retrieveVersionInfo();
  }

  Future<void> _retrieveVersionInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      _version = packageInfo.version;
      _buildNum = packageInfo.buildNumber;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const WEAppBar(
        titleText: '设置',
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 17.w, right: 17.w),
        child: SafeArea(
          child: Center(
            child: ListView(
              children: [
                34.verticalSpace,
                _buildItemWidget('注销账号', onPress: () {
                  pushNamed(AppRouteName.deleteAccount);
                }),
                12.verticalSpace,
                _buildItemWidget('清除缓存', onPress: () {
                  EasyLoading.show();
                  Future.delayed(const Duration(seconds: 1), (){
                    showToast("清除成功");
                    EasyLoading.dismiss();
                  }); 
                }),
                12.verticalSpace,
                _buildItemWidget('帮助与反馈', onPress: () {
                  pushNamed(AppRouteName.reBack);
                }),
                if (defaultTargetPlatform == TargetPlatform.android) ...[
                  12.verticalSpace,
                  _buildVideoRenderingSetting(),
                ],
                12.verticalSpace,
                _buildItemWidget('Version: $_version Build:$_buildNum',
                    onPress: () {}),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildVideoRenderingSetting() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h),
      decoration: BoxDecoration(
        border: Border.all(color: const Color(0xFF140C10), width: 1.5.w),
        borderRadius: BorderRadius.circular(15.r),
        color: Colors.white,
      ),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '视频兼容模式',
                  style: TextStyle(
                      color: const Color(0xFF333333), fontSize: 19.sp),
                ),
                4.verticalSpace,
                Text(
                  _usePlatformView
                      ? 'PlatformView(兼容测试)· 新打开的视频生效'
                      : 'Texture(默认)· 新打开的视频生效',
                  style: TextStyle(
                      color: const Color(0xFF777777), fontSize: 13.sp),
                ),
              ],
            ),
          ),
          Switch.adaptive(
            value: _usePlatformView,
            onChanged: (value) async {
              setState(() => _usePlatformView = value);
              await VideoRenderingPreference.setUsesPlatformView(value);
              showToast(value ? '已切换为 PlatformView' : '已切换为 Texture');
            },
          ),
        ],
      ),
    );
  }

  Widget _buildItemWidget(String text, {VoidCallback? onPress}) {
    return OutlinedButton(
      onPressed: () => onPress?.call(),
      style: ButtonStyle(
        side: MaterialStateProperty.all(
            BorderSide(color: const Color(0xFF140C10), width: 1.5.w)),
        shape: MaterialStateProperty.all(
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.r))),
        minimumSize: MaterialStateProperty.all(Size(double.infinity, 58.h)),
        backgroundColor: MaterialStateProperty.all(Colors.white),
      ),
      child: Text(
        text,
        style: TextStyle(
          //fontWeight: FontWeight.w600,
          color: const Color(0xFF333333),
          fontSize: 21.sp,
        ),
      ),
    );
  }
}