1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// Flutter code sample for [Action.Action.overridable].
void main() {
runApp(
const MaterialApp(
home: Scaffold(
body: Center(child: VerificationCodeGenerator()),
),
),
);
}
const CopyTextIntent copyTextIntent = CopyTextIntent._();
class CopyTextIntent extends Intent {
const CopyTextIntent._();
}
class CopyableText extends StatelessWidget {
const CopyableText({super.key, required this.text});
final String text;
void _copy(CopyTextIntent intent) => Clipboard.setData(ClipboardData(text: text));
@override
Widget build(BuildContext context) {
final Action<CopyTextIntent> defaultCopyAction = CallbackAction<CopyTextIntent>(onInvoke: _copy);
return Shortcuts(
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.keyC, control: true): copyTextIntent
},
child: Actions(
actions: <Type, Action<Intent>>{
// The Action is made overridable so the VerificationCodeGenerator
// widget can override how copying is handled.
CopyTextIntent: Action<CopyTextIntent>.overridable(defaultAction: defaultCopyAction, context: context),
},
child: Focus(
autofocus: true,
child: DefaultTextStyle.merge(
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
child: Text(text),
),
),
),
);
}
}
class VerificationCodeGenerator extends StatelessWidget {
const VerificationCodeGenerator({super.key});
void _copy(CopyTextIntent intent) {
debugPrint('Content copied');
Clipboard.setData(const ClipboardData(text: '111222333'));
}
@override
Widget build(BuildContext context) {
return Actions(
actions: <Type, Action<Intent>>{CopyTextIntent: CallbackAction<CopyTextIntent>(onInvoke: _copy)},
child: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Press Ctrl-C to Copy'),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CopyableText(text: '111'),
SizedBox(width: 5),
CopyableText(text: '222'),
SizedBox(width: 5),
CopyableText(text: '333'),
],
),
],
),
);
}
}