Flutter桌面软件开发如何复制文本内容

发布于 1周前 作者 phonegap100 最后一次编辑是 5天前 来自 分享

Flutter桌面软件开发中要复制文本内容可以使用SelectableText

提示:SelectableText 会和自定义的右键菜单冲突,所以配置右键菜单应该避免全局配置

在 Flutter 1.9 中引入了 SelectableText 小部件。 SelectableText 定义的text就是可复制的文本

const SelectableText("flutter 如何从 TextWidget 复制文本,可以使用SelectableText")

Flutter桌面软件开发复制文本内容完整代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SelectableText(
                "flutter 如何从 TextWidget 复制文本,可以使用SelectableText"),
            const SizedBox(height: 40),
            Row(
              children: [
                Text("xxwqrwqtxx114215eeee4215"),
                ElevatedButton(
                  child: Text('点击复制'),
                  onPressed: () {
                    FlutterClipboard.copy('xxwqrwqtxx114215eeee4215')
                        .then((value) => print('copied'));
                  },
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

回到顶部