将所选图像添加到水平列表视图

Add selected image to horizontal listView

我在我的项目中使用了 multi_image_picker 插件,它运行良好。现在我尝试修改代码,使所选图像以水平 ListView 而不是 GridView 显示。

但是我遇到了以下异常

I/flutter (18429): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 3723 pos 14: 'owner._debugCurrentBuildTarget == this': is not true.
I/chatty  (18429): uid=10160(com.example.flutter_sample) 1.ui identical 7 lines
I/flutter (18429): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 3723 pos 14: 'owner._debugCurrentBuildTarget == this': is not true.
D/EGL_emulation(18429): eglMakeCurrent: 0xe18058a0: ver 2 0 (tinfo 0xe18035c0)

这是我尝试的代码。

Container(
      margin: EdgeInsets.symmetric(vertical: 20.0),
      height: 200.0,
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: <Widget>[
            Expanded(
               child: buildGridView(),
            )
             ],
           ),
         )

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

开始前请查看此文档

https://pub.dev/packages/multi_image_picker https://sh1d0w.github.io/multi_image_picker/#/gettingstarted

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

    import 'package:flutter/services.dart';
    import 'package:multi_image_picker/multi_image_picker.dart';

    //https://pub.dartlang.org/packages/multi_image_picker#-example-tab-
    class MultiImagePick extends StatefulWidget {
    @override
    _MyAppState createState() => new _MyAppState();
    }

    class _MyAppState extends State<MultiImagePick> {
    List<Asset> images;
    String _error;

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

    Future<void> pickImages() async {
        setState(() {
        images = null;
        });

        List resultList;
        String error;

        try {
        resultList = await MultiImagePicker.pickImages(
            maxImages: 3,
        );
        } on PlatformException catch (e) {
        error = e.message;
        }

        // 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;
        if (error == null) _error = 'No Error Dectected';
        });
    }

    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
            title: const Text('Photo view'),
            ),
            body: new Container(
            padding: const EdgeInsets.all(8.0),
            child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                images == null
                    ?  Container(
                        height: 300.0,
                        width: 400.0,
                        child: new Icon(
                            Icons.image,
                            size: 250.0,
                            color: Theme.of(context).primaryColor,
                        ),
                        )
                    :  SizedBox(
                        height: 200.0,
                        width: 500,
                        child: new ListView.builder(
                            scrollDirection: Axis.horizontal,
                            itemBuilder: (BuildContext context, int index) =>
                                new Padding(
                                padding: const EdgeInsets.all(5.0),

                                child : AssetThumb(
                                    asset: images[index],
                                    height: 200,
                                    width: 200,
                                    )
                            ),
                            itemCount: images.length,
                        ),
                        ),
                Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: new Text('Error Dectected: $_error'),
                ),
                RaisedButton.icon(
                    onPressed: pickImages,
                    icon: new Icon(Icons.image),
                    label: new Text("Pick-Up Images")),
                ],
            ),
            ),
        ),
        );
    }
    }