You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何用share_plus按自定义格式分享文本

Solution

To format your shared password data into the specified structure, you need to construct the shared text with each field labeled and separated by newlines. Replace your current text variable assignment with a multi-line string that includes the required labels and corresponding controller values.

Here's the corrected code for your IconButton:

IconButton(
  onPressed: () async {
    // Construct the formatted text with labels and newlines
    final text = '''Title: ${_titleController.text}
Email: ${_emailController.text}
Password: ${_userPasswordController.text}
URL: ${_urlController.text}''';
    
    if (_password == null || text.trim().isEmpty) {
      await showCannotShareEmptyPasswordDialog(context);
    } else {
      Share.share(text);
    }
  },
  icon: const Icon(Icons.share),
),

Key Changes:

  • Used Dart's triple quotes (''') to create a multi-line string, which simplifies defining the exact format without manually adding \n escape characters.
  • Added each field label (Title, Email, Password, URL) followed by the corresponding controller's text value.
  • Updated the empty check to use text.trim().isEmpty to avoid false positives if only whitespace is present.

内容的提问来源于stack exchange,提问作者Ruben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:12:14