11import 'dart:async' ;
22
3+ import 'package:arche/arche.dart' ;
4+ import 'package:arche/extensions/dialogs.dart' ;
5+ import 'package:cczu_helper/models/fields.dart' ;
36import 'package:cczu_helper/src/bindings/signals/signals.dart' ;
47import 'package:flutter/material.dart' ;
58
@@ -21,25 +24,42 @@ class TermPicker extends StatefulWidget {
2124class _TermPickerState extends State <TermPicker > {
2225 final termPopMenuKey = GlobalKey <PopupMenuButtonState >();
2326 List <String >? terms;
27+ late final StreamSubscription listener;
2428 String ? term;
2529 @override
2630 void initState () {
2731 super .initState ();
2832 if (widget.ensureAwake) {
29- late final StreamSubscription listener;
3033 listener = WeChatTermsOutput .rustSignalStream.listen ((data) {
34+ if (data.message.terms.isEmpty) {
35+ if (viewKey.currentContext != null ) {
36+ ComplexDialog .instance.text (
37+ title: Text ("Error" ),
38+ context: viewKey.currentContext,
39+ content: Text (
40+ "Failed to get terms: ${data .message .error }, please check connection and reopen" ));
41+ }
42+ return ;
43+ }
44+ termPopMenuKey.currentState? .showButtonMenu ();
45+
3146 widget.onChanged (data.message.terms.first);
3247 setState (() {
3348 terms = data.message.terms;
3449 term = data.message.terms.first;
3550 });
36- listener.cancel ();
3751 });
3852
3953 WeChatTermsInput ().sendSignalToRust ();
4054 }
4155 }
4256
57+ @override
58+ void dispose () {
59+ super .dispose ();
60+ listener.cancel ();
61+ }
62+
4363 @override
4464 Widget build (BuildContext context) {
4565 return PopupMenuButton (
@@ -48,15 +68,19 @@ class _TermPickerState extends State<TermPicker> {
4868 term = value;
4969 widget.onChanged (value);
5070 }),
51- itemBuilder: (context) {
52- if (terms == null || terms! .isEmpty) {
53- WeChatTermsOutput .rustSignalStream.listen ((data) {
54- terms = (data.message.terms);
55- termPopMenuKey.currentState? .showButtonMenu ();
56- });
57-
71+ onOpened: () {
72+ if (terms? .isEmpty ?? true ) {
5873 WeChatTermsInput ().sendSignalToRust ();
59- terms = [];
74+ }
75+ },
76+ itemBuilder: (context) {
77+ if (terms == null ) {
78+ return [
79+ PopupMenuItem (
80+ enabled: false ,
81+ child: CircularProgressIndicator (),
82+ )
83+ ];
6084 }
6185
6286 return terms!
0 commit comments