How to make copyable Text Widget in Flutter?

前端 未结 5 1203
闹比i
闹比i 2020-12-04 16:54

When long tab on Text widget, a tooltip show up with \'copy\'. When click on the \'copy\' the text content should copy to system clipboard.

The following will copy t

相关标签:
5条回答
  • 2020-12-04 17:07
    child: RaisedButton(
        onPressed: (){
            mensagemAlertaFlushBar(context,"Ótimo!" ,"Linha digitável copiada com sucesso!");
            Clipboard.setData(new ClipboardData(text: element['linhaDigitavel'].toString()));
    },
        disabledColor: Colors.blue[400],
        child: Text("Copiar linha digitável", style: TextStyle(color: Colors.white),),
    ),
    
    ...
    
    mensagemAlertaFlushBar(BuildContext context, String status, String texto) {
      return Flushbar(
        title: status,
        message:
            texto,
        duration: Duration(seconds: 4),
      )..show(context);
    }
    
    0 讨论(0)
  • 2020-12-04 17:10
    SelectableText(
      "Copy me",
      onTap: () {
        // you can show toast to the user, like "Copied"
      },
    )
    

    If you want to have different styling for text, use

    SelectableText.rich(
      TextSpan(
        children: [
          TextSpan(text: "Copy me", style: TextStyle(color: Colors.red)),
          TextSpan(text: " and leave me"),
        ],
      ),
    )
    

    0 讨论(0)
  • 2020-12-04 17:13

    There is also list of properties it in SelectableText to enable option copy, paste, selectAll, cut

            child: Center(
                child: SelectableText('Hello Flutter Developer',
                    cursorColor: Colors.red,
                    showCursor: true,
                    toolbarOptions: ToolbarOptions(
                    copy: true,
                    selectAll: true,
                    cut: false,
                    paste: false
                    ),
                    style: Theme.of(context).textTheme.body2)
                ),
    

    SelectableText widget

            const SelectableText(
                this.data, {
                Key key,
                this.focusNode,
                this.style,
                this.strutStyle,
                this.textAlign,
                this.textDirection,
                this.showCursor = false,
                this.autofocus = false,
                ToolbarOptions toolbarOptions,
                this.maxLines,
                this.cursorWidth = 2.0,
                this.cursorRadius,
                this.cursorColor,
                this.dragStartBehavior = DragStartBehavior.start,
                this.enableInteractiveSelection = true,
                this.onTap,
                this.scrollPhysics,
                this.textWidthBasis,
            })
    

    0 讨论(0)
  • 2020-12-04 17:16

    You can use a SnackBar to notify the user about the copy.

    Here is a relevant code:

    String _copy = "Copy Me";
    
      @override
      Widget build(BuildContext context) {
        final key = new GlobalKey<ScaffoldState>();
        return new Scaffold(
          key: key,
          appBar: new AppBar(
            title: new Text("Copy"),
            centerTitle: true,
          ),
          body:
          new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new GestureDetector(
                  child: new Text(_copy),
                  onLongPress: () {
                    Clipboard.setData(new ClipboardData(text: _copy));
                    key.currentState.showSnackBar(
                        new SnackBar(content: new Text("Copied to Clipboard"),));
                  },
                ),
                new TextField(
                    decoration: new InputDecoration(hintText: "Paste Here")),
              ]),
    
    
        );
      }
    

    EDIT

    I was working on something and I did the followin, so I thought of revisiting this answer:

    import "package:flutter/material.dart";
    import 'package:flutter/services.dart';
    
    void main() {
      runApp(new MaterialApp(home: new MyApp(),
      ));
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
      String _copy = "Copy Me";
    
      @override
      Widget build(BuildContext context) {
        final key = new GlobalKey<ScaffoldState>();
        return new Scaffold(
          key: key,
          appBar: new AppBar(
            title: new Text("Copy"),
            centerTitle: true,
          ),
          body:
          new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new GestureDetector(
                  child: new CustomToolTip(text: "My Copyable Text"),
                  onTap: () {
    
                  },
                ),
                new TextField(
                    decoration: new InputDecoration(hintText: "Paste Here")),
              ]),
    
    
        );
      }
    }
    
    class CustomToolTip extends StatelessWidget {
    
      String text;
    
      CustomToolTip({this.text});
    
      @override
      Widget build(BuildContext context) {
        return new GestureDetector(
          child: new Tooltip(preferBelow: false,
              message: "Copy", child: new Text(text)),
          onTap: () {
            Clipboard.setData(new ClipboardData(text: text));
          },
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-04 17:29

    Since Flutter 1.9 you can use

    SelectableText("Lorem ipsum...")
    

    When text is selected the "Copy" context button will appear.

    0 讨论(0)
提交回复
热议问题