如何将共享功能添加到以下Dart代码中

时间:2019-10-11 11:41:17

标签: flutter dart


import 'package:flutter/material.dart';

import 'package:share/share.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Stop addiction',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: Scaffold(
        appBar: AppBar(backgroundColor:Colors.black,
          title: Text('stop addiction',style:TextStyle(color:Colors.red),),centerTitle: true,actions: <Widget>[
            IconButton(icon: Icon(Icons.share,color:Colors.white70,), onPressed:Share.share('check out my website https://example.com');)
          ],
        ),
        body: _blank()
      ),
    );
  }
}

问题:

  1. 如何向上述代码添加共享功能
  2. 用户单击图标时,共享选项应出现在屏幕上。

1 个答案:

答案 0 :(得分:1)

代码应为

    IconButton(icon: Icon(Icons.share,color:Colors.white70,), onPressed:() => Share.share('check out my website https://example.com')/*semicolon removed here*/)

然后,删除分号