如何在Flutter上创建复制到剪贴板事件?

问题描述 投票:0回答:2

目前我想在用户设备上为“复制到剪贴板”制作事件。

当用户单击“列出视图前导icon.content副本”时,文本应存储在他的设备剪贴板上。

请有人帮帮我吗?

Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
    final record = Record.fromSnapshot(data);

    return Padding(
      key: ValueKey(record.name),
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey),
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: ListTile(
          leading: Icon(Icons.content_copy),
          title: Text(record.group),
          subtitle: Text(record.name),
          // can anyone help me how to create event on onTap action.
          // When user click then text copy to clipboard on his device. 
             onTap: () {
             debugPrint ("Tapped");
          },
          ),
      ),
    );
  }
}

class Record {
  final String name;
  final String group;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['group'] != null),
        name = map['name'],
        group = map['group'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$group>";
}
flutter flutter-layout flutter-dependencies flutter-test flutter-sliver
2个回答
3
投票
import 'package:flutter/services.dart';

在你的onTap里面添加以下内容:

onTap:(){
   Clipboard.setData(new ClipboardData(text: record.name));
   Scaffold.of(context).showSnackBar(SnackBar
     (content: Text('text copied')));
}

-1
投票

如果Sami Kanafani的答案因为小吃店而引发异常,请试试这个对我有用的解决方案。

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

...//build method
Scaffold(
key: _scaffoldKey,
appBar: _appBar,
body: _content,
floatingActionButton: FloatingActionButton.extended(
label: Text('copy'),
onPressed:(){
  Clipboard.setData(new ClipboardData(text: record.name));
   _scaffoldKey.currentState.showSnackBar(SnackBar
     (content: Text('text copied')));
}
)
© www.soinside.com 2019 - 2024. All rights reserved.