我希望在点击并按住文本时在文本选择弹出按钮中显示一个附加按钮。 选择文本将位于文本小部件或任何其他小部件内,但不是像输入表单字段那样的可编辑字段。我尝试了各种方法,但都没有显示出不匹配的 lint/语法错误或按钮本身。
我想从文本小部件(不是表单字段)中选择文本,并有一个附加按钮来执行操作。
https://zenn.dev/taki/articles/9feff78e2b2503 再加上更多的变体,我也尝试了 SelectableRegion,但仍然不起作用。
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? selectedText;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Selection Area'),
),
body: Center(
// I tried SelectableRegion as well, still not working
child: SelectionArea(
onSelectionChanged: (final value) {
print(value?.plainText);
setState(() {
selectedText = value?.plainText;
});
},
contextMenuBuilder: (context, selectableRegionState) {
final extraButton = ContextMenuButtonItem(
label: 'Extra Button',
onPressed: () async {
if (selectedText == null) return;
});
return AdaptiveTextSelectionToolbar.buttonItems(buttonItems: [
extraButton,
...selectableRegionState.contextMenuButtonItems
], anchors: selectableRegionState.contextMenuAnchors);
},
child: const Text(
'!!! Select Part of Me !!!',
)),
));
}
}
尝试使用
SelectableText
小部件来选择您的文本