【问题标题】:Image is being passed in second attempt not in the first attempt in Flutter图像在第二次尝试中传递,而不是在 Flutter 中的第一次尝试中传递
【发布时间】:2020-12-11 19:57:23
【问题描述】:

我需要一些关于 Flutter 中的图像选择器插件的帮助。当我在警报对话框中按下图像或画廊并选择我的图像时,背景图像正在显示但未传递/保存但是当我再次在警报对话框中按下图像或画廊并选择我的图像时,背景图像都被显示并通过/保存。我该怎么办?

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class UserImagePicker extends StatefulWidget {
  UserImagePicker(this.imagePickfn);

  final void Function(File pickedImage) imagePickfn;

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

class _UserImagePickerState extends State<UserImagePicker> {
  File _pickedImage;

  void _pickImage() {
    showDialog<ImageSource>(
      context: context,
      builder: (context) =>
          AlertDialog(title: Text("Choose image source"), actions: [
        FlatButton(
          child: Text("Camera"),
          onPressed: () => Navigator.pop(context, ImageSource.camera),
        ),
        FlatButton(
          child: Text("Gallery"),
          onPressed: () => Navigator.pop(context, ImageSource.gallery),
        ),
      ]),
    ).then((ImageSource source) async {
      if (source != null) {
        final pickedFile = await ImagePicker().getImage(
          source: source,
          imageQuality: 50,
          maxWidth: 150,
        );
        setState(() => _pickedImage = File(pickedFile.path));
      }
    });
    widget.imagePickfn(_pickedImage);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        CircleAvatar(
          radius: 40,
          backgroundColor: Theme.of(context).primaryColor,
          backgroundImage:
              _pickedImage != null ? FileImage(_pickedImage) : null,
        ),
        FlatButton.icon(
          textColor: Theme.of(context).primaryColor,
          onPressed: _pickImage,
          icon: Icon(Icons.image),
          label: Text('Add an Image'),
        ),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    问题出在_pickImage() 方法中。您需要将 widget.imagePickfn(_pickedImage); 移动到 then 部分中。像这样。

    void _pickImage() {
        showDialog<ImageSource>(
          context: context,
          builder: (context) =>
              AlertDialog(title: Text("Choose image source"), actions: [
            FlatButton(
              child: Text("Camera"),
              onPressed: () => Navigator.pop(context, ImageSource.camera),
            ),
            FlatButton(
              child: Text("Gallery"),
              onPressed: () => Navigator.pop(context, ImageSource.gallery),
            ),
          ]),
        ).then((ImageSource source) async {
          if (source != null) {
            final pickedFile = await ImagePicker().getImage(
              source: source,
              imageQuality: 50,
              maxWidth: 150,
            );
            setState(() => _pickedImage = File(pickedFile.path));
            widget.imagePickfn(_pickedImage);
          }
        });
      }
    

    这是因为您在 then 方法之外调用了 widget.imagePickfn(_pickedImage); 方法。关闭对话框后,在then 部分中返回拾取的图像结果。这就是为什么在第一次尝试时您的图像没有返回,而在第二次尝试中它返回的是之前选择的图像。

    希望这能解决您的问题。

    如果上面的方法不能解决你的问题,你可以试试这个

    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    
    class UserImagePicker extends StatefulWidget {
      UserImagePicker(this.imagePickfn);
    
      final void Function(File pickedImage) imagePickfn;
    
      @override
      _UserImagePickerState createState() => _UserImagePickerState();
    }
    
    class _UserImagePickerState extends State<UserImagePicker> {
      File _pickedImage;
    
      void _pickImage() async {
        ImageSource source = await showDialog<ImageSource>(
          context: context,
          builder: (context) =>
              AlertDialog(title: Text("Choose image source"), actions: [
            FlatButton(
              child: Text("Camera"),
              onPressed: () => Navigator.pop(context, ImageSource.camera),
            ),
            FlatButton(
              child: Text("Gallery"),
              onPressed: () => Navigator.pop(context, ImageSource.gallery),
            ),
          ]),
        );
        
        if (source != null) {
          final pickedFile = await ImagePicker().getImage(
            source: source,
            imageQuality: 50,
            maxWidth: 150,
          );
          setState(() => _pickedImage = File(pickedFile.path));
          widget.imagePickfn(_pickedImage);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: <Widget>[
            CircleAvatar(
              radius: 40,
              backgroundColor: Theme.of(context).primaryColor,
              backgroundImage:
                  _pickedImage != null ? FileImage(_pickedImage) : null,
            ),
            FlatButton.icon(
              textColor: Theme.of(context).primaryColor,
              onPressed: () => _pickImage(),
              icon: Icon(Icons.image),
              label: Text('Add an Image'),
            ),
          ],
        );
      }
    }
    

    【讨论】:

    • 它不工作,再次尝试第二次接受输入。
    • 添加了另一个解决方案,希望第二个能解决您的问题。
    • 它不工作,同样的事情。在第二次尝试中拾取图像。不过谢谢你的回复。
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2013-07-01
    • 1970-01-01
    相关资源
    最近更新 更多