Enable copy to clipboard for web, as it's added to Flutter master channel (#253)

pull/257/head
Per Classon 5 years ago committed by GitHub
parent 7f783273c3
commit be484af4d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -299,10 +299,6 @@ class _DemoPageState extends State<DemoPage> with TickerProviderStateMixin {
maxHeight: maxSectionHeight, maxHeight: maxSectionHeight,
codeWidget: CodeDisplayPage( codeWidget: CodeDisplayPage(
_currentConfig.code, _currentConfig.code,
hasCopyButton:
!kIsWeb, // TODO: Add support for copying code in Web.
// TODO: It is a known issue that Clipboard does not work on web.
// TODO: https://github.com/flutter/flutter/issues/40124
), ),
), ),
); );
@ -701,10 +697,9 @@ class _DemoSectionCode extends StatelessWidget {
} }
class CodeDisplayPage extends StatelessWidget { class CodeDisplayPage extends StatelessWidget {
const CodeDisplayPage(this.code, {this.hasCopyButton = true}); const CodeDisplayPage(this.code);
final CodeDisplayer code; final CodeDisplayer code;
final bool hasCopyButton;
Widget build(BuildContext context) { Widget build(BuildContext context) {
final isDesktop = isDisplayDesktop(context); final isDesktop = isDisplayDesktop(context);
@ -723,7 +718,7 @@ class CodeDisplayPage extends StatelessWidget {
); );
} }
void _showSnackBarOnCopyFailure(Exception exception) { void _showSnackBarOnCopyFailure(Object exception) {
Scaffold.of(context).showSnackBar( Scaffold.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text( content: Text(
@ -737,32 +732,33 @@ class CodeDisplayPage extends StatelessWidget {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
if (hasCopyButton) Padding(
Padding( padding: isDesktop
padding: isDesktop ? EdgeInsets.only(bottom: 8)
? EdgeInsets.only(bottom: 8) : EdgeInsets.symmetric(vertical: 8),
: EdgeInsets.symmetric(vertical: 8), child: FlatButton(
child: FlatButton( color: Colors.white.withOpacity(0.15),
color: Colors.white.withOpacity(0.15), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, padding: EdgeInsets.symmetric(horizontal: 8),
padding: EdgeInsets.symmetric(horizontal: 8), shape: RoundedRectangleBorder(
shape: RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(4)),
borderRadius: BorderRadius.all(Radius.circular(4)), ),
), onPressed: () async {
onPressed: () async { // The future will not complete on web, thus no
await Clipboard.setData(ClipboardData(text: _plainTextCode)) // Snackbar will be shown, see https://github.com/flutter/flutter/issues/49349.
.then(_showSnackBarOnCopySuccess) await Clipboard.setData(ClipboardData(text: _plainTextCode))
.catchError(_showSnackBarOnCopyFailure); .then(_showSnackBarOnCopySuccess)
}, .catchError(_showSnackBarOnCopyFailure);
child: Text( },
GalleryLocalizations.of(context).demoCodeViewerCopyAll, child: Text(
style: Theme.of(context).textTheme.button.copyWith( GalleryLocalizations.of(context).demoCodeViewerCopyAll,
color: Colors.white, style: Theme.of(context).textTheme.button.copyWith(
fontWeight: FontWeight.w500, color: Colors.white,
), fontWeight: FontWeight.w500,
), ),
), ),
), ),
),
Expanded( Expanded(
child: SingleChildScrollView( child: SingleChildScrollView(
child: Container( child: Container(

Loading…
Cancel
Save