Flutter,按下按钮后如何在Text Widget上粘贴数据?

Flutter, how to paste data on Text Widget after pressing the button?

一开始我真的很抱歉我的英语不完美;)

我从应用程序复制数据,我想在我的应用程序中点击按钮,它会复制数据并粘贴到小部件文本上

预先感谢您的帮助。

有一项名为 Clipboard which will allow you to access the system's clipboard/pasteboard. It will give you some ClipboardData 的服务,其中将包含一些文本。

  void _getClipboardText() async {
    final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
    String? clipboardText = clipboardData?.text;
    print(clipboardText);
  }

如何将其设置为文本小部件的完整示例

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String? _clipboardText;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            if (_clipboardText?.isNotEmpty == true)
              Text(
                '$_clipboardText',
                style: Theme.of(context).textTheme.headline4,
              ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getClipboardText,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  void _getClipboardText() async {
    final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
    setState(() {
      _clipboardText = clipboardData?.text;
    });
  }
}