如何解开Texfield并将Keybaord隐藏在糊状扑来中

发布于 2025-02-09 13:03:39 字数 1705 浏览 1 评论 0原文

我有一个我的文本字段,主要是粘贴内容,所以我想解开textfield并在糊状上隐藏键盘,因此我有能力使用Textfield SelectionControls处理粘贴,但问题是Focusing和Keybaord,而Keybaord正在重新开放我疲倦的所有焦点方法来解散关注的方法这是我的代码

import 'package:flutter/material.dart';

main() => runApp(const App());

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

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: Center(
            child: TextField(
              selectionControls: MySelectionControls(
                onPaste: () {
                  print('onPaste');

                  // FocusManager.instance.primaryFocus?.unfocus();

                  // Focus.of(context).unfocus();

                  // FocusScope.of(context).unfocus();

                  // FocusScope.of(context).requestFocus(FocusNode());

                  // FocusScopeNode currentFocus = FocusScope.of(context);
                  // if (!currentFocus.hasPrimaryFocus) {
                  //   currentFocus.focusedChild?.unfocus();
                  // }
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class MySelectionControls extends MaterialTextSelectionControls {
  final Function onPaste;

  MySelectionControls({required this.onPaste});

  @override
  Future<void> handlePaste(TextSelectionDelegate delegate) {
    onPaste();
    return super.handlePaste(delegate);
  }
}

I have a textfield on which i mostly paste content so i want to unfocus textfield and hide keybaord on paste so i have achive to handle on paste using textfield selectionControls but the problem is focusing and keybaord which is reopening i have tired all focus methods to unfocus here is my code

import 'package:flutter/material.dart';

main() => runApp(const App());

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

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: Center(
            child: TextField(
              selectionControls: MySelectionControls(
                onPaste: () {
                  print('onPaste');

                  // FocusManager.instance.primaryFocus?.unfocus();

                  // Focus.of(context).unfocus();

                  // FocusScope.of(context).unfocus();

                  // FocusScope.of(context).requestFocus(FocusNode());

                  // FocusScopeNode currentFocus = FocusScope.of(context);
                  // if (!currentFocus.hasPrimaryFocus) {
                  //   currentFocus.focusedChild?.unfocus();
                  // }
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class MySelectionControls extends MaterialTextSelectionControls {
  final Function onPaste;

  MySelectionControls({required this.onPaste});

  @override
  Future<void> handlePaste(TextSelectionDelegate delegate) {
    onPaste();
    return super.handlePaste(delegate);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2025-02-16 13:03:39

尝试这个


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextSelectionControls? _textSelectionControls;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _textSelectionControls = MySelectionControls(onPaste: onPaste);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: Center(
            child: Column(
              children: [
                TextField(
                  selectionControls: _textSelectionControls,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future<void> onPaste(final TextSelectionDelegate? delegate) async {
    Future.delayed(Duration(milliseconds: 100), () {
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }
}

class MySelectionControls extends MaterialTextSelectionControls {
  MySelectionControls({required this.onPaste});
  ValueChanged<TextSelectionDelegate> onPaste;
  @override
  Future<void> handlePaste(TextSelectionDelegate delegate) async {
    onPaste(delegate);
    return super.handlePaste(delegate);
  }
}

我已经测试过的及其工作

Try this one


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextSelectionControls? _textSelectionControls;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _textSelectionControls = MySelectionControls(onPaste: onPaste);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: Center(
            child: Column(
              children: [
                TextField(
                  selectionControls: _textSelectionControls,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future<void> onPaste(final TextSelectionDelegate? delegate) async {
    Future.delayed(Duration(milliseconds: 100), () {
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }
}

class MySelectionControls extends MaterialTextSelectionControls {
  MySelectionControls({required this.onPaste});
  ValueChanged<TextSelectionDelegate> onPaste;
  @override
  Future<void> handlePaste(TextSelectionDelegate delegate) async {
    onPaste(delegate);
    return super.handlePaste(delegate);
  }
}

I have tested this and its working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文