Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-网格布局_Flutter_Dart_Flutter Layout - Fatal编程技术网

Flutter 颤振-网格布局

Flutter 颤振-网格布局,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我试图在颤振中布置一个4x4的瓷砖网格。我设法用列和行来完成它。但是现在我找到了GridView组件。有谁能举例说明如何使用它 我真的不能把我的头放在那些文件上。我似乎没有得到我想要的结果 用于执行材料。如果你知道你有固定数量的项目,但数量不多(16个就可以了),你可以使用GridView.count。但是,您应该注意,GridView是可滚动的,如果这不是您想要的,您最好只使用行和列 导入“dart:collection”; 导入“package:flatter/scheduler.dart

我试图在颤振中布置一个4x4的瓷砖网格。我设法用列和行来完成它。但是现在我找到了
GridView
组件。有谁能举例说明如何使用它

我真的不能把我的头放在那些文件上。我似乎没有得到我想要的结果

用于执行材料。如果你知道你有固定数量的项目,但数量不多(16个就可以了),你可以使用
GridView.count
。但是,您应该注意,
GridView
是可滚动的,如果这不是您想要的,您最好只使用行和列

导入“dart:collection”;
导入“package:flatter/scheduler.dart”;
进口“包装:颤振/材料.省道”;
导入“dart:convert”;
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
进口“包装:颤振/基础.dart”;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
原始样本:颜色。橙色,
),
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“网格演示”),
),
正文:新建GridView.count(
交叉轴计数:4,
子项:新列表。生成(16,(索引){
返回新网格块(
孩子:新卡(
颜色:Colors.blue.shade200,
孩子:新中心(
子项:新文本('tile$index'),
)
),
);
}),
),
);
}
}

一个将图像加载到平铺中的简单示例

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(

      color: Colors.white30,
      child: GridView.count(
          crossAxisCount: 4,
          childAspectRatio: 1.0,
          padding: const EdgeInsets.all(4.0),
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          children: <String>[
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
            'http://www.for-example.org/img/main/forexamplelogo.png',
          ].map((String url) {
            return GridTile(
                child: Image.network(url, fit: BoxFit.cover));
          }).toList()),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:Colors.white30,
子项:GridView.count(
交叉轴计数:4,
childAspectRatio:1.0,
填充:常数边集全部(4.0),
主轴间距:4.0,
交叉轴间距:4.0,
儿童:[
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
].map((字符串url){
返回网格(
子:Image.network(url,fit:BoxFit.cover));
}).toList()),
);
}
}
FlitterGallery应用程序包含一个真实世界的示例,可以找到

请访问此网站

下面的屏幕截图包含交叉轴计数:2
对于不同的场景,
GridView
中很少有命名构造函数

构造函数

  • GridView
  • GridView.builder
  • GridView.count
  • GridView.custom
  • GridView.extent
  • 下面是
    GridView
    构造函数的示例:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
      MaterialApp(
        home: ExampleGrid(),
      ),
    );
    
    class ExampleGrid extends StatelessWidget {
      List<String> images = [
        "https://uae.microless.com/cdn/no_image.jpg",
        "https://images-na.ssl-images-amazon.com/images/I/81aF3Ob-2KL._UX679_.jpg",
        "https://www.boostmobile.com/content/dam/boostmobile/en/products/phones/apple/iphone-7/silver/device-front.png.transform/pdpCarousel/image.jpg",
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgUgs8_kmuhScsx-J01d8fA1mhlCR5-1jyvMYxqCB8h3LCqcgl9Q",
        "https://ae01.alicdn.com/kf/HTB11tA5aiAKL1JjSZFoq6ygCFXaw/Unlocked-Samsung-GALAXY-S2-I9100-Mobile-Phone-Android-Wi-Fi-GPS-8-0MP-camera-Core-4.jpg_640x640.jpg",
        "https://media.ed.edmunds-media.com/gmc/sierra-3500hd/2018/td/2018_gmc_sierra-3500hd_f34_td_411183_1600.jpg",
        "https://hips.hearstapps.com/amv-prod-cad-assets.s3.amazonaws.com/images/16q1/665019/2016-chevrolet-silverado-2500hd-high-country-diesel-test-review-car-and-driver-photo-665520-s-original.jpg",
        "https://www.galeanasvandykedodge.net/assets/stock/ColorMatched_01/White/640/cc_2018DOV170002_01_640/cc_2018DOV170002_01_640_PSC.jpg",
        "https://media.onthemarket.com/properties/6191869/797156548/composite.jpg",
        "https://media.onthemarket.com/properties/6191840/797152761/composite.jpg",
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GridView(
            physics: BouncingScrollPhysics(), // if you want IOS bouncing effect, otherwise remove this line
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),//change the number as you want
            children: images.map((url) {
              return Card(child: Image.network(url));
            }).toList(),
          ),
        );
      }
    }
    
    以上代码片段的屏幕截图:

    SliverGridView
    示例:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: ExampleGrid(),
          ),
        );
    
    class ExampleGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            primary: false,
            slivers: <Widget>[
              SliverPadding(
                padding: const EdgeInsets.all(20.0),
                sliver: SliverGrid.count(
                  crossAxisSpacing: 10.0,
                  crossAxisCount: 2,
                  children: List.generate(20, (index) {
                    return Card(child: Image.network("https://robohash.org/$index"));
                  }),
                ),
              ),
            ],
          )
        );
      }
    }
    
    导入“包装:颤振/材料.省道”;
    void main()=>runApp(
    材料聚丙烯(
    主页:ExampleGrid(),
    ),
    );
    类ExampleGrid扩展了无状态小部件{
    @凌驾
    小部件构建(构建上下文){
    返回脚手架(
    正文:自定义滚动视图(
    主要:错误,
    条子:[
    填缝料(
    填充:常数边集全部(20.0),
    sliver:SliverGrid.count(
    交叉轴间距:10.0,
    交叉轴计数:2,
    子项:List.generate(20,(索引){
    返回卡(子项:Image.network(“https://robohash.org/美元指数),;
    }),
    ),
    ),
    ],
    )
    );
    }
    }
    
    选择适合您需要的

  • GridView.count(…)

  • GridView(…)

  • GridView.extent(…)

    GridView.extent(
    maxCrossAxisExtent:400,
    儿童:[
    徽标(),
    徽标(),
    徽标(),
    徽标(),
    ],
    )
    
  • 输出(所有输出相同):


    如果有8个项目,我如何在中心显示网格视图例如,如果有8个项目,我如何在中心显示网格视图例如,请查看该子视图如何计算:8.0/9.0?找到8.0/9.0的过程是什么?请在一篇文章中查看所有解决方案,谢谢。它们之间有什么不同呢?@DolDurma谢谢,这些小部件之间不应该有任何类似“差异”的东西,它们应该用于不同的用例,所以请使用更适合您需要的小部件。第二个是很好的选择,感谢这篇文章非常有用maxCrossAxisExtent的意义是什么?@DolDurma在
    GridView.extent
    中,指定
    maxCrossAxisExtent
    ,告诉孩子们即使有空间也不要超过这个宽度。在我的例子中,我使用了
    400
    ,这没有多大意义,取一些较小的值,比如
    100
    ,你强迫孩子们留下来
    请看一下谢谢,physi
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(
      MaterialApp(
        home: ExampleGrid(),
      ),
    );
    
    class ExampleGrid extends StatelessWidget {
      List<String> images = [
        "https://uae.microless.com/cdn/no_image.jpg",
        "https://images-na.ssl-images-amazon.com/images/I/81aF3Ob-2KL._UX679_.jpg",
        "https://www.boostmobile.com/content/dam/boostmobile/en/products/phones/apple/iphone-7/silver/device-front.png.transform/pdpCarousel/image.jpg",
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgUgs8_kmuhScsx-J01d8fA1mhlCR5-1jyvMYxqCB8h3LCqcgl9Q",
        "https://ae01.alicdn.com/kf/HTB11tA5aiAKL1JjSZFoq6ygCFXaw/Unlocked-Samsung-GALAXY-S2-I9100-Mobile-Phone-Android-Wi-Fi-GPS-8-0MP-camera-Core-4.jpg_640x640.jpg",
        "https://media.ed.edmunds-media.com/gmc/sierra-3500hd/2018/td/2018_gmc_sierra-3500hd_f34_td_411183_1600.jpg",
        "https://hips.hearstapps.com/amv-prod-cad-assets.s3.amazonaws.com/images/16q1/665019/2016-chevrolet-silverado-2500hd-high-country-diesel-test-review-car-and-driver-photo-665520-s-original.jpg",
        "https://www.galeanasvandykedodge.net/assets/stock/ColorMatched_01/White/640/cc_2018DOV170002_01_640/cc_2018DOV170002_01_640_PSC.jpg",
        "https://media.onthemarket.com/properties/6191869/797156548/composite.jpg",
        "https://media.onthemarket.com/properties/6191840/797152761/composite.jpg",
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GridView(
            physics: BouncingScrollPhysics(), // if you want IOS bouncing effect, otherwise remove this line
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),//change the number as you want
            children: images.map((url) {
              return Card(child: Image.network(url));
            }).toList(),
          ),
        );
      }
    }
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: ExampleGrid(),
          ),
        );
    
    class ExampleGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GridView.count(
            crossAxisCount: 4,
            children: List.generate(40, (index) {
              return Card(
                child: Image.network("https://robohash.org/$index"),
              ); //robohash.org api provide you different images for any number you are giving
            }),
          ),
        );
      }
    }
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: ExampleGrid(),
          ),
        );
    
    class ExampleGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            primary: false,
            slivers: <Widget>[
              SliverPadding(
                padding: const EdgeInsets.all(20.0),
                sliver: SliverGrid.count(
                  crossAxisSpacing: 10.0,
                  crossAxisCount: 2,
                  children: List.generate(20, (index) {
                    return Card(child: Image.network("https://robohash.org/$index"));
                  }),
                ),
              ),
            ],
          )
        );
      }
    }
    
    GridView.count(
      crossAxisCount: 2,
      children: <Widget>[
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
      ],
    )
    
    GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      itemBuilder: (_, index) => FlutterLogo(),
      itemCount: 4,
    )
    
    GridView(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      children: <Widget>[
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
      ],
    )
    
    GridView.custom(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      childrenDelegate: SliverChildListDelegate(
        [
          FlutterLogo(),
          FlutterLogo(),
          FlutterLogo(),
          FlutterLogo(),
        ],
      ),
    )
    
    GridView.extent(
      maxCrossAxisExtent: 400,
      children: <Widget>[
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
        FlutterLogo(),
      ],
    )