简体   繁体   English

如何发现 TextField 编辑被取消/键盘折叠/焦点丢失

[英]How to discover TextField editing being cancelled / keyboard collaped / focus lost

The code below shows 2 text fields.下面的代码显示了 2 个文本字段。 It's a stripped example, just ignore things like TextEditingController not being disposed.这是一个剥离的例子,只需忽略TextEditingController之类的东西没有被处理。

If user edits one of the text fields, and presses "Done" on the keyboard, new value is sent to the cubit (saved) and all is good.如果用户编辑其中一个文本字段,并在键盘上按“完成”,则会将新值发送到肘位(已保存),一切都很好。

However, when - during editing - user clicks on another TextField, or collapses the keyboard, he's left with a very inconsistent state.但是,在编辑过程中,当用户单击另一个文本字段或折叠键盘时,他会留下非常不一致的 state。 TextField - the one he was editing - shows value, which wasn't saved, and he's not aware of it. TextField - 他正在编辑的那个 - 显示了没有保存的值,他没有意识到这一点。

So how can I either save the value, or restore it back to the original, or do whatever when user just didn't confirm changes and stopped editing?那么如何保存该值,或将其恢复为原始值,或者在用户没有确认更改并停止编辑时执行任何操作?

I tried messing with text controllers, forms, TextField and TextFormField - no luck.我尝试弄乱文本控制器、forms、 TextFieldTextFormField - 不走运。 I don't want a Save button, and it seems the onSubmitted is there for some reason, but I miss onCancelled or sth.我不想要一个保存按钮,似乎onSubmitted出于某种原因在那里,但我想念onCancelled或某事。

testpage.dart : testpage.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'some_cubit.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Test')),
      body: BlocProvider(
        create: (context) => SomeCubit(),
        child: BlocBuilder<SomeCubit, SomeState>(
          builder: (context, state) {
            return Column(
              children: [
                Flexible(
                  child: TextField(
                    controller: TextEditingController(text: state.string1),
                    onSubmitted: (s) {
                      BlocProvider.of<SomeCubit>(context).updateString1(s);
                    },
                  ),
                ),
                Flexible(
                  child: TextField(
                    controller: TextEditingController(text: state.string2),
                    onSubmitted: (s) {
                      BlocProvider.of<SomeCubit>(context).updateString2(s);
                    },
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

some_cubit.dart

import 'package:bloc/bloc.dart';

class SomeCubit extends Cubit<SomeState> {
  SomeCubit() : super(SomeState("abc", "123"));

  void updateString1(String value) => emit(SomeState(value, state.string2));
  void updateString2(String value) => emit(SomeState(state.string1, value));
}

class SomeState {
  final String string1;
  final String string2;

  SomeState(this.string1, this.string2);
}

textField has onChanged function that it fires each time the user adds a character textField 具有onChanged function ,每次用户添加字符时都会触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM