setting_page.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:shirne_dialog/shirne_dialog.dart';
  4. import '../global.dart';
  5. import '../models/engine_type.dart';
  6. import '../models/engine_level.dart';
  7. import '../models/game_setting.dart';
  8. /// 设置页
  9. class SettingPage extends StatefulWidget {
  10. const SettingPage({Key? key}) : super(key: key);
  11. @override
  12. State<SettingPage> createState() => _SettingPageState();
  13. }
  14. class _SettingPageState extends State<SettingPage> {
  15. // 全局设置
  16. GameSetting? setting;
  17. @override
  18. void initState() {
  19. super.initState();
  20. GameSetting.getInstance().then(
  21. (value) => setState(() {
  22. setting = value;
  23. }),
  24. );
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. double width = 500;
  29. if (MediaQuery.of(context).size.width < width) {
  30. width = MediaQuery.of(context).size.width;
  31. }
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: Text(context.l10n.settingTitle),
  35. actions: [
  36. TextButton(
  37. onPressed: () {
  38. setting?.save().then((v) {
  39. Navigator.pop(context);
  40. MyDialog.toast('保存成功', iconType: IconType.success);
  41. });
  42. },
  43. child: const Text(
  44. '保存',
  45. style: TextStyle(color: Colors.white),
  46. ),
  47. ),
  48. ],
  49. ),
  50. body: Center(
  51. child: setting == null
  52. ? const CircularProgressIndicator()
  53. : Container(
  54. width: width,
  55. padding: const EdgeInsets.all(20),
  56. child: SingleChildScrollView(
  57. child: ListBody(
  58. children: [
  59. ListTile(
  60. title: const Text('AI类型'),
  61. trailing: CupertinoSegmentedControl(
  62. onValueChanged: (value) {
  63. if (value == null) return;
  64. setState(() {
  65. setting!.robotType = value;
  66. });
  67. },
  68. groupValue: setting!.robotType,
  69. children: const {
  70. EngineType.builtIn: Padding(
  71. padding: EdgeInsets.symmetric(
  72. horizontal: 10,
  73. ),
  74. child: Text('内置引擎'),
  75. ),
  76. EngineType.elephantEye: Padding(
  77. padding: EdgeInsets.symmetric(
  78. horizontal: 10,
  79. ),
  80. child: Text('elephantEye'),
  81. ),
  82. EngineType.pikafish: Padding(
  83. padding: EdgeInsets.symmetric(
  84. horizontal: 10,
  85. ),
  86. child: Text('皮卡鱼'),
  87. ),
  88. },
  89. ),
  90. ),
  91. ListTile(
  92. title: const Text('AI级别'),
  93. trailing: CupertinoSegmentedControl(
  94. onValueChanged: (value) {
  95. if (value == null) return;
  96. setState(() {
  97. setting!.robotLevel = value as int;
  98. });
  99. },
  100. groupValue: setting!.robotLevel,
  101. children: const {
  102. EngineLevel.learn: Padding(
  103. padding: EdgeInsets.symmetric(
  104. horizontal: 10,
  105. ),
  106. child: Text('初级'),
  107. ),
  108. EngineLevel.middle: Padding(
  109. padding: EdgeInsets.symmetric(
  110. horizontal: 10,
  111. ),
  112. child: Text('中级'),
  113. ),
  114. EngineLevel.master: Padding(
  115. padding: EdgeInsets.symmetric(
  116. horizontal: 10,
  117. ),
  118. child: Text('大师'),
  119. )
  120. },
  121. ),
  122. ),
  123. ListTile(
  124. title: const Text('游戏声音'),
  125. trailing: CupertinoSwitch(
  126. value: setting!.sound,
  127. onChanged: (v) {
  128. setState(() {
  129. setting!.sound = v;
  130. });
  131. },
  132. ),
  133. ),
  134. ListTile(
  135. title: const Text('游戏音量'),
  136. trailing: CupertinoSlider(
  137. value: setting!.soundVolume,
  138. min: 0,
  139. max: 1,
  140. onChanged: (v) {
  141. setState(() {
  142. setting!.soundVolume = v;
  143. });
  144. },
  145. ),
  146. ),
  147. ],
  148. ),
  149. ),
  150. ),
  151. ),
  152. );
  153. }
  154. }