欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Flutter GridView显示随机单词效果

时间:2022-07-09 09:35:40|栏目:Android代码|点击:

本文实例为大家分享了Flutter GridView显示随机单词的具体代码,供大家参考,具体内容如下

一.Flutter GridView属性及构建方法介绍

1.SliverGridDelegateWithFixedCrossAxisCount构建固定数量的Widget

gridDelegate = SliverGridDelegateWithFixedCrossAxisCount(
         crossAxisCount: crossAxisCount, //设置每行个数
         mainAxisSpacing: mainAxisSpacing, //设置上下间隙
         crossAxisSpacing: crossAxisSpacing, //设置水平间隙
         childAspectRatio: childAspectRatio, //来设置宽高比例
       ),

1).GridView.custom构建

GridView.custom(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, mainAxisSpacing: 10.0, crossAxisSpacing: 20.0, ),
          childrenDelegate: SliverChildBuilderDelegate((context, position) {
            return getItemContainer(datas[position].asPascalCase);
          }, childCount: datas.length))

2).GridView.builder构建

GridView.builder(
          itemCount: datas.length,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 20.0,
              crossAxisSpacing: 10.0,
              childAspectRatio: 1.0),
              itemBuilder: (BuildContext context, int index) {
                return getItemContainer(datas[index]);
          }),

2.  GridView.count属性及构建

GridView.count({
    Key? key,
    Axis scrollDirection = Axis.vertical, //设置滚动方向
    bool reverse = false,
    ScrollController? controller,
    bool? primary,
    ScrollPhysics? physics,
    bool shrinkWrap = false,
    EdgeInsetsGeometry? padding,
    required int crossAxisCount, //设置每行个数
    double mainAxisSpacing = 0.0,//设置上下间隙
    double crossAxisSpacing = 0.0, //设置水平间隙
    double childAspectRatio = 1.0, //来设置宽高比例
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
    double? cacheExtent,
    List<Widget> children = const <Widget>[],
    int? semanticChildCount,
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
    ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
    String? restorationId,
    Clip clipBehavior = Clip.hardEdge,
  })
GridView.count(
        crossAxisSpacing: 10.0,//水平子Widget之间间距
        mainAxisSpacing: 20.0,//垂直子Widget之间间距
        padding: const EdgeInsets.all(10.0),//GridView内边距
        crossAxisCount: 2,//行的Widget数量
        childAspectRatio: 2.0,//子Widget宽高比例
        children: getWidgetList()//子Widget列表
      ),

二.demo展示

import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
 
class GridViewPage extends StatefulWidget{
  const GridViewPage({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState()=>GridViewPageStae();
 
}
 
class GridViewPageStae extends State<GridViewPage>{
  @override
  Widget build(BuildContext context) {
    //final _suggestions=<WordPair>[];
    List<WordPair> datas =generateWordPairs().take(100).toList();
    return Scaffold(
      appBar: AppBar(
        title: Text('Gridview test'),
      ),
      body:/*GridView.custom(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, mainAxisSpacing: 10.0, crossAxisSpacing: 20.0, ),
          childrenDelegate: SliverChildBuilderDelegate((context, position) {
            return getItemContainer(datas[position].asPascalCase);
          }, childCount: datas.length))*/
     /* GridView.count(
        crossAxisSpacing: 10.0,//水平子Widget之间间距
        mainAxisSpacing: 20.0,//垂直子Widget之间间距
        padding: const EdgeInsets.all(10.0),//GridView内边距
        crossAxisCount: 2,//行的Widget数量
        childAspectRatio: 2.0,//子Widget宽高比例
        children: getWidgetList()//子Widget列表
      ),*/
      GridView.builder(
          itemCount: datas.length,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 20.0,
              crossAxisSpacing: 10.0,
              childAspectRatio: 1.0),
              itemBuilder: (BuildContext context, int index) {
                return getItemContainer(datas[index]);
          }),
    );
 
  }
 
  List<WordPair> getDataList() {
    List<WordPair> datas =generateWordPairs().take(100).toList();
    return datas;
  }
 
  List<Widget> getWidgetList() {
    return getDataList().map((item) => getItemContainer(item)).toList();
  }
 
  Widget getItemContainer(WordPair item) {
    return Container(
      alignment: Alignment.center,
      //width: 50.0,
      //height: 50.0,
      child: Text(
        item.asPascalCase,
        style: const TextStyle(color: Colors.white, fontSize: 18),
      ),
      color: Colors.lightGreen,
    );
  }
 
}

三.实际效果

上一篇:Android自定义样式圆角dialog对话框

栏    目:Android代码

下一篇:Android ViewBinding的使用详解

本文标题:Flutter GridView显示随机单词效果

本文地址:http://www.codeinn.net/misctech/207259.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有