2

I have to select image with MultiImagePicker so i have this function that await for selected asset, but this function is called when i click on a button . I want to select foto when i enter inside the page without clicking any button, is this possible ?

List<Asset> images = List<Asset>();
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = '';
try {
  resultList = await MultiImagePicker.pickImages(
    maxImages: 300,
    enableCamera: false,
    selectedAssets: images,
  );
} on Exception catch (e) {
  error = e.toString();
}
if (!mounted) return;

setState(() {
  images = resultList;
});
}

@override
Widget build(BuildContext context) {
if(images.length == 0){
return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
              centerTitle: true,
              title: Text('Picker Example'),
              backgroundColor: Colors.blueAccent
          ),
          body:Container(
            child: images.length > 0 ? Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text("Select photo"),
                  onPressed: () => loadAssets(),
                ),
                RaisedButton(
                  child: Text("Remove photo"),
                  onPressed: removeAssets,
                ),
                RaisedButton(
                  child: Text("upload photo"),
                  onPressed: upload,
                ),
                Expanded(
                  child: buildGridView(),
                )
              ],
            ) :loadAssets();
       )
);

Is this possible ?

matteo
  • 95
  • 1
  • 7
  • Go through the example here: https://pub.dev/packages/multi_image_picker/example – Shri Hari L Nov 29 '20 at 13:11
  • already saw that and i say that i wan't that without clic on any button, so i wan't to load assets without onpressed – matteo Nov 29 '20 at 13:13
  • So, you want to select images on page loads without button? – Shri Hari L Nov 29 '20 at 13:14
  • yes exact, and if i select some immages i wan't to see two button for upload and for remove precent selected photo and for re-select photo, i already do that now i update my question so you can understand better – matteo Nov 29 '20 at 13:17

1 Answers1

1

Call loadAssets() in initState() function. After selecting, images are stored in images list. Then do what you want.

Example taken from Docs,

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

import 'package:multi_image_picker/multi_image_picker.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Asset> images = List<Asset>();
  String _error = 'No Error Dectected';

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

  Widget buildGridView() {
    return GridView.count(
      crossAxisCount: 3,
      children: List.generate(images.length, (index) {
        Asset asset = images[index];
        return AssetThumb(
          asset: asset,
          width: 300,
          height: 300,
        );
      }),
    );
  }

  Future<void> loadAssets() async {
    List<Asset> resultList = List<Asset>();
    String error = 'No Error Dectected';

    try {
      resultList = await MultiImagePicker.pickImages(
        maxImages: 300,
        enableCamera: true,
        selectedAssets: images,
        cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
        materialOptions: MaterialOptions(
          actionBarColor: "#abcdef",
          actionBarTitle: "Example App",
          allViewTitle: "All Photos",
          useDetailsView: false,
          selectCircleStrokeColor: "#000000",
        ),
      );
    } on Exception catch (e) {
      error = e.toString();
    }

    // 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(() {
      images = resultList;
      _error = error;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            Center(child: Text('Error: $_error')),
            Expanded(
              child: images.length > 0 ? buildGridView() : Center(child: Text("No Images")),
            )
          ],
        ),
      ),
    );
  }
}
Shri Hari L
  • 4,551
  • 2
  • 6
  • 18
  • yo Hari, by any chance you know how to solve this? https://stackoverflow.com/questions/67705273/build-data-back-to-listview-builder-flutter – VegetaSaiyan May 27 '21 at 04:41