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;
? ? }
}