【问题标题】:Flutter cannot select, copy or paste in EditableTextFlutter 无法在 EditableText 中选择、复制或粘贴
【发布时间】:2020-12-09 13:53:37
【问题描述】:

我试图在 Flutter 中使用可编辑文本小部件,但我无法选择、复制、剪切和粘贴,但无法使用它,我不知道为什么会发生这种情况我已启用与选择和复制粘贴,还是不行

    class EditProfile extends StatefulWidget {
  @override
  _EditProfileState createState() => _EditProfileState();
}

class _EditProfileState extends State<EditProfile> {
  var _controller = TextEditingController();
  TextSelectionControls _mainContentSelectionController;
  @override


  Widget _backButton() {
    return InkWell(
      onTap: () {
        Navigator.pop(context);
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: Row(
          children: <Widget>[
            Icon(
              Icons.clear,
              size: 30.0,
              color: Color(0xff8729e6),
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                color: Colors.white,
                child: EditableText(
                  controller: _controller,
                  toolbarOptions: ToolbarOptions(
                    paste: true,
                    copy: true,
                    selectAll: true,
                  ),
                  readOnly : false,
                  focusNode:  FocusNode(),
                  cursorColor: Colors.blue,
                  style: TextStyle(
                      color: Colors.black
                  ),
                  keyboardType: TextInputType.text,
                  autocorrect: false,
                  autofocus: false,
                  backgroundCursorColor: Colors.red,
                  maxLines: 10,
                  minLines: 1,
                  enableInteractiveSelection: true,
                  selectionColor: Colors.red,
                  selectionControls: _mainContentSelectionController,

                ),
              )
            ],
          )
        ),
      ),
    );
  }
}

我希望这段代码就足够了,如果您希望我提供更多代码或者如果您有任何问题,请告诉我,谢谢!

【问题讨论】:

  • 你为什么不用TextField
  • @sleepingkit 我试图在文本字段中突出显示@、# 和 URL,但我无法在普通文本字段中做到这一点

标签: flutter dart


【解决方案1】:

尝试可选文本而不是简单的文本小部件。更多信息here.

您可以使用 TextField 代替 Editable Text,此代码现在可以使用。

import 'package:flutter/material.dart';

class EditProfile extends StatefulWidget {
  @override
  _EditProfileState createState() => _EditProfileState();
}

class _EditProfileState extends State<EditProfile> {
  var _controller = TextEditingController();
  TextSelectionControls _mainContentSelectionController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: MediaQuery.of(context).size.height * 0.1,
              color: Colors.white,
              child: TextField(
                controller: _controller,
                toolbarOptions: ToolbarOptions(
                  paste: true,
                  cut: true,
                  copy: true,
                  selectAll: true,
                ),
                readOnly: false,
                focusNode: FocusNode(),
                cursorColor: Colors.blue,
                style: TextStyle(color: Colors.black),
                keyboardType: TextInputType.text,
                autocorrect: false,
                autofocus: false,
                maxLines: 10,
                minLines: 1,
                enableInteractiveSelection: true,
              ),
            )
          ],
        )),
      ),
    );
  }
}

根据this 的回答,EditableText 很少使用。

【讨论】:

  • 您的代码似乎是正确的,您面临什么困难?尝试热重启一次。
  • 它可以在您的设备上使用吗?我已经尝试了所有方法,但无法复制/粘贴并选择 Editabletext 中的文本
  • 您的可编辑文本上是否有文本??表示它们在您键入时会发生变化吗?
  • 是的,它就像一个普通的文本字段,但我无法选择里面的文本
  • 你能发布你的整个代码吗?页面和所有内容?
猜你喜欢
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2017-07-15
相关资源
最近更新 更多