text
stringlengths 1
474
|
|---|
),<code_end>
|
<topic_end>
|
<topic_start>
|
Interactive example
|
Desktop and web only:
|
This example works in the browser and on the desktop.
|
However, as this list scrolls on the horizontal axis
|
(left to right or right to left),
|
hold Shift while using the mouse scroll wheel to scroll the list.To learn more, read the breaking change page on the
|
default drag for scrolling devices.
|
<code_start>import 'package:flutter/material.dart';
|
void main() => runApp(const MyApp());
|
class MyApp extends StatelessWidget {
|
const MyApp({super.key});
|
@override
|
Widget build(BuildContext context) {
|
const title = 'Horizontal List';
|
return MaterialApp(
|
title: title,
|
home: Scaffold(
|
appBar: AppBar(
|
title: const Text(title),
|
),
|
body: Container(
|
margin: const EdgeInsets.symmetric(vertical: 20),
|
height: 200,
|
child: ListView(
|
// This next line does the trick.
|
scrollDirection: Axis.horizontal,
|
children: <Widget>[
|
Container(
|
width: 160,
|
color: Colors.red,
|
),
|
Container(
|
width: 160,
|
color: Colors.blue,
|
),
|
Container(
|
width: 160,
|
color: Colors.green,
|
),
|
Container(
|
width: 160,
|
color: Colors.yellow,
|
),
|
Container(
|
width: 160,
|
color: Colors.orange,
|
),
|
],
|
),
|
),
|
),
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>Create a grid list
|
In some cases, you might want to display your items as a grid rather than
|
a normal list of items that come one after the next.
|
For this task, use the GridView widget.The simplest way to get started using grids is by using the
|
GridView.count() constructor,
|
because it allows you to specify how many rows or columns you’d like.To visualize how GridView works,
|
generate a list of 100 widgets that display their index in the list.
|
<code_start>GridView.count(
|
// Create a grid with 2 columns. If you change the scrollDirection to
|
// horizontal, this produces 2 rows.
|
crossAxisCount: 2,
|
// Generate 100 widgets that display their index in the List.
|
children: List.generate(100, (index) {
|
return Center(
|
child: Text(
|
'Item $index',
|
style: Theme.of(context).textTheme.headlineSmall,
|
),
|
);
|
}),
|
),<code_end>
|
<topic_end>
|
<topic_start>
|
Interactive example
|
<code_start>import 'package:flutter/material.dart';
|
void main() {
|
runApp(const MyApp());
|
}
|
class MyApp extends StatelessWidget {
|
const MyApp({super.key});
|
@override
|
Widget build(BuildContext context) {
|
const title = 'Grid List';
|
return MaterialApp(
|
title: title,
|
home: Scaffold(
|
appBar: AppBar(
|
title: const Text(title),
|
),
|
body: GridView.count(
|
// Create a grid with 2 columns. If you change the scrollDirection to
|
// horizontal, this produces 2 rows.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.