similar_img 0.0.1 copy "similar_img: ^0.0.1" to clipboard
similar_img: ^0.0.1 copied to clipboard

outdated

A new Flutter project.

example/lib/main.dart

import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:similar_img/entity.dart';
import 'package:similar_img/image_provider.dart';
import 'package:similar_img/similar_img.dart';
import 'package:similar_img/thumbnail.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _similarImgPlugin = SimilarImg();
  final List<Uint8List> _dataList = [];
  final List<AssetEntity> _assetEntityList = [];
  final List<Map> _mapList = [];

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _similarImgPlugin.getPlatformVersion() ??
          'Unknown platform version';
      final list = await _similarImgPlugin.fectchSimilarArray();
      for (var element in list) {
        for (var child in element) {
          var map = {};
          var tmp = child as Map;
          _assetEntityList.add(AssetEntity.fromJson(tmp));
          var id = tmp["id"];
          var width = tmp["width"];
          var height = tmp["height"];
          var option = ThumbnailOption.ios(size: ThumbnailSize(width, height));
          map["id"] = id;
          map["option"] = option.toMap();
          _mapList.add(map);
        }
      }
      for (var map in _mapList) {
        var data = await _similarImgPlugin.getThumb(map);
        _dataList.add(data);
      }

      // 查看第一个图的原图地址
      final path = await _similarImgPlugin.getOrigin(_assetEntityList.first.id);
      if (kDebugMode) {
        print("path=$path");
      }
      // 删除最后一个
      final remove = await _similarImgPlugin.removeAeest(
          _assetEntityList.last.id, _assetEntityList.last.typeInt);
      if (kDebugMode) {
        print("remove=$remove");
      }
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ...List<Widget>.generate(_assetEntityList.length, (index) {
                final ent = _assetEntityList[index];
                final data = _dataList[index];
                bool isNotEmpty = ent != null && data != null;
                return isNotEmpty
                    ? Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: AssetEntityImage(
                          ent,
                          data,
                        ),
                      )
                    : Container();
              }).toList(),
              Text('Running on: $_platformVersion\n'),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
30
pub points
13%
popularity

Publisher

unverified uploader

A new Flutter project.

Homepage

License

Apache-2.0 (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on similar_img