无法在 SelectableRegion 或 SelectionArea 中添加文本选择上下文菜单按钮项目

问题描述 投票:0回答:1

我希望在点击并按住文本时在文本选择弹出按钮中显示一个附加按钮。 选择文本将位于文本小部件或任何其他小部件内,但不是像输入表单字段那样的可编辑字段。我尝试了各种方法,但都没有显示出不匹配的 lint/语法错误或按钮本身。

最终目标:

我想从文本小部件(不是表单字段)中选择文本,并有一个附加按钮来执行操作。

enter image description here

我尝试过:

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 !!!',
              )),
        ));
  }
}

flutter dart
1个回答
0
投票

尝试使用

SelectableText
小部件来选择您的文本

© www.soinside.com 2019 - 2024. All rights reserved.