character_activator.0.dart 1.82 KB
Newer Older
1 2 3 4 5 6
// 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';

7
/// Flutter code sample for [CharacterActivator].
8

9
void main() => runApp(const CharacterActivatorExampleApp());
10

11 12
class CharacterActivatorExampleApp extends StatelessWidget {
  const CharacterActivatorExampleApp({super.key});
13 14 15 16 17

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
18
        appBar: AppBar(title: const Text('CharacterActivator Sample')),
19
        body: const Center(
20
          child: CharacterActivatorExample(),
21 22 23 24 25 26 27 28 29 30
        ),
      ),
    );
  }
}

class HelpMenuIntent extends Intent {
  const HelpMenuIntent();
}

31 32
class CharacterActivatorExample extends StatefulWidget {
  const CharacterActivatorExample({super.key});
33 34

  @override
35
  State<CharacterActivatorExample> createState() => _CharacterActivatorExampleState();
36 37
}

38
class _CharacterActivatorExampleState extends State<CharacterActivatorExample> {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  @override
  Widget build(BuildContext context) {
    return Shortcuts(
      shortcuts: const <ShortcutActivator, Intent>{
        CharacterActivator('?'): HelpMenuIntent(),
      },
      child: Actions(
        actions: <Type, Action<Intent>>{
          HelpMenuIntent: CallbackAction<HelpMenuIntent>(
            onInvoke: (HelpMenuIntent intent) {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('Keep calm and carry on!')),
              );
              return null;
            },
          ),
        },
56
        child: const Focus(
57 58
          autofocus: true,
          child: Column(
59
            children: <Widget>[
60 61 62 63 64 65 66 67
              Text('Press question mark for help'),
            ],
          ),
        ),
      ),
    );
  }
}