mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.0 KiB
95 lines
3.0 KiB
2 years ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:url_launcher/url_launcher.dart';
|
||
|
|
||
|
import 'constants.dart';
|
||
|
import 'is_valid_email.dart';
|
||
|
import 'platform_selector.dart';
|
||
|
|
||
|
class EmailButtonPage extends StatelessWidget {
|
||
|
EmailButtonPage({
|
||
2 years ago
|
super.key,
|
||
2 years ago
|
required this.onChangedPlatform,
|
||
2 years ago
|
});
|
||
2 years ago
|
|
||
|
static const String route = 'email-button';
|
||
|
static const String title = 'Email Button';
|
||
|
static const String subtitle = 'A selection-aware email button';
|
||
|
static const String url = '$kCodeUrl/email_button_page.dart';
|
||
|
|
||
|
final PlatformCallback onChangedPlatform;
|
||
|
|
||
|
final TextEditingController _controller = TextEditingController(
|
||
|
text: 'Select the email address and open the menu: me@example.com',
|
||
|
);
|
||
|
|
||
|
DialogRoute _showDialog(BuildContext context) {
|
||
|
return DialogRoute<void>(
|
||
|
context: context,
|
||
2 years ago
|
builder: (context) =>
|
||
2 years ago
|
const AlertDialog(title: Text('You clicked send email!')),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text(EmailButtonPage.title),
|
||
|
actions: <Widget>[
|
||
|
PlatformSelector(
|
||
|
onChangedPlatform: onChangedPlatform,
|
||
|
),
|
||
|
IconButton(
|
||
|
icon: const Icon(Icons.code),
|
||
|
onPressed: () async {
|
||
|
if (!await launchUrl(Uri.parse(url))) {
|
||
|
throw 'Could not launch $url';
|
||
|
}
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
body: Center(
|
||
|
child: SizedBox(
|
||
|
width: 300.0,
|
||
|
child: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.start,
|
||
|
children: <Widget>[
|
||
|
const SizedBox(height: 20.0),
|
||
|
const Text(
|
||
|
'This example shows how to add a special button to the context menu depending on the current selection.',
|
||
|
),
|
||
|
const SizedBox(height: 40.0),
|
||
|
TextField(
|
||
|
maxLines: 2,
|
||
|
controller: _controller,
|
||
2 years ago
|
contextMenuBuilder: (context, editableTextState) {
|
||
2 years ago
|
final TextEditingValue value =
|
||
|
editableTextState.textEditingValue;
|
||
|
final List<ContextMenuButtonItem> buttonItems =
|
||
|
editableTextState.contextMenuButtonItems;
|
||
|
if (isValidEmail(value.selection.textInside(value.text))) {
|
||
|
buttonItems.insert(
|
||
|
0,
|
||
|
ContextMenuButtonItem(
|
||
|
label: 'Send email',
|
||
|
onPressed: () {
|
||
|
ContextMenuController.removeAny();
|
||
|
Navigator.of(context).push(_showDialog(context));
|
||
|
},
|
||
|
));
|
||
|
}
|
||
|
return AdaptiveTextSelectionToolbar.buttonItems(
|
||
|
anchors: editableTextState.contextMenuAnchors,
|
||
|
buttonItems: buttonItems,
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|