import 'dart:math'; import 'package:PiliPlus/pages/setting/widgets/switch_item.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:hive/hive.dart'; class PlaySpeedPage extends StatefulWidget { const PlaySpeedPage({super.key}); @override State createState() => _PlaySpeedPageState(); } class _PlaySpeedPageState extends State { late double playSpeedDefault; late double longPressSpeedDefault; late List speedList; late bool enableAutoLongPressSpeed; List> sheetMenu = [ { 'id': 1, 'title': '设置为默认倍速', 'leading': const Icon( Icons.speed, size: 21, ), }, { 'id': 2, 'title': '设置为默认长按倍速', 'leading': const Icon( Icons.speed_sharp, size: 21, ), }, { 'id': -1, 'title': '删除该项', 'leading': const Icon( Icons.delete_outline, size: 21, ), }, ]; Box get video => GStorage.video; @override void initState() { super.initState(); // 默认倍速 playSpeedDefault = video.get(VideoBoxKey.playSpeedDefault, defaultValue: 1.0); // 默认长按倍速 longPressSpeedDefault = video.get(VideoBoxKey.longPressSpeedDefault, defaultValue: 3.0); // 倍速 speedList = GStorage.speedList; enableAutoLongPressSpeed = GStorage.setting .get(SettingBoxKey.enableAutoLongPressSpeed, defaultValue: false); if (enableAutoLongPressSpeed) { Map newItem = sheetMenu[1]; newItem['show'] = false; setState(() { sheetMenu[1] = newItem; }); } } // 添加自定义倍速 void onAddSpeed() { double? customSpeed; showDialog( context: context, builder: (context) { return AlertDialog( title: const Text('添加倍速'), content: Column( mainAxisSize: MainAxisSize.min, children: [ const SizedBox(height: 12), TextField( autofocus: true, keyboardType: const TextInputType.numberWithOptions(decimal: true), decoration: const InputDecoration( labelText: '自定义倍速', border: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(6)), ), ), onChanged: (value) { customSpeed = double.tryParse(value); }, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'[\d\.]+')), ], ), ], ), actions: [ TextButton( onPressed: Get.back, child: const Text('取消'), ), TextButton( onPressed: () { if (customSpeed == null) { SmartDialog.showToast('输入倍数不合法'); } else if (speedList.contains(customSpeed)) { SmartDialog.showToast('该倍速已存在'); } else { Get.back(); speedList ..add(customSpeed!) ..sort(); video.put(VideoBoxKey.speedsList, speedList); setState(() {}); } }, child: const Text('确认'), ) ], ); }, ); } // 设定倍速弹窗 void showBottomSheet(ThemeData theme, int index) { showModalBottomSheet( context: context, useSafeArea: true, isScrollControlled: true, clipBehavior: Clip.hardEdge, constraints: BoxConstraints( maxWidth: min(640, min(Get.width, Get.height)), ), builder: (context) { return Column( mainAxisSize: MainAxisSize.min, children: [ const SizedBox(height: 10), ...sheetMenu.map( (item) => ListTile( onTap: () { Navigator.pop(context); menuAction(index, item['id']); }, minLeadingWidth: 0, iconColor: theme.colorScheme.onSurface, leading: item['leading'], title: Text( item['title'], style: theme.textTheme.titleSmall, ), ), ), SizedBox(height: 25 + MediaQuery.paddingOf(context).bottom), ], ); }, ); } // void menuAction(index, id) { double speed = speedList[index]; // 设置 if (id == 1) { // 设置默认倍速 playSpeedDefault = speed; video.put(VideoBoxKey.playSpeedDefault, playSpeedDefault); } else if (id == 2) { // 设置默认长按倍速 longPressSpeedDefault = speed; video.put(VideoBoxKey.longPressSpeedDefault, longPressSpeedDefault); } else if (id == -1) { if ([ 1.0, playSpeedDefault, longPressSpeedDefault, ].contains(speed)) { SmartDialog.showToast('不支持删除默认倍速'); return; } speedList.removeAt(index); video.put(VideoBoxKey.speedsList, speedList); } setState(() {}); } @override Widget build(BuildContext context) { final theme = Theme.of(context); return Scaffold( appBar: AppBar( title: const Text('倍速设置'), actions: [ TextButton( onPressed: () async { await video.delete(VideoBoxKey.speedsList); speedList = GStorage.speedList; setState(() {}); }, child: const Text('重置'), ), const SizedBox(width: 16), ], ), body: SafeArea( bottom: false, child: ListView( children: [ Padding( padding: const EdgeInsets.only(left: 14, right: 14, top: 6, bottom: 0), child: Text( '点击下方按钮设置默认(长按)倍速', style: TextStyle(color: theme.colorScheme.outline), ), ), ListTile( title: const Text('默认倍速'), subtitle: Text(playSpeedDefault.toString()), ), SetSwitchItem( title: '动态长按倍速', subtitle: '根据默认倍速长按时自动双倍', setKey: SettingBoxKey.enableAutoLongPressSpeed, defaultVal: enableAutoLongPressSpeed, onChanged: (val) { Map newItem = sheetMenu[1]; val ? newItem['show'] = false : newItem['show'] = true; setState(() { sheetMenu[1] = newItem; enableAutoLongPressSpeed = val; }); }, ), if (!enableAutoLongPressSpeed) ListTile( title: const Text('默认长按倍速'), subtitle: Text(longPressSpeedDefault.toString()), ), Padding( padding: const EdgeInsets.only( left: 14, right: 14, bottom: 10, top: 20, ), child: Row( children: [ Text( '倍速列表', style: theme.textTheme.titleMedium, ), const SizedBox(width: 12), TextButton( onPressed: onAddSpeed, child: const Text('添加'), ), ], ), ), Padding( padding: const EdgeInsets.only( left: 18, right: 18, bottom: 30, ), child: Wrap( alignment: WrapAlignment.start, spacing: 8, runSpacing: 2, children: List.generate( speedList.length, (index) => FilledButton.tonal( onPressed: () => showBottomSheet(theme, index), child: Text(speedList[index].toString()), ), ), ), ), ], ), ), ); } }