图像选择器颤动网1.9

时间:2019-09-21 19:34:06

标签: flutter flutter-web

在新的Flutter Web 1.9上寻找图像选择器。我找到了一种方法,但不到1.9,现在合并了,不知道我怎么能做到这一点。尝试使用dart:html,但无法发布!仅在运行

3 个答案:

答案 0 :(得分:6)

对于Flutter web 1.10 dev来说,universal_html package似乎是dart:html暂时空缺职位的好人选:

import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:universal_html/prefer_universal/html.dart' as html;

class ImagePickerLabPage extends StatefulWidget {
  @override
  _ImagePickerLabPageState createState() => _ImagePickerLabPageState();
}

class _ImagePickerLabPageState extends State<ImagePickerLabPage> {
  String name = '';
  String error;
  Uint8List data;

  pickImage() {
    final html.InputElement input = html.document.createElement('input');
    input
      ..type = 'file'
      ..accept = 'image/*';

    input.onChange.listen((e) {
      if (input.files.isEmpty) return;
      final reader = html.FileReader();
      reader.readAsDataUrl(input.files[0]);
      reader.onError.listen((err) => setState(() {
            error = err.toString();
          }));
      reader.onLoad.first.then((res) {
        final encoded = reader.result as String;
        // remove data:image/*;base64 preambule
        final stripped =
            encoded.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), '');

        setState(() {
          name = input.files[0].name;
          data = base64.decode(stripped);
          error = null;
        });
      });
    });

    input.click();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(name),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.open_in_browser),
        onPressed: () {
          pickImage();
        },
      ),
      body: Center(
        child: error != null
            ? Text(error)
            : data != null ? Image.memory(data) : Text('No data...'),
      ),
    );
  }
}

答案 1 :(得分:1)

实际上onChange不能在移动safari上很好地工作,它需要更改addEventListener并附加它。

func processLogin() {

  //performing login in this method so when login successful we setting root view controller
  callLoginAPI { response in

    if response.isSuccess {
       UserDefaults.standard.set(true, forKey: "isLogin")
       if #available(iOS 13.0, *) {
           SceneDelegate.shared.setRootViewControllerBasedOnLogin()
        } else {
          // Fallback on earlier versions
        }
    }
  }
}

这也可以:

Future<void> _setImage() async {
    final completer = Completer<List<String>>();
    InputElement uploadInput = FileUploadInputElement();
    uploadInput.multiple = true;
    uploadInput.accept = 'image/*';
    uploadInput.click();
    // onChange doesn't work on mobile safari
    uploadInput.addEventListener('change', (e) async {
        // read file content as dataURL
        final files = uploadInput.files;
        Iterable<Future<String>> resultsFutures = files.map((file) {
            final reader = FileReader();
            reader.readAsDataUrl(file);
            reader.onError.listen((error) => completer.completeError(error));
            return reader.onLoad.first.then((_) => reader.result as String);
        });

        final results = await Future.wait(resultsFutures);
        completer.complete(results);
    });
    // need to append on mobile safari
    document.body.append(uploadInput);
    final List<String> images = await completer.future;
    setState(() {
        _uploadedImages = images;
    });
    uploadInput.remove();
}

答案 2 :(得分:1)

您为什么不只使用https://pub.dev/packages/image_picker_web

它还支持多种outputTypes(文件,小部件,Uint8List)