【问题标题】:How to force show text selection controls?如何强制显示文本选择控件?
【发布时间】:2020-10-09 11:20:10
【问题描述】:

我需要在以下位置显示文本选择控件:

setState( () {
  textController.selection = TextSelection(baseOffset: 0, extentOffset: textController.text.length);
});

但它没有出现在屏幕上。那么,如何强制flutter显示剪切/复制/粘贴菜单呢?

【问题讨论】:

标签: flutter flutter-layout


【解决方案1】:

希望对你有帮助。
您可以使用可选小部件。
https://www.youtube.com/watch?v=ZSU3ZXOs6hc&list=PLjxrf2q8roU23XGwz3Km7sQZFTdB996iG&index=56

第二个建议

当我使用下面的代码进行测试时,它运行良好。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "App",
      theme: new ThemeData(primarySwatch: Colors.amber),
      home: Test(),
    );
  }
}

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  TextEditingController controller = new TextEditingController(text: 'Kevin A');

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Title",
      theme: new ThemeData(primarySwatch: Colors.amber),
      home: Scaffold(
        body: SafeArea(
          child: TextField(
            controller: controller,
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            controller.selection = new TextSelection(
              baseOffset: 0,
              extentOffset: controller.text.length,
            );
            showMenu(
              context: context,
              // TODO: Position dynamically based on cursor or textfield
              position: RelativeRect.fromLTRB(0.0, 300.0, 300.0, 0.0),
              items: [
                PopupMenuItem(
                  child: Row(
                    children: <Widget>[
                      // TODO: Dynamic items / handle click
                      PopupMenuItem(
                        child: Text(
                          "Paste",
                          style: Theme.of(context)
                              .textTheme
                              .body2
                              .copyWith(color: Colors.red),
                        ),
                      ),
                      PopupMenuItem(
                        child: Text("Select All"),
                      ),
                    ],
                  ),
                ),
              ],
            );
          },
          child: Icon(Icons.navigation),
          backgroundColor: Colors.green,
        ),
      ),
    );
  }
}

【讨论】:

  • 不,我需要使用 TextEditingController 和 TextField 来完成
猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 2016-03-14
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多