desktop_settings.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_hbb/common.dart';
  3. import 'package:flutter_hbb/models/platform_model.dart';
  4. import 'package:flutter_hbb/plugin/model.dart';
  5. import 'package:flutter_hbb/plugin/common.dart';
  6. import 'package:get/get.dart';
  7. import '../manager.dart';
  8. import './desc_ui.dart';
  9. // to-do: use settings from desktop_setting_page.dart
  10. const double _kCardFixedWidth = 540;
  11. const double _kCardLeftMargin = 15;
  12. const double _kContentHMargin = 15;
  13. const double _kTitleFontSize = 20;
  14. const double _kVersionFontSize = 12;
  15. class DesktopSettingsCard extends StatefulWidget {
  16. final PluginInfo plugin;
  17. DesktopSettingsCard({
  18. Key? key,
  19. required this.plugin,
  20. }) : super(key: key);
  21. @override
  22. State<DesktopSettingsCard> createState() => _DesktopSettingsCardState();
  23. }
  24. class _DesktopSettingsCardState extends State<DesktopSettingsCard> {
  25. PluginInfo get plugin => widget.plugin;
  26. bool get installed => plugin.installed;
  27. bool isEnabled = false;
  28. @override
  29. Widget build(BuildContext context) {
  30. isEnabled = bind.pluginIsEnabled(id: plugin.meta.id);
  31. return Row(
  32. children: [
  33. Flexible(
  34. child: SizedBox(
  35. width: _kCardFixedWidth,
  36. child: Card(
  37. child: Column(
  38. children: [
  39. header(),
  40. body(),
  41. ],
  42. ).marginOnly(bottom: 10),
  43. ).marginOnly(left: _kCardLeftMargin, top: 15),
  44. ),
  45. ),
  46. ],
  47. );
  48. }
  49. Widget header() {
  50. return Row(
  51. children: [
  52. headerNameVersion(),
  53. headerInstallEnable(),
  54. ],
  55. ).marginOnly(
  56. left: _kContentHMargin,
  57. top: 10,
  58. bottom: 10,
  59. right: _kContentHMargin,
  60. );
  61. }
  62. Widget headerNameVersion() {
  63. return Expanded(
  64. child: Row(
  65. children: [
  66. Text(
  67. widget.plugin.meta.name,
  68. textAlign: TextAlign.start,
  69. style: const TextStyle(
  70. fontSize: _kTitleFontSize,
  71. ),
  72. ),
  73. SizedBox(
  74. width: 5,
  75. ),
  76. Text(
  77. plugin.meta.version,
  78. textAlign: TextAlign.start,
  79. style: const TextStyle(
  80. fontSize: _kVersionFontSize,
  81. ),
  82. )
  83. ],
  84. ),
  85. );
  86. }
  87. Widget headerButton(String label, VoidCallback onPressed) {
  88. return Container(
  89. child: ElevatedButton(
  90. onPressed: onPressed,
  91. child: Text(translate(label)),
  92. ),
  93. );
  94. }
  95. Widget headerInstallEnable() {
  96. final installButton = headerButton(
  97. installed ? 'Uninstall' : 'Install',
  98. () {
  99. bind.pluginInstall(
  100. id: plugin.meta.id,
  101. b: !installed,
  102. );
  103. },
  104. );
  105. if (installed) {
  106. final updateButton = plugin.needUpdate
  107. ? headerButton('Update', () {
  108. bind.pluginInstall(
  109. id: plugin.meta.id,
  110. b: !installed,
  111. );
  112. })
  113. : Container();
  114. final enableButton = !installed
  115. ? Container()
  116. : headerButton(isEnabled ? 'Disable' : 'Enable', () {
  117. if (isEnabled) {
  118. clearPlugin(plugin.meta.id);
  119. }
  120. bind.pluginEnable(id: plugin.meta.id, v: !isEnabled);
  121. setState(() {});
  122. });
  123. return Row(
  124. children: [
  125. updateButton,
  126. SizedBox(
  127. width: 10,
  128. ),
  129. installButton,
  130. SizedBox(
  131. width: 10,
  132. ),
  133. enableButton,
  134. ],
  135. );
  136. } else {
  137. return installButton;
  138. }
  139. }
  140. Widget body() {
  141. return Column(children: [
  142. author(),
  143. description(),
  144. more(),
  145. ]).marginOnly(
  146. left: _kCardLeftMargin,
  147. top: 4,
  148. right: _kContentHMargin,
  149. );
  150. }
  151. Widget author() {
  152. return Align(
  153. alignment: Alignment.centerLeft,
  154. child: Text(plugin.meta.author),
  155. );
  156. }
  157. Widget description() {
  158. return Align(
  159. alignment: Alignment.centerLeft,
  160. child: Text(plugin.meta.description),
  161. );
  162. }
  163. Widget more() {
  164. if (!(installed && isEnabled)) {
  165. return Container();
  166. }
  167. final List<Widget> children = [];
  168. final model = getPluginModel(kLocationHostMainPlugin, plugin.meta.id);
  169. if (model != null) {
  170. children.add(PluginItem(
  171. pluginId: plugin.meta.id,
  172. peerId: '',
  173. location: kLocationHostMainPlugin,
  174. pluginModel: model,
  175. isMenu: false,
  176. ));
  177. }
  178. return ExpansionTile(
  179. title: Text('Options'),
  180. controlAffinity: ListTileControlAffinity.leading,
  181. children: children,
  182. );
  183. }
  184. }