File tree Expand file tree Collapse file tree 2 files changed +49
-5
lines changed
frontend/appflowy_flutter/lib
workspace/presentation/settings/widgets Expand file tree Collapse file tree 2 files changed +49
-5
lines changed Original file line number Diff line number Diff line change 1+ import 'dart:async' ;
2+
3+ import 'package:flutter/material.dart' ;
4+
5+ class Debounce {
6+ final Duration duration;
7+ Timer ? _timer;
8+
9+ Debounce ({
10+ this .duration = const Duration (milliseconds: 1000 ),
11+ });
12+
13+ void call (VoidCallback action) {
14+ dispose ();
15+ _timer = Timer (duration, () {
16+ action ();
17+ });
18+ }
19+
20+ void dispose () {
21+ _timer? .cancel ();
22+ _timer = null ;
23+ }
24+ }
Original file line number Diff line number Diff line change 11import 'package:appflowy/startup/startup.dart' ;
2+ import 'package:appflowy/util/debounce.dart' ;
3+ import 'package:appflowy_backend/log.dart' ;
24import 'package:flowy_infra/size.dart' ;
35import 'package:flowy_infra_ui/style_widget/text.dart' ;
46import 'package:flutter/material.dart' ;
@@ -98,11 +100,20 @@ class _OpenaiKeyInput extends StatefulWidget {
98100
99101class _OpenaiKeyInputState extends State <_OpenaiKeyInput > {
100102 bool visible = false ;
103+ final textEditingController = TextEditingController ();
104+ final debounce = Debounce ();
105+
106+ @override
107+ void initState () {
108+ super .initState ();
109+
110+ textEditingController.text = widget.openAIKey;
111+ }
101112
102113 @override
103114 Widget build (BuildContext context) {
104115 return TextField (
105- controller: TextEditingController ()..text = widget.openAIKey ,
116+ controller: textEditingController ,
106117 obscureText: ! visible,
107118 decoration: InputDecoration (
108119 labelText: 'OpenAI Key' ,
@@ -120,13 +131,22 @@ class _OpenaiKeyInputState extends State<_OpenaiKeyInput> {
120131 },
121132 ),
122133 ),
123- onSubmitted: (val) {
124- context
125- .read <SettingsUserViewBloc >()
126- .add (SettingsUserEvent .updateUserOpenAIKey (val));
134+ onChanged: (value) {
135+ debounce.call (() {
136+ Log .debug ('SettingsUserViewBloc' );
137+ context
138+ .read <SettingsUserViewBloc >()
139+ .add (SettingsUserEvent .updateUserOpenAIKey (value));
140+ });
127141 },
128142 );
129143 }
144+
145+ @override
146+ void dispose () {
147+ debounce.dispose ();
148+ super .dispose ();
149+ }
130150}
131151
132152class _CurrentIcon extends StatelessWidget {
You can’t perform that action at this time.
0 commit comments