【问题标题】:Flutter: Different ListView ItemBuilder layouts based on Network image dimensionsFlutter:基于网络图像尺寸的不同 ListView ItemBuilder 布局
【发布时间】:2020-06-08 22:36:35
【问题描述】:

我正在尝试使用 Flutter 构建一个新闻阅读器应用程序。然而,我面临的挑战是我需要根据网络图像的尺寸显示不同的布局。我对此进行了研究,但找不到解决方案。请帮忙。

【问题讨论】:

    标签: listview flutter flutter-layout


    【解决方案1】:

    我不知何故做了一个解决方法,不使用列表视图,而是在水平方向使用单子滚动视图和行小部件列表。 只需查看我制作的示例: 我已经使用网络图像列表在本地获取了 JSON 数据。

    以下是我的本地 JSON(parse.json):

    {
        "apus_findgo_gallery_images": {
            "2092": "http://www.camhalalguide.com/wp-content/uploads/2019/11/6b806c88-2f19-4054-914b-dee3c0c091eb.jpg",
            "2093": "http://www.camhalalguide.com/wp-content/uploads/2019/11/24.1.1-phnom-chisor-temple.jpg",
            "2096": "http://www.camhalalguide.com/wp-content/uploads/2019/11/image-asset.jpg",
            "2098": "http://www.camhalalguide.com/wp-content/uploads/2019/11/phnom-chisor-01-600_orig.jpg",
            "2099": "http://www.camhalalguide.com/wp-content/uploads/2019/11/phnom-chisor-02-600_orig.jpg",
            "2100": "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg",
            "2101":"https://images.ctfassets.net/4kwp3o37mlek/26EyWHHgDOS4qSCCmAWYuY/e4fd2b3c2054b06fafe7ddebca47d1a8/Apple_Logo.png",
            "2102":"https://img.helpnetsecurity.com/wp-content/uploads/2015/12/09195727/1450275992_key-100x100.png",
            "2103":"https://fakeimg.pl/200x100/282828/eae0d0/?retina=1",
            "2104":"https://www.calliaweb.co.uk/wp-content/uploads/2015/10/600x200.jpg"
        }
    }
    

    下面是我制作的模型类:

    // To parse this JSON data, do
    //
    //     final yourModelClass = yourModelClassFromJson(jsonString);
    
    import 'dart:convert';
    
    YourModelClass yourModelClassFromJson(String str) => YourModelClass.fromJson(json.decode(str));
    
    String yourModelClassToJson(YourModelClass data) => json.encode(data.toJson());
    
    class YourModelClass {
        Map<String, String> apusFindgoGalleryImages;
    
        YourModelClass({
            this.apusFindgoGalleryImages,
        });
    
        factory YourModelClass.fromJson(Map<String, dynamic> json) => YourModelClass(
            apusFindgoGalleryImages: Map.from(json["apus_findgo_gallery_images"]).map((k, v) => MapEntry<String, String>(k, v)),
        );
    
        Map<String, dynamic> toJson() => {
            "apus_findgo_gallery_images": Map.from(apusFindgoGalleryImages).map((k, v) => MapEntry<String, dynamic>(k, v)),
        };
    }
    
    

    这是 ia 制作的主要课程:

    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:sample_project_for_api/model.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      List<String> imagesList = List();
    
      @override
      void initState() {
        super.initState();
        yourMethod();
      }
    
      yourMethod() async {
        String jsonString = await loadFromAssets();
        final yourModelClass = yourModelClassFromJson(jsonString);
    
        Map data = yourModelClass.apusFindgoGalleryImages;
        List<String> list = new List();
    
        data.forEach((key, value) {
          list.add(value);
        });
        imagesList = list;
    
        setState(() {});
      }
    
      Future<String> loadFromAssets() async {
        return await rootBundle.loadString('json/parse.json');
      }
    
      Widget ImageCard(String data) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            width: MediaQuery.of(context).size.width * 0.97,
            child: Card(
                elevation: 5,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Image.network(
                        data,
                        loadingBuilder: (BuildContext context, Widget child,
                            ImageChunkEvent loadingProgress) {
                          if (loadingProgress == null) return child;
                          return Center(
                            child: CircularProgressIndicator(
                              value: loadingProgress.expectedTotalBytes != null
                                  ? loadingProgress.cumulativeBytesLoaded /
                                      loadingProgress.expectedTotalBytes
                                  : null,
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                )),
          ),
        );
      }
    
      Widget _buildImageCards(List<String> product) {
        final cards = <Widget>[];
        Widget imageCards;
    
        if (product.length > 0) {
          for (int i = 0; i < product.length; i++) {
            cards.add(ImageCard(product[i]));
            print(product.length);
          }
          imageCards = Container(
            padding: EdgeInsets.only(top: 16, bottom: 8),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(children: cards),
                ),
              ],
            ),
          );
        } else {
          imageCards = Container();
        }
        return imageCards;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Container(
          padding: EdgeInsets.only(top: 16, bottom: 8),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Row(
                  children: <Widget>[
                    _buildImageCards(imagesList),
                  ],
                ),
              ),
            ],
          ),
        ));
      }
    }
    
    
    

    【讨论】:

    • 谢谢您的回复。但是,我一直在寻找一种解决方案,可以根据网络图像的尺寸进行不同的布局。
    • 能否根据尺寸简单介绍不同的布局。
    最近更新 更多