【问题标题】:In Flutter how can I receive an image in a main screen and then redirect it to another screen?在 Flutter 中,如何在主屏幕中接收图像,然后将其重定向到另一个屏幕?
【发布时间】:2021-01-08 08:12:41
【问题描述】:

我正在开发一款纸牌游戏,用户可以在其中添加新卡片。每张卡片都有一个图像路径。 我正在尝试使用来自另一个应用程序(例如:图库)的图像预填充我的新卡屏幕。 为此,我使用包receive_sharing_intent,成功地打印以在我的应用程序中控制我的图像路径。现在我不能做的是在获取图像后重定向到新屏幕。 这是我主屏幕上的相关代码:

class Menu extends StatefulWidget {
  final String title;

  Menu({Key key, this.title}) : super(key: key);

  @override
  _MenuState createState() => _MenuState();
}

class _MenuState extends State<Menu> {
  StreamSubscription _intentDataStreamSubscription;
  List<SharedMediaFile> _sharedFiles;

  @override
  void initState() {
    super.initState();
    bootstrapCards();
    getOurCards();
    // For sharing images coming from outside the app while the app is in the memory
    _intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream()
        .listen((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
        print("Shared:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
        _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));

      });
    }, onError: (err) {
      print("getIntentDataStream error: $err");
    });

    // For sharing images coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
        print("Shared:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
        _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));

      });
    });

  }
  @override
  void dispose() {
    _intentDataStreamSubscription.cancel();
    super.dispose();
  }
  void _goCardAdd(path) {
    MyCard myEmptyCard = new MyCard();
    myEmptyCard.url=path;
    Navigator.of(context).push(
      MaterialPageRoute<void>(
          builder: (BuildContext context) => AddEditScreen(myCard: myEmptyCard)),
    );
  }

似乎 _goCardAdd 调用没有做任何事情,我不明白为什么。 如果我把它放在 setState 调用之后,我会收到一个错误提示

在构建期间调用 setState() 或 markNeedsBuild()。

完整代码见https://github.com/diogocsc/papoapapoFlutter

【问题讨论】:

    标签: image flutter


    【解决方案1】:

    这似乎是某种构建问题,因为我只对代码进行了微小的更改,它就开始工作了。同时我需要添加一个 if 子句来检查文件是否存在,因为如果不存在,它总是会在我打开我的应用程序时将我重定向到新的卡片屏幕,即使没有图像文件正在进行中。检查下面的代码。

    class _MenuState extends State<Menu> {
      StreamSubscription _intentDataStreamSubscription;
      List<SharedMediaFile> _sharedFiles;
    
      @override
      void initState() {
        super.initState();
        bootstrapCards();
        // For sharing images coming from outside the app while the app is in the memory
        _intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream()
            .listen((List<SharedMediaFile> value) {
          setState(() {
            _sharedFiles = value;
            print("Shared 1:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
            if (_sharedFiles != null) _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
    
          });
    
        }, onError: (err) {
          print("getIntentDataStream error: $err");
        });
    
        // For sharing images coming from outside the app while the app is closed
        ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
          setState(() {
            _sharedFiles = value;
            print("Shared 2:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
            if (_sharedFiles != null) _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
    
          });
    
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2020-11-13
      • 2019-10-27
      • 2021-07-29
      • 1970-01-01
      • 2021-12-15
      • 2018-05-28
      相关资源
      最近更新 更多