gpt4 book ai didi

Block undo keyboard commands of Flutter's TextField for custom implementation(用于自定义实现的Ffltter的Textfield的阻止撤消键盘命令)

转载 作者:bug小助手 更新时间:2023-10-25 23:15:01 28 4
gpt4 key购买 nike



I'm developing an app with Flutter that includes TextField widgets for text input. My aim is to handle undo and redo manually with a custom implementation, bypassing Flutter's built-in features.

我正在开发一款应用程序,其中包括用于文本输入的TextFieldWidget。我的目标是绕过Ffltter的内置功能,通过定制实现手动处理撤消和重做。


Here's my issue: even though I've tried wrapping the TextField with widgets like RawKeyboardListener to capture keyboard events, it seems like TextField still manages undo and redo internally. Is there another way to block these and do a custom implementation?

我的问题是:尽管我已经尝试用RawKeyboardListener之类的小部件包装Textfield来捕获键盘事件,但Textfield似乎仍然在内部管理撤消和重做。有没有其他方法来阻止这些并执行定制实现?


更多回答
优秀答案推荐

Updated: I've added more possibilities to the code. I've created two functions for preventing such actions.
1st one is for the undo action & 2nd one is for the redo action.

更新:我在代码中添加了更多可能性。我已经创建了两个函数来防止此类操作。第一个用于撤消操作,第二个用于重做操作。


What we're doing is that whenever the user performs an undo or a redo operation, we're blocking that operation from happening.

我们所做的是,每当用户执行撤消或重做操作时,我们都会阻止该操作发生。


We've created our function over there. So, whenever the user performs an undo operation at that time, the preventPerformingUndo() function will call & whenever the user performs a redo operation at that time, the preventPerformingRedo() function will call.

我们已经在那里创建了我们的函数。因此,每当用户执行撤消操作时,就会调用brementPerformingUndo()函数&每当用户执行重做操作时,就会调用brumentPerformingRedo()函数。


Source code:

源代码:


import "package:flutter/material.dart";
import "package:flutter/services.dart";

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(home: MyWidget());
}
}

class MyWidget extends StatefulWidget {
const MyWidget({super.key});

@override
State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
final TextEditingController textEditingController = TextEditingController();

@override
void dispose() {
textEditingController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent value) {
// For Control & Command Key
final bool isControlPressed = value.isControlPressed;
final bool isMetaPressed = value.isMetaPressed;
final bool isControlOrMeta = isControlPressed || isMetaPressed;

// For Z Key
final bool isLZ = value.logicalKey == LogicalKeyboardKey.keyZ;
final bool isPZ = value.physicalKey == PhysicalKeyboardKey.keyZ;
final bool isLogicalOrPhysicalZ = isLZ || isPZ;

// For Y Key
final bool isLY = value.logicalKey == LogicalKeyboardKey.keyY;
final bool isPY = value.physicalKey == PhysicalKeyboardKey.keyY;
final bool isLogicalOrPhysicalY = isLY || isPY;

if (isControlOrMeta && isLogicalOrPhysicalZ) {
preventPerformingUndo();
} else {}

if (isControlOrMeta && isLogicalOrPhysicalY) {
preventPerformingRedo();
} else {}
},
child: TextField(
controller: textEditingController,
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: preventPerformingUndo,
child: const Text("Perform Undo"),
),
ElevatedButton(
onPressed: preventPerformingRedo,
child: const Text("Perform Redo"),
),
],
),
],
),
),
);
}

void preventPerformingUndo() {
// What to do if user pressed Ctrl + Z
return;
}

void preventPerformingRedo() {
// What to do if user pressed Ctrl + Y
return;
}
}


You can pass an UndoHistoryController to TextField. UndoHistoryController is a ValueNotifier that notifies attached listeners of changes in UndoHistoryValue.

您可以将UndoHistory oryController传递给Textfield。UndoHistory oryController是一个ValueNotiator,它通知附加的侦听器UndoHistoryValue中的更改。


create an instance of UndoHistoryController.

创建一个UndoHistoryController的实例。


 final UndoHistoryController undoController = UndoHistoryController();

Pass it to a TextField, listen to that instance using a ValueListenableBuilder and return a row on buttons in the builder to perform undo/redo operations.

将其传递给Textfield,使用ValueListenableBuilder侦听该实例,并在构建器中的按钮上返回一行以执行撤消/重做操作。


Here are example for better understanding

为了更好地理解,这里有一些例子


Widget build(BuildContext context) {

return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: controller,
focusNode: focusNode,
undoController: undoController,
),
ValueListenableBuilder<UndoHistoryValue>(
valueListenable: undoController,
builder: (BuildContext context, UndoHistoryValue value,
Widget? child) {
return Row(
children: <Widget>[
TextButton(
child: Text('Undo'),
onPressed: () {
undoController.undo();
},
),
TextButton(
child: Text('Redo'),
onPressed: () {
undoController.redo();
},
),
],
);
},
),
],
),
);
}

Clicking the Undo and Redo buttons will remove and reapply changes from the TextField value respectively

单击撤消和重做按钮将分别从TextField值中移除和重新应用更改


更多回答

Same question I put under the above response, but respectfully, how does this answer my question? I’m not asking how to do an undo/redo, I’m asking to stop flutter’s implementation of it from executing.

我在上面的回答中提出了同样的问题,但恕我直言,这如何回答我的问题?我不是在问如何撤消/重做,我是在问停止Ffltter执行它的实现。

Sorry for the inconvenience & misunderstanding. I've updated my answer. Kindly check it out.

对于给您带来的不便和误解,深表歉意。我已经更新了我的答案。请查收。

I tried the code and still the default behavior of Ctrl + Z happens.

我尝试了该代码,但仍然会出现Ctrl+Z的默认行为。

I updated my answer & I've added more possibilities to the code. Kindly test it out.

我更新了我的答案,并在代码中添加了更多的可能性。请测试一下。

This still doesn't perform in the expected way, clearly showing you did not test it. While I can't be 100% certain, this looks nearly Identical to what would be generated by ChatGPT or another LLM (I would know, I tried before asking for human help)

这仍然没有以预期的方式执行,这清楚地表明您没有对其进行测试。虽然我不能100%确定,但这看起来与ChatGPT或其他LLM生成的结果几乎相同(我知道,我在请求人类帮助之前尝试过)

Respectfully, how does this answer my question? I’m not asking how to do an undo/redo, I’m asking to stop flutter’s implementation of it from executing.

恕我直言,这是如何回答我的问题的?我不是在问如何撤消/重做,我是在问停止Ffltter执行它的实现。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com