Flutter?GridView顯示隨機(jī)單詞效果
本文實(shí)例為大家分享了Flutter GridView顯示隨機(jī)單詞的具體代碼,供大家參考,具體內(nèi)容如下
一.Flutter GridView屬性及構(gòu)建方法介紹
1.SliverGridDelegateWithFixedCrossAxisCount構(gòu)建固定數(shù)量的Widget
gridDelegate = SliverGridDelegateWithFixedCrossAxisCount( ? ? ? ? ?crossAxisCount: crossAxisCount, //設(shè)置每行個(gè)數(shù) ? ? ? ? ?mainAxisSpacing: mainAxisSpacing, //設(shè)置上下間隙 ? ? ? ? ?crossAxisSpacing: crossAxisSpacing, //設(shè)置水平間隙 ? ? ? ? ?childAspectRatio: childAspectRatio, //來(lái)設(shè)置寬高比例 ? ? ? ?),
1).GridView.custom構(gòu)建
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構(gòu)建
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屬性及構(gòu)建
GridView.count({
? ? Key? key,
? ? Axis scrollDirection = Axis.vertical, //設(shè)置滾動(dòng)方向
? ? bool reverse = false,
? ? ScrollController? controller,
? ? bool? primary,
? ? ScrollPhysics? physics,
? ? bool shrinkWrap = false,
? ? EdgeInsetsGeometry? padding,
? ? required int crossAxisCount, //設(shè)置每行個(gè)數(shù)
? ? double mainAxisSpacing = 0.0,//設(shè)置上下間隙
? ? double crossAxisSpacing = 0.0, //設(shè)置水平間隙
? ? double childAspectRatio = 1.0, //來(lái)設(shè)置寬高比例
? ? 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內(nèi)邊距 ? ? ? ? crossAxisCount: 2,//行的Widget數(shù)量 ? ? ? ? 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內(nèi)邊距
? ? ? ? crossAxisCount: 2,//行的Widget數(shù)量
? ? ? ? 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,
? ? );
? }
?
}三.實(shí)際效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例詳解android studio如何導(dǎo)入.so文件的方法
通過(guò)實(shí)例給大家詳細(xì)講解了如何在android studio如何導(dǎo)入.so文件以及中間遇到的問(wèn)題解決辦法,需要的讀者們可以仔細(xì)學(xué)習(xí)一下。2017-12-12
AndroidStudio升級(jí)到3.0的新特性和注意事項(xiàng)小結(jié)
這篇文章主要介紹了AndroidStudio升級(jí)到3.0的新特性和注意事項(xiàng),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
android音樂(lè)播放簡(jiǎn)單實(shí)現(xiàn)的簡(jiǎn)單示例(MediaPlayer)
本篇文章主要介紹了android音樂(lè)播放簡(jiǎn)單實(shí)現(xiàn)的簡(jiǎn)單示例(MediaPlayer),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
深入android中The connection to adb is
本篇文章是對(duì)android中The connection to adb is down的問(wèn)題以及解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android 中 viewpager 滑動(dòng)指示器的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了android 中 viewpager 滑動(dòng)指示器,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Android圓形旋轉(zhuǎn)菜單開(kāi)發(fā)實(shí)例
本文給大家分享一個(gè)動(dòng)畫(huà)菜單,基于android開(kāi)發(fā)圓形旋轉(zhuǎn)菜單案例,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
Andriod開(kāi)發(fā)中引入jar包的正確方式介紹
andriod中如果引入jar包的方式不對(duì)就會(huì)出現(xiàn)一些奇怪的錯(cuò)誤,用了很長(zhǎng)時(shí)間才解決出現(xiàn)的bug,下面與大家分享下正確引入jar包的方式2013-06-06
Android 微信小視頻錄制功能實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Android 微信小視頻錄制功能實(shí)現(xiàn)詳解的相關(guān)資料,這里提供了具體的實(shí)現(xiàn)思路及代碼,需要的朋友可以參考下2016-11-11

