4

im trying to use an Editable text widget in Flutter and i cant select, copy, cut and paste don't work with it, i don't know why this is happening I've enabled everything that is related to selecting and copy pasting, but it still doesn't work

    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,

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

i hope this code is enough, please let me know if you want me to provide more code or if you have any questions, Thank you!

imcouri
  • 124
  • 4
  • 9

1 Answers1

2

Try Selectable Text instead of simple text widget. more info here.

You can use TextField instead of Editable Text, this code works now.

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,
              ),
            )
          ],
        )),
      ),
    );
  }
}

As per this answer, EditableText is rarely used.

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35