Skip to content

Instantly share code, notes, and snippets.

@iapicca
Last active March 22, 2023 14:15
Show Gist options
  • Save iapicca/222c792502d2a049fa957a6c9b4f5a0e to your computer and use it in GitHub Desktop.
Save iapicca/222c792502d2a049fa957a6c9b4f5a0e to your computer and use it in GitHub Desktop.
SingleChildScrollView VS ListView.builder
import 'package:flutter/material.dart';
void main() => runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
),
);
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
static const verticalCount = 200;
static const horizontalCount = 10;
@override
Widget build(context) => DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Sample'),
bottom: const TabBar(
tabs: [
Tab(icon: Text('SingleChildScrollView')),
Tab(icon: Text('Builder')),
],
),
),
body: TabBarView(
children: [
SingleChildScrollView(
child: Row(
children: [
for (var i = 0; i < horizontalCount; ++i)
Column(
children: [
for (int j = 0; j < verticalCount; j++)
ItemWidget(index: j),
],
)
],
),
),
ListView.builder(
itemCount: verticalCount,
itemBuilder: (context, index) => Row(
children: [
for (int i = 0; i < horizontalCount; i++)
ItemWidget(index: index),
],
),
),
],
),
),
);
}
class ItemWidget extends StatelessWidget {
final double height;
final double width;
final int index;
const ItemWidget({
super.key,
this.width = 70,
this.height = 50,
required this.index,
});
@override
Widget build(context) => SizedBox(
height: height,
width: width,
child: DecoratedBox(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: Text('$index'),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment