Flutter GridView显示随机单词效果
更新时间:2022年03月23日 11:17:58 作者:伟雪无痕
这篇文章主要为大家详细介绍了Flutter GridView显示随机单词效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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自动生成dimens适配文件的图文教程详解(无需Java工具类)
这篇文章主要介绍了android自动生成dimens适配文件,无需Java工具类,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-03-03Android开发中DatePicker日期与时间控件实例代码
本文通过实例代码给大家介绍了Android开发中DatePicker日期与时间控件,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-08-08Android SwipeRefreshLayout仿抖音app静态刷新
这篇文章主要为大家详细介绍了Android SwipeRefreshLayout仿抖音app静态刷新,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-03-03解析Android中View转换为Bitmap及getDrawingCache=null的解决方法
在android中经常会遇到View转换为Bitmap的情形,本篇文章主要介绍了Android中View转换为Bitmap及getDrawingCache=null的解决方法,有需要的可以了解一下。2016-11-11
最新评论