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 [LogicalKeySet]. |
9 | |
10 | void main() => runApp(const LogicalKeySetExampleApp()); |
11 | |
12 | class LogicalKeySetExampleApp extends StatelessWidget { |
13 | const LogicalKeySetExampleApp({super.key}); |
14 | |
15 | @override |
16 | Widget build(BuildContext context) { |
17 | return MaterialApp( |
18 | home: Scaffold( |
19 | appBar: AppBar(title: const Text('LogicalKeySet Sample')), |
20 | body: const Center(child: LogicalKeySetExample()), |
21 | ), |
22 | ); |
23 | } |
24 | } |
25 | |
26 | class IncrementIntent extends Intent { |
27 | const IncrementIntent(); |
28 | } |
29 | |
30 | class LogicalKeySetExample extends StatefulWidget { |
31 | const LogicalKeySetExample({super.key}); |
32 | |
33 | @override |
34 | State<LogicalKeySetExample> createState() => _LogicalKeySetExampleState(); |
35 | } |
36 | |
37 | class _LogicalKeySetExampleState extends State<LogicalKeySetExample> { |
38 | int count = 0; |
39 | |
40 | @override |
41 | Widget build(BuildContext context) { |
42 | return Shortcuts( |
43 | shortcuts: <ShortcutActivator, Intent>{ |
44 | LogicalKeySet(LogicalKeyboardKey.keyC, LogicalKeyboardKey.controlLeft): |
45 | const IncrementIntent(), |
46 | }, |
47 | child: Actions( |
48 | actions: <Type, Action<Intent>>{ |
49 | IncrementIntent: CallbackAction<IncrementIntent>( |
50 | onInvoke: |
51 | (IncrementIntent intent) => setState(() { |
52 | count = count + 1; |
53 | }), |
54 | ), |
55 | }, |
56 | child: Focus( |
57 | autofocus: true, |
58 | child: Column( |
59 | children: <Widget>[ |
60 | const Text('Add to the counter by pressing Ctrl+C'), |
61 | Text('count:$count '), |
62 | ], |
63 | ), |
64 | ), |
65 | ), |
66 | ); |
67 | } |
68 | } |
69 |