Add checkbox and radio menu buttons (#112821)

This commit is contained in:
Greg Spencer
2022-10-07 14:13:36 -07:00
committed by GitHub
parent 91b5079f8e
commit 6b32c0691b
10 changed files with 995 additions and 48 deletions

View File

@@ -0,0 +1,108 @@
// 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.
/// Flutter code sample for [CheckboxMenuButton].
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MenuApp());
class MyCheckboxMenu extends StatefulWidget {
const MyCheckboxMenu({super.key, required this.message});
final String message;
@override
State<MyCheckboxMenu> createState() => _MyCheckboxMenuState();
}
class _MyCheckboxMenuState extends State<MyCheckboxMenu> {
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
static const SingleActivator _showShortcut = SingleActivator(LogicalKeyboardKey.keyS, control: true);
bool _showingMessage = false;
@override
void dispose() {
_buttonFocusNode.dispose();
super.dispose();
}
void _setMessageVisibility(bool visible) {
setState(() {
_showingMessage = visible;
});
}
@override
Widget build(BuildContext context) {
return CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
_showShortcut: () {
_setMessageVisibility(!_showingMessage);
},
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MenuAnchor(
childFocusNode: _buttonFocusNode,
menuChildren: <Widget>[
CheckboxMenuButton(
value: _showingMessage,
onChanged: (bool? value) {
_setMessageVisibility(value!);
},
child: const Text('Show Message'),
),
],
builder: (BuildContext context, MenuController controller, Widget? child) {
return TextButton(
focusNode: _buttonFocusNode,
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('OPEN MENU'),
);
},
),
Expanded(
child: Container(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
_showingMessage ? widget.message : '',
style: Theme.of(context).textTheme.headlineSmall,
),
),
],
),
),
),
],
),
);
}
}
class MenuApp extends StatelessWidget {
const MenuApp({super.key});
static const String kMessage = '"Talk less. Smile more." - A. Burr';
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: MyCheckboxMenu(message: kMessage)),
);
}
}

View File

@@ -0,0 +1,114 @@
// 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.
/// Flutter code sample for [RadioMenuButton].
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MenuApp());
class MyRadioMenu extends StatefulWidget {
const MyRadioMenu({super.key});
@override
State<MyRadioMenu> createState() => _MyRadioMenuState();
}
class _MyRadioMenuState extends State<MyRadioMenu> {
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
Color _backgroundColor = Colors.red;
late ShortcutRegistryEntry _entry;
static const SingleActivator _redShortcut = SingleActivator(LogicalKeyboardKey.keyR, control: true);
static const SingleActivator _greenShortcut = SingleActivator(LogicalKeyboardKey.keyG, control: true);
static const SingleActivator _blueShortcut = SingleActivator(LogicalKeyboardKey.keyB, control: true);
@override
void didChangeDependencies() {
super.didChangeDependencies();
_entry = ShortcutRegistry.of(context).addAll(<ShortcutActivator, VoidCallbackIntent>{
_redShortcut: VoidCallbackIntent(() => _setBackgroundColor(Colors.red)),
_greenShortcut: VoidCallbackIntent(() => _setBackgroundColor(Colors.green)),
_blueShortcut: VoidCallbackIntent(() => _setBackgroundColor(Colors.blue)),
});
}
@override
void dispose() {
_buttonFocusNode.dispose();
_entry.dispose();
super.dispose();
}
void _setBackgroundColor(Color? color) {
setState(() {
_backgroundColor = color!;
});
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MenuAnchor(
childFocusNode: _buttonFocusNode,
menuChildren: <Widget>[
RadioMenuButton<Color>(
value: Colors.red,
shortcut: _redShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Red Background'),
),
RadioMenuButton<Color>(
value: Colors.green,
shortcut: _greenShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Green Background'),
),
RadioMenuButton<Color>(
value: Colors.blue,
shortcut: _blueShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Blue Background'),
),
],
builder: (BuildContext context, MenuController controller, Widget? child) {
return TextButton(
focusNode: _buttonFocusNode,
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('OPEN MENU'),
);
},
),
Expanded(
child: Container(
color: _backgroundColor,
),
),
],
);
}
}
class MenuApp extends StatelessWidget {
const MenuApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: MyRadioMenu()),
);
}
}