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\nescape 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().isEmptyto avoid false positives if only whitespace is present.
内容的提问来源于stack exchange,提问作者Ruben
相关产品推荐
相关产品推荐

