fluttrer 列表學(xué)習(xí)

import 'dart:math';

import 'package:flutter/material.dart';

void main() {

? runApp(const MyApp());

}

class MyApp extends StatelessWidget {

? const MyApp({super.key});

? @override

? Widget build(BuildContext context) {

? ? return MaterialApp(

? ? ? title: 'Flutter Demo',

? ? ? theme: ThemeData(

? ? ? ? primarySwatch: Colors.blue,

? ? ? ),

? ? ? home: const MyHomePage(title: 'Flutter Demo Home Page'),

? ? );

? }

}

class MyHomePage extends StatefulWidget {

? const MyHomePage({super.key, required this.title});

? final String title;

? @override

? State<MyHomePage> createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {

? int _counter = 0;

? List<String> items = List.generate(10, (int i) => '$i' + "10");

? List<String> items2 = List.generate(10, (int i) => '$i' + "1000");

? @override

? Widget build(BuildContext context) {

? ? return Scaffold(

? ? ? appBar: AppBar(

? ? ? ? // Here we take the value from the MyHomePage object that was created by

? ? ? ? // the App.build method, and use it to set our appbar title.

? ? ? ? title: Text(widget.title),

? ? ? ),

? ? ? body: _createListView2() , // This trailing comma makes auto-formatting nicer for build methods.

? ? );

? }

Widget _createListView2() {

? ? Widget widget = ListView.builder(itemBuilder: (context, index) {

? ? ? if (index == 0 || index == 2) {

? ? ? return Container(

? ? ? ? key: ValueKey("$index"),

? ? ? ? color: Colors.white,

? ? ? ? height: 80,

? ? ? ? width: double.infinity,

? ? ? ? child: Text("$index分類行",style: const TextStyle(color: Colors.black,fontSize: 20),),

? ? ? );

? ? ? } else {

? ? ? return Container(

? ? ? ? height: 56 * 10,

? ? ? ? child: _createReorderableListView(index == 1 ? 0 : 1),

? ? ? );

? ? ? }

? ? } , itemCount: 4);

? ? return widget;

? }


? Widget _createRenderTable() {

? ? ? ? Widget widget = CustomScrollView(

? ? ? ? slivers:[

? ? ? ? ? SliverReorderableList(itemBuilder: (context, index) {

? ? ? ? ? ? return Container(

? ? ? ? ? ? ? key: ValueKey("$index + w"),

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? height: 56,

? ? ? ? ? ? ? width: double.infinity,

? ? ? ? ? ? ? child: Text("第$index行",style: const TextStyle(color: Colors.red,fontSize: 20),),

? ? ? ? ? ? ? );

? ? ? ? ? ? ? }, itemCount: 5, onReorder: (int oldIndex, int newIndex) {

? ? ? ? ? ? ? ? if (newIndex > oldIndex) {

? ? ? ? ? ? ? ? ? newIndex -= 1;

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? }),

? ? ? ? ? ? ? ? ? SliverReorderableList(itemBuilder: (context, index) {

? ? ? ? ? ? return Container(

? ? ? ? ? ? ? key: ValueKey("$index + k"),

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? height: 56,

? ? ? ? ? ? ? width: double.infinity,

? ? ? ? ? ? ? child: Text("第$index行",style: const TextStyle(color: Colors.red,fontSize: 20),),

? ? ? ? ? ? ? );

? ? ? ? ? ? ? }, itemCount: 5, onReorder: (int oldIndex, int newIndex) {

? ? ? ? ? ? ? ? if (newIndex > oldIndex) {

? ? ? ? ? ? ? ? ? newIndex -= 1;

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? }),

? ? ? ? ],

? ? ? );

? ? ? return widget;

? }

? Widget _createReorderableListView(int type) {

? ? //SliverReorderableList

? ? Widget widget = ReorderableListView.builder(key:ValueKey('$type' + 't'), itemBuilder: (context, index) {

? ? ? return Container(

? ? ? ? key: type == 0 ? ValueKey(items[index]) : ValueKey(items2[index]),

? ? ? ? color: Colors.white,

? ? ? ? height: 56,

? ? ? ? width: double.infinity,

? ? ? ? child: Text("第${type == 0 ? items[index] : items2[index]}行",style: const TextStyle(color: Colors.red,fontSize: 20),),

? ? ? );

? ? } , itemCount: 10,

? ? onReorder: (int oldIndex, int newIndex) {

? ? ? ? //? if (newIndex > oldIndex) {

? ? ? ? //? ? newIndex -= 1;

? ? ? ? //? }

? ? ? ? ? print(oldIndex);

? ? ? ? ? print(newIndex);

? ? ? ? ? var element = (type == 0 ? items[oldIndex] : items2[oldIndex]);

? ? ? ? ? if (type == 0) {

? ? ? ? ? ? if (newIndex >= items.length) {

? ? ? ? ? ? ? newIndex = (items.length - 1);

? ? ? ? ? ? }

? ? ? ? ? } else {

? ? ? ? ? ? if (newIndex >= items2.length) {

? ? ? ? ? ? ? newIndex = (items2.length - 1);

? ? ? ? ? ? }

? ? ? ? ? }

? ? ? ? ? setState(() {

? ? ? ? ? ? if (type == 0) {

? ? ? ? ? ? items.removeAt(oldIndex);

? ? ? ? ? ? items.insert(newIndex, element);

? ? ? ? ? ? } else {

? ? ? ? ? ? ? items2.removeAt(oldIndex);

? ? ? ? ? ? ? items2.insert(newIndex, element);

? ? ? ? ? ? }

? ? ? ? ? });

? ? });

? ? return widget;

? }

? Widget _createListView() {

? ? Widget widget = ListView.builder(itemBuilder: (context, index) {

? ? ? return Container(

? ? ? ? key: ValueKey("$index"),

? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? height: 56,

? ? ? ? width: double.infinity,

? ? ? ? child: Text("第$index行",style: const TextStyle(color: Colors.red,fontSize: 20),),

? ? ? );

? ? } , itemCount: 5);

? ? return widget;

? }

? Widget _createMyScrollviewDemo03() {

? ? ? ? Widget widget = CustomScrollView(

? ? ? ? slivers:[

? ? ? ? ? SliverList(delegate: SliverChildBuilderDelegate(

? ? ? ? ? ? (BuildContext contetx, int index){

? ? ? ? ? ? ? return Container(

? ? ? ? ? ? ? ? height: 20,

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? );

? ? ? ? ? ? },

? ? ? ? ? ? childCount: 20,

? ? ? ? ? )),

? ? ? ? ? SliverList(delegate: SliverChildBuilderDelegate(

? ? ? ? ? ? (BuildContext contetx, int index){

? ? ? ? ? ? ? return Container(

? ? ? ? ? ? ? ? height: 20,

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? );

? ? ? ? ? ? },

? ? ? ? ? ? childCount: 20,

? ? ? ? ? )),

? ? ? ? ],

? ? ? );

? ? ? return widget;

? }

? Widget _createMyScrollviewDemo02() {

? ? ? ? Widget widget = CustomScrollView(

? ? ? ? slivers:[

? ? ? ? ? SliverGrid(delegate: SliverChildBuilderDelegate(

? ? ? ? ? ? // ignore: avoid_types_as_parameter_names, non_constant_identifier_names

? ? ? ? ? ? (BuilderContext, int)? {

? ? ? ? ? ? ? return Container(

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? );

? ? ? ? ? ? },

? ? ? ? ? ? childCount: 20,

? ? ? ? ? ),

? ? ? ? ? gridDelegate:? const SliverGridDelegateWithFixedCrossAxisCount(

? ? ? ? ? ? crossAxisCount: 2,

? ? ? ? ? ? crossAxisSpacing: 8,

? ? ? ? ? ? mainAxisSpacing: 8,

? ? ? ? ? ? childAspectRatio: 1.5

? ? ? ? ? ),),

? ? ? ? ? SliverList(delegate: SliverChildBuilderDelegate(

? ? ? ? ? ? (BuildContext contetx, int index){

? ? ? ? ? ? ? return Container(

? ? ? ? ? ? ? height: 56,

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? );

? ? ? ? ? ? },

? ? ? ? ? ? childCount: 20,

? ? ? ? ? )),

? ? ? ? ],

? ? ? );

? ? ? return widget;

? }

? Widget _createMyScrollviewDemo01() {

? ? Widget widget = CustomScrollView(

? ? ? ? slivers:[ _createSliverGirdle()],

? ? ? );

? ? ? return widget;

? ? }

? ? Widget _createSliverGirdle() {

? ? ? Widget widget =? SliverSafeArea(

? ? ? ? ? ? sliver: SliverPadding(padding:const EdgeInsets.only(top: 10),

? ? ? ? ? ? sliver: SliverGrid(delegate: SliverChildBuilderDelegate(

? ? ? ? ? ? // ignore: avoid_types_as_parameter_names, non_constant_identifier_names

? ? ? ? ? ? (BuilderContext, int)? {

? ? ? ? ? ? ? return Container(

? ? ? ? ? ? ? color: Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256),Random().nextInt(256)),

? ? ? ? ? ? ? );

? ? ? ? ? ? },

? ? ? ? ? ? childCount: 20,

? ? ? ? ? ),

? ? ? ? ? gridDelegate:? const SliverGridDelegateWithFixedCrossAxisCount(

? ? ? ? ? ? crossAxisCount: 2,

? ? ? ? ? ? crossAxisSpacing: 8,

? ? ? ? ? ? mainAxisSpacing: 8,

? ? ? ? ? ? childAspectRatio: 1.5

? ? ? ? ? ),)

? ? ? ? ? ));

? ? ? ? ? return widget;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末诫舅,一起剝皮案震驚了整個(gè)濱河市歇式,隨后出現(xiàn)的幾起案子亡脸,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件低匙,死亡現(xiàn)場(chǎng)離奇詭異蒲赂,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)寓搬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)县耽,“玉大人句喷,你說(shuō)我怎么就攤上這事⊥帽校” “怎么了唾琼?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)澎剥。 經(jīng)常有香客問(wèn)我锡溯,道長(zhǎng),這世上最難降的妖魔是什么哑姚? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任祭饭,我火速辦了婚禮,結(jié)果婚禮上叙量,老公的妹妹穿的比我還像新娘倡蝙。我一直安慰自己,他們只是感情好绞佩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布寺鸥。 她就那樣靜靜地躺著,像睡著了一般品山。 火紅的嫁衣襯著肌膚如雪胆建。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天肘交,我揣著相機(jī)與錄音笆载,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛宰译,可吹牛的內(nèi)容都是我干的檐蚜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼沿侈,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼闯第!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起缀拭,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤咳短,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后蛛淋,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體咙好,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年褐荷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了勾效。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡叛甫,死狀恐怖层宫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情其监,我是刑警寧澤萌腿,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站抖苦,受9級(jí)特大地震影響毁菱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜锌历,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一贮庞、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辩涝,春花似錦贸伐、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)脯丝。三九已至商膊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宠进,已是汗流浹背晕拆。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人实幕。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓吝镣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親昆庇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子末贾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容