1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/material.dart'; |
6 | import 'package:flutter/services.dart'; |
7 | |
8 | /// Flutter code sample for [SingleActivator]. |
9 | |
10 | void main() => runApp(const SingleActivatorExampleApp()); |
11 | |
12 | class SingleActivatorExampleApp extends StatelessWidget { |
13 | const SingleActivatorExampleApp({super.key}); |
14 | |
15 | @override |
16 | Widget build(BuildContext context) { |
17 | return MaterialApp( |
18 | home: Scaffold( |
19 | appBar: AppBar(title: const Text('SingleActivator Sample' )), |
20 | body: const Center(child: SingleActivatorExample()), |
21 | ), |
22 | ); |
23 | } |
24 | } |
25 | |
26 | class IncrementIntent extends Intent { |
27 | const IncrementIntent(); |
28 | } |
29 | |
30 | class SingleActivatorExample extends StatefulWidget { |
31 | const SingleActivatorExample({super.key}); |
32 | |
33 | @override |
34 | State<SingleActivatorExample> createState() => _SingleActivatorExampleState(); |
35 | } |
36 | |
37 | class _SingleActivatorExampleState extends State<SingleActivatorExample> { |
38 | int count = 0; |
39 | |
40 | @override |
41 | Widget build(BuildContext context) { |
42 | return Shortcuts( |
43 | shortcuts: const <ShortcutActivator, Intent>{ |
44 | SingleActivator(LogicalKeyboardKey.keyC, control: true): IncrementIntent(), |
45 | }, |
46 | child: Actions( |
47 | actions: <Type, Action<Intent>>{ |
48 | IncrementIntent: CallbackAction<IncrementIntent>( |
49 | onInvoke: (IncrementIntent intent) => setState(() { |
50 | count = count + 1; |
51 | }), |
52 | ), |
53 | }, |
54 | child: Focus( |
55 | autofocus: true, |
56 | child: Column( |
57 | children: <Widget>[ |
58 | const Text('Add to the counter by pressing Ctrl+C' ), |
59 | Text('count: $count' ), |
60 | ], |
61 | ), |
62 | ), |
63 | ), |
64 | ); |
65 | } |
66 | } |
67 | |