calculator_page.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_math_fork/flutter_math.dart';
  5. import 'package:provider/provider.dart';
  6. import 'curve_plot.dart';
  7. import 'expr_editor.dart';
  8. import 'expr_node.dart';
  9. import 'expr_parse.dart';
  10. import 'symja.dart';
  11. import 'symja_functions.dart';
  12. /// 科学计算器(默认主界面)。
  13. ///
  14. /// 输入用**结构化节点树**(见 lib/expr_node.dart),不是字符串:
  15. /// 按键把节点插进当前光标槽,分数竖排、指数上标、多参数函数各有自己的槽。
  16. /// 纯文本输入留给控制台。
  17. class CalculatorPage extends StatefulWidget {
  18. const CalculatorPage({super.key});
  19. @override
  20. State<CalculatorPage> createState() => _CalculatorPageState();
  21. }
  22. class _CalculatorPageState extends State<CalculatorPage> {
  23. final _doc = ExprDoc();
  24. SymjaResult? _preview;
  25. SymjaResult? _committed;
  26. String? _error;
  27. Timer? _debounce;
  28. int _seq = 0;
  29. /// 用户点了"光标已经在的地方"—— 显示粘贴入口。
  30. bool _pastePrompt = false;
  31. @override
  32. void dispose() {
  33. _debounce?.cancel();
  34. super.dispose();
  35. }
  36. // ------------------------------------------------------------------ 求值
  37. /// 编辑器每次变化都走这里:清掉上次的提交态,防抖后重算预览。
  38. void _onDocChanged() {
  39. setState(() {
  40. _committed = null;
  41. _error = null;
  42. _pastePrompt = false; // 动过内容/光标就收起操作栏
  43. });
  44. _debounce?.cancel();
  45. _debounce = Timer(const Duration(milliseconds: 500), _runPreview);
  46. }
  47. String get _source => _doc.toSymja();
  48. void _snack(String msg) {
  49. if (!mounted) return;
  50. ScaffoldMessenger.of(context)
  51. ..hideCurrentSnackBar()
  52. ..showSnackBar(SnackBar(content: Text(msg)));
  53. }
  54. Future<void> _copy(String text, String what) async {
  55. await Clipboard.setData(ClipboardData(text: text));
  56. _snack('已复制$what:$text');
  57. }
  58. /// 从剪贴板读一段 Symja 表达式,解析成节点树后整体替换。
  59. ///
  60. /// 解析器只认一个明确的子集,**认不出来就报错**,不会猜着解析 ——
  61. /// 猜错了你会看到算错的结果,那比报错糟糕得多。
  62. Future<void> _paste() async {
  63. final data = await Clipboard.getData(Clipboard.kTextPlain);
  64. final text = (data?.text ?? '').trim();
  65. if (text.isEmpty) {
  66. _snack('剪贴板里没有文本');
  67. return;
  68. }
  69. try {
  70. final parsed = parseSymjaExpr(text);
  71. setState(() {
  72. _pastePrompt = false;
  73. if (_doc.selection != null) _doc.deleteSelection(); // 有选区就替换掉
  74. _doc.insertNodesAtCursor(parsed.children); // 否则插到光标处
  75. _preview = null;
  76. _committed = null;
  77. _error = null;
  78. });
  79. _onDocChanged();
  80. _snack('已粘贴到光标处:${_doc.toSymja()}');
  81. } on ExprParseException catch (e) {
  82. _snack('这段表达式没法转成按钮式输入 —— $e');
  83. }
  84. }
  85. // ------------------------------------------------------------------ 选区操作
  86. Future<void> _copySelection() async {
  87. final t = _doc.selectionText;
  88. if (t.isEmpty) return;
  89. await _copy(t, '选中内容');
  90. setState(() => _doc.clearSelection());
  91. }
  92. Future<void> _cutSelection() async {
  93. final t = _doc.selectionText;
  94. if (t.isEmpty) return;
  95. await Clipboard.setData(ClipboardData(text: t));
  96. setState(() {
  97. _doc.deleteSelection();
  98. _pastePrompt = false;
  99. });
  100. _onDocChanged();
  101. _snack('已剪切:$t');
  102. }
  103. void _deleteSelection() {
  104. setState(() {
  105. _doc.deleteSelection();
  106. _pastePrompt = false;
  107. });
  108. _onDocChanged();
  109. }
  110. void _dismissBar() {
  111. setState(() {
  112. _pastePrompt = false;
  113. _doc.clearSelection();
  114. });
  115. }
  116. /// 选中部分内容后自动出现的操作栏;或点了光标处时的粘贴入口。
  117. Widget? _contextBar() {
  118. final sel = _doc.selection;
  119. final hasSel = sel != null && !sel.isEmpty;
  120. if (!hasSel && !_pastePrompt) return null;
  121. final scheme = Theme.of(context).colorScheme;
  122. return Material(
  123. color: scheme.surfaceContainerHighest,
  124. child: Padding(
  125. padding: const EdgeInsets.only(left: 12, right: 4),
  126. child: Row(
  127. children: [
  128. Expanded(
  129. child: Text(
  130. hasSel ? '已选中 ${_doc.selectionText}' : '粘贴到光标处',
  131. maxLines: 1,
  132. overflow: TextOverflow.ellipsis,
  133. style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
  134. ),
  135. ),
  136. if (hasSel) ...[
  137. TextButton(onPressed: _copySelection, child: const Text('复制')),
  138. TextButton(onPressed: _cutSelection, child: const Text('剪切')),
  139. TextButton(onPressed: _deleteSelection, child: const Text('删除')),
  140. ] else
  141. TextButton(onPressed: _paste, child: const Text('粘贴')),
  142. IconButton(
  143. tooltip: '关闭',
  144. icon: const Icon(Icons.close, size: 18),
  145. onPressed: _dismissBar,
  146. ),
  147. ],
  148. ),
  149. ),
  150. );
  151. }
  152. Future<void> _runPreview() async {
  153. final expr = _source;
  154. if (expr.isEmpty || expr == 'Null') {
  155. if (mounted) setState(() => _preview = null);
  156. return;
  157. }
  158. final session = context.read<SymjaSession>();
  159. final id = ++_seq;
  160. final r = await session.preview(expr);
  161. if (!mounted || id != _seq) return; // 已有更新的预览在路上
  162. setState(() => _preview = r);
  163. }
  164. Future<void> _commit() async {
  165. final expr = _source;
  166. if (expr.isEmpty || expr == 'Null') return;
  167. _debounce?.cancel();
  168. final session = context.read<SymjaSession>();
  169. final id = ++_seq; // 让在途的预览作废
  170. try {
  171. final r = await session.evaluate(expr);
  172. if (!mounted || id != _seq) return;
  173. setState(() {
  174. _committed = r;
  175. _preview = null;
  176. _error = null;
  177. });
  178. } on SymjaException catch (e) {
  179. if (!mounted || id != _seq) return;
  180. setState(() {
  181. _error = '$e';
  182. _committed = null;
  183. _preview = null;
  184. });
  185. }
  186. await session.refreshVariables();
  187. }
  188. Future<void> _plotCurrent() async {
  189. final expr = _source;
  190. if (expr.isEmpty || expr == 'Null') return;
  191. final session = context.read<SymjaSession>();
  192. final messenger = ScaffoldMessenger.of(context);
  193. try {
  194. final s = await session.sample(expr, from: -6.3, to: 6.3, count: 240);
  195. if (!mounted) return;
  196. await showDialog<void>(
  197. context: context,
  198. builder: (_) => AlertDialog(
  199. title: Text('$expr (${s.ys.length} 点,${s.elapsedMs} ms)'),
  200. content: SizedBox(
  201. width: 360,
  202. height: 420,
  203. child: CurvePlot(series: [
  204. CurveSeries(
  205. label: expr,
  206. color: const Color(0xFF3F51B5),
  207. points: [
  208. for (var i = 0; i < s.ys.length; i++)
  209. Offset(s.xAt(i), s.ys[i]),
  210. ],
  211. ),
  212. ]),
  213. ),
  214. actions: [
  215. TextButton(
  216. onPressed: () => Navigator.pop(context),
  217. child: const Text('关闭'),
  218. ),
  219. ],
  220. ),
  221. );
  222. } on SymjaException catch (e) {
  223. messenger.showSnackBar(SnackBar(content: Text('$e')));
  224. }
  225. }
  226. // ------------------------------------------------------------------ 按键
  227. void _press(_Key k) {
  228. switch (k.act) {
  229. case _Act.char:
  230. if (k.mergeDigit) {
  231. // 数字键并入左边那个数字字面量,`42` 才是一个数而不是两个节点
  232. _doc.insertChar(k.payload);
  233. } else {
  234. _doc.insert(LitNode(k.payload));
  235. }
  236. case _Act.fn:
  237. _doc.makeFunction(k.payload);
  238. case _Act.fnPrefixed:
  239. // 像 D 这种,第一个参数留空给用户填,其余预置好
  240. _doc.makeFunction(k.payload, prefill: k.prefill);
  241. case _Act.bigOp:
  242. // ∫ / Σ / lim:上下限单独成槽,才画得到习惯位置
  243. final p = k.prefill;
  244. _doc.makeBigOp(
  245. k.payload,
  246. variable: p.isNotEmpty ? p[0] : 'x',
  247. lower: p.length > 1 ? p[1] : '',
  248. upper: p.length > 2 ? p[2] : '',
  249. );
  250. case _Act.sqrt:
  251. _doc.makeFunction('Sqrt');
  252. case _Act.group:
  253. _doc.makeGroup();
  254. case _Act.power:
  255. _doc.makePower();
  256. case _Act.frac:
  257. _doc.makeFraction();
  258. case _Act.postfix:
  259. // 阶乘是后缀:把光标左边那个节点收进来当操作数
  260. final operand = _doc.takeLeft();
  261. _doc.insert(PostfixNode(
  262. operand: SeqNode(operand == null ? null : [operand]),
  263. ));
  264. case _Act.comma:
  265. _doc.nextArgument();
  266. case _Act.back:
  267. _doc.backspace();
  268. case _Act.clear:
  269. _doc.clear();
  270. case _Act.commit:
  271. _commit();
  272. return; // 提交不改文档,不用刷新
  273. }
  274. _onDocChanged();
  275. }
  276. Future<void> _showPalette() async {
  277. final picked = await showModalBottomSheet<({String value, bool isFn})>(
  278. context: context,
  279. isScrollControlled: true,
  280. showDragHandle: true,
  281. builder: (_) => const _FunctionPalette(),
  282. );
  283. if (picked == null) return;
  284. if (picked.isFn) {
  285. _doc.makeFunction(picked.value);
  286. } else {
  287. _doc.insert(LitNode(picked.value));
  288. }
  289. _onDocChanged();
  290. }
  291. // ------------------------------------------------------------------ UI
  292. @override
  293. Widget build(BuildContext context) {
  294. final session = context.watch<SymjaSession>();
  295. final bar = _contextBar();
  296. return Scaffold(
  297. appBar: AppBar(
  298. title: const Text('计算器'),
  299. actions: [
  300. // 精确 / 近似 切换。默认精确:`1/3` 就是 `1/3`,这是符号计算的意义所在。
  301. TextButton(
  302. onPressed: () {
  303. session.approximate = !session.approximate;
  304. _onDocChanged(); // 立刻按新模式重算预览
  305. },
  306. child: Text(
  307. session.approximate ? '≈ 近似' : '精确',
  308. style: TextStyle(
  309. fontFamily: 'monospace',
  310. fontSize: 13,
  311. color: Theme.of(context).colorScheme.primary,
  312. ),
  313. ),
  314. ),
  315. IconButton(
  316. tooltip: '画图(x ∈ [-6.3, 6.3])',
  317. icon: const Icon(Icons.show_chart),
  318. onPressed: _plotCurrent,
  319. ),
  320. IconButton(
  321. tooltip: '函数面板',
  322. icon: const Icon(Icons.functions),
  323. onPressed: _showPalette,
  324. ),
  325. ],
  326. ),
  327. body: SafeArea(
  328. child: Column(
  329. children: [
  330. _display(context),
  331. ?bar,
  332. const Divider(height: 1),
  333. Expanded(child: _keypad(context)),
  334. ],
  335. ),
  336. ),
  337. );
  338. }
  339. Widget _display(BuildContext context) {
  340. final scheme = Theme.of(context).colorScheme;
  341. final shown = _committed ?? _preview;
  342. return Container(
  343. width: double.infinity,
  344. constraints: const BoxConstraints(minHeight: 132),
  345. padding: const EdgeInsets.fromLTRB(12, 10, 12, 10),
  346. color: scheme.surfaceContainerLow,
  347. child: Column(
  348. crossAxisAlignment: CrossAxisAlignment.end,
  349. children: [
  350. // 固定高度 + 超高时纵向滚动。
  351. // 不固定的话,表达式一变高(分式、Σ 带上下限)编辑区就撑大,
  352. // 下面的键盘整体往下跳,手指刚瞄好的键就换位置了。
  353. SizedBox(
  354. height: 104,
  355. child: SingleChildScrollView(
  356. scrollDirection: Axis.vertical,
  357. reverse: true, // 内容超高时把底部(光标那侧)留在视野里
  358. child: ExprEditor(
  359. doc: _doc,
  360. onChanged: _onDocChanged,
  361. onCaretTapped: () => setState(() => _pastePrompt = true),
  362. ),
  363. ),
  364. ),
  365. const SizedBox(height: 6),
  366. // 常显。它就是真正送进引擎的那串文本(也是"复制表达式"复制走的内容),
  367. // 空槽会写成 Null、隐含括号也会露出来。
  368. Text(
  369. _source,
  370. textAlign: TextAlign.right,
  371. maxLines: 2,
  372. overflow: TextOverflow.ellipsis,
  373. style: TextStyle(
  374. fontFamily: 'monospace',
  375. fontSize: 11,
  376. color: scheme.outline,
  377. ),
  378. ),
  379. const SizedBox(height: 6),
  380. SizedBox(
  381. height: 52,
  382. width: double.infinity,
  383. child: GestureDetector(
  384. key: const ValueKey('calc-result'),
  385. // 点结果 = 复制结果
  386. onTap: shown == null ? null : () => _copy(shown.text, '结果'),
  387. behavior: HitTestBehavior.opaque,
  388. child: FittedBox(
  389. fit: BoxFit.scaleDown,
  390. alignment: Alignment.centerRight,
  391. child: _resultView(context, shown),
  392. ),
  393. ),
  394. ),
  395. ],
  396. ),
  397. );
  398. }
  399. Widget _resultView(BuildContext context, SymjaResult? shown) {
  400. final scheme = Theme.of(context).colorScheme;
  401. if (_error != null) {
  402. return SelectableText(
  403. _error!,
  404. style: TextStyle(
  405. fontFamily: 'monospace',
  406. fontSize: 13,
  407. color: scheme.error,
  408. ),
  409. );
  410. }
  411. if (shown == null) return const SizedBox.shrink();
  412. // 用 Text 而不是 SelectableText:点结果是"复制",选中文字反而挡住点击
  413. final plain = Text(
  414. shown.text,
  415. style: const TextStyle(fontFamily: 'monospace', fontSize: 22),
  416. );
  417. final tex = shown.tex;
  418. final body = tex == null
  419. ? plain
  420. : Math.tex(
  421. tex,
  422. mathStyle: MathStyle.display,
  423. textStyle: TextStyle(fontSize: 26, color: scheme.onSurface),
  424. onErrorFallback: (_) => plain,
  425. );
  426. return Row(
  427. mainAxisSize: MainAxisSize.min,
  428. children: [
  429. if (_committed != null) ...[
  430. Icon(Icons.check_circle, size: 16, color: scheme.primary),
  431. const SizedBox(width: 6),
  432. Text('${_committed!.elapsedMs} ms',
  433. style: TextStyle(fontSize: 11, color: scheme.outline)),
  434. const SizedBox(width: 10),
  435. ],
  436. body,
  437. ],
  438. );
  439. }
  440. Widget _keypad(BuildContext context) {
  441. return LayoutBuilder(
  442. builder: (context, box) {
  443. final rows = <List<_Key>>[
  444. [
  445. // 不定积分:只给变量,不给上下限
  446. _Key('∫', _Act.bigOp, payload: 'Integrate', prefill: ['x']),
  447. _Key('Σ', _Act.bigOp,
  448. payload: 'Sum', prefill: ['n', '1', 'Infinity']),
  449. _Key('d/dx', _Act.fnPrefixed, payload: 'D', prefill: ['', 'x']),
  450. _Key('lim', _Act.bigOp, payload: 'Limit', prefill: ['x', '0']),
  451. _Key('solve', _Act.fnPrefixed, payload: 'Solve', prefill: ['', 'x']),
  452. ],
  453. [
  454. _Key('sin', _Act.fn, payload: 'Sin'),
  455. _Key('cos', _Act.fn, payload: 'Cos'),
  456. _Key('tan', _Act.fn, payload: 'Tan'),
  457. _Key('ln', _Act.fn, payload: 'Log'),
  458. _Key('√', _Act.sqrt),
  459. ],
  460. [
  461. _Key('7', _Act.char, payload: '7', mergeDigit: true),
  462. _Key('8', _Act.char, payload: '8', mergeDigit: true),
  463. _Key('9', _Act.char, payload: '9', mergeDigit: true),
  464. _Key('(', _Act.group),
  465. _Key('/', _Act.frac),
  466. ],
  467. [
  468. _Key('4', _Act.char, payload: '4', mergeDigit: true),
  469. _Key('5', _Act.char, payload: '5', mergeDigit: true),
  470. _Key('6', _Act.char, payload: '6', mergeDigit: true),
  471. _Key('×', _Act.char, payload: '*'),
  472. _Key('!', _Act.postfix),
  473. ],
  474. [
  475. _Key('1', _Act.char, payload: '1', mergeDigit: true),
  476. _Key('2', _Act.char, payload: '2', mergeDigit: true),
  477. _Key('3', _Act.char, payload: '3', mergeDigit: true),
  478. _Key('+', _Act.char, payload: '+'),
  479. _Key('−', _Act.char, payload: '-'),
  480. ],
  481. [
  482. _Key('0', _Act.char, payload: '0', mergeDigit: true),
  483. _Key('.', _Act.char, payload: '.', mergeDigit: true),
  484. _Key('^', _Act.power),
  485. _Key('x', _Act.char, payload: 'x'),
  486. _Key('n', _Act.char, payload: 'n'),
  487. ],
  488. [
  489. _Key('⌫', _Act.back, tint: true),
  490. _Key('CLR', _Act.clear, tint: true),
  491. _Key(',', _Act.comma),
  492. _Key('ANS', _Act.char, payload: '%', tint: true),
  493. _Key('=', _Act.commit, primary: true),
  494. ],
  495. ];
  496. // 7 行 + 6 个间隙,再扣掉 Padding 上下各 6 ——
  497. // box.maxHeight 是 padding **之前**的高度,漏掉会溢出。
  498. final h = (box.maxHeight - 12 - 6 * 6) / 7;
  499. return Padding(
  500. padding: const EdgeInsets.all(6),
  501. child: Column(
  502. children: [
  503. for (var r = 0; r < rows.length; r++) ...[
  504. if (r > 0) const SizedBox(height: 6),
  505. SizedBox(
  506. height: h,
  507. child: Row(
  508. children: [
  509. for (var c = 0; c < rows[r].length; c++) ...[
  510. if (c > 0) const SizedBox(width: 6),
  511. Expanded(child: _keyButton(rows[r][c])),
  512. ],
  513. ],
  514. ),
  515. ),
  516. ],
  517. ],
  518. ),
  519. );
  520. },
  521. );
  522. }
  523. Widget _keyButton(_Key k) {
  524. final scheme = Theme.of(context).colorScheme;
  525. final bg = k.primary
  526. ? scheme.primary
  527. : k.tint
  528. ? scheme.surfaceContainerHighest
  529. : scheme.surfaceContainerLow;
  530. final fg = k.primary ? scheme.onPrimary : scheme.onSurface;
  531. return Material(
  532. // 加 key 是为了让测试能精确定位按键 —— 编辑器里渲染出的字面量(比如 'x')
  533. // 和键盘上同名的按键都符合 find.text,会撞车。
  534. key: ValueKey('key-${k.label}'),
  535. color: bg,
  536. borderRadius: BorderRadius.circular(10),
  537. child: InkWell(
  538. borderRadius: BorderRadius.circular(10),
  539. onTap: () => _press(k),
  540. child: Center(
  541. child: Text(
  542. k.label,
  543. style: TextStyle(
  544. color: fg,
  545. fontSize: k.label.length > 2 ? 13 : 17,
  546. fontFamily: k.label.length > 2 ? null : 'monospace',
  547. ),
  548. ),
  549. ),
  550. ),
  551. );
  552. }
  553. }
  554. enum _Act {
  555. char,
  556. fn,
  557. fnPrefixed,
  558. bigOp,
  559. sqrt,
  560. group,
  561. power,
  562. frac,
  563. postfix,
  564. comma,
  565. back,
  566. clear,
  567. commit,
  568. }
  569. @immutable
  570. class _Key {
  571. final String label;
  572. final _Act act;
  573. final String payload;
  574. /// fnPrefixed 用:第一个参数留空给用户填,其余的预置。
  575. final List<String> prefill;
  576. /// 数字键:并入左边那个数字字面量,而不是各建一个节点。
  577. final bool mergeDigit;
  578. final bool tint;
  579. final bool primary;
  580. const _Key(
  581. this.label,
  582. this.act, {
  583. this.payload = '',
  584. this.prefill = const [],
  585. this.mergeDigit = false,
  586. this.tint = false,
  587. this.primary = false,
  588. });
  589. }
  590. /// 函数面板:检索 Symja 的两百多个函数名,选中后插入一个函数节点。
  591. class _FunctionPalette extends StatefulWidget {
  592. const _FunctionPalette();
  593. @override
  594. State<_FunctionPalette> createState() => _FunctionPaletteState();
  595. }
  596. class _FunctionPaletteState extends State<_FunctionPalette> {
  597. final _search = TextEditingController();
  598. String _q = '';
  599. @override
  600. void dispose() {
  601. _search.dispose();
  602. super.dispose();
  603. }
  604. @override
  605. Widget build(BuildContext context) {
  606. final q = _q.trim().toLowerCase();
  607. final fns = kSymjaFunctions
  608. .where((f) => q.isEmpty || f.toLowerCase().contains(q))
  609. .toList(growable: false);
  610. return DraggableScrollableSheet(
  611. expand: false,
  612. initialChildSize: 0.75,
  613. maxChildSize: 0.95,
  614. builder: (context, scroll) => Column(
  615. children: [
  616. Padding(
  617. padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
  618. child: TextField(
  619. controller: _search,
  620. autofocus: true,
  621. decoration: InputDecoration(
  622. isDense: true,
  623. hintText: '函数名(共 ${kSymjaFunctions.length} 个)',
  624. prefixIcon: const Icon(Icons.search),
  625. border: const OutlineInputBorder(),
  626. ),
  627. onChanged: (v) => setState(() => _q = v),
  628. ),
  629. ),
  630. // 常量做成可点的 —— 它们从键盘上挪走了,得有个地方能按。
  631. Padding(
  632. padding: const EdgeInsets.symmetric(horizontal: 16),
  633. child: Wrap(
  634. spacing: 8,
  635. runSpacing: 4,
  636. crossAxisAlignment: WrapCrossAlignment.center,
  637. children: [
  638. Text('常量', style: Theme.of(context).textTheme.bodySmall),
  639. for (final c in kSymjaConstants)
  640. ActionChip(
  641. label: Text(
  642. c,
  643. style: const TextStyle(
  644. fontFamily: 'monospace', fontSize: 12),
  645. ),
  646. visualDensity: VisualDensity.compact,
  647. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  648. onPressed: () =>
  649. Navigator.pop(context, (value: c, isFn: false)),
  650. ),
  651. ],
  652. ),
  653. ),
  654. const Divider(height: 16),
  655. Expanded(
  656. child: fns.isEmpty
  657. ? const Center(child: Text('没有匹配的函数'))
  658. : ListView.builder(
  659. controller: scroll,
  660. itemCount: fns.length,
  661. itemBuilder: (_, i) => ListTile(
  662. dense: true,
  663. title: Text(
  664. fns[i],
  665. style: const TextStyle(fontFamily: 'monospace'),
  666. ),
  667. // 一句中文说明,挂名字后面
  668. subtitle: Text(
  669. kSymjaFunctionHelp[fns[i]] ?? '',
  670. style: const TextStyle(fontSize: 12),
  671. ),
  672. onTap: () => Navigator.pop(
  673. context, (value: fns[i], isFn: true)),
  674. ),
  675. ),
  676. ),
  677. ],
  678. ),
  679. );
  680. }
  681. }