-
-
Save codedstrings/a54135317025c60c0500c7d5da784d4f to your computer and use it in GitHub Desktop.
ListView Demo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| visualDensity: VisualDensity.adaptivePlatformDensity, | |
| ), | |
| home: ListViewDemo(), | |
| ); | |
| } | |
| } | |
| class ListViewDemo extends StatefulWidget { | |
| @override | |
| _ListViewDemoState createState() => _ListViewDemoState(); | |
| } | |
| class _ListViewDemoState extends State<ListViewDemo> { | |
| var _items = List<ListItem>(); | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _items.add(ListItem("Title 1", "Message 1")); | |
| _items.add(ListItem("Title 2", "Message 2")); | |
| _items.add(ListItem("Title 3", "Message 3")); | |
| _items.add(ListItem("Title 4", "Message 4")); | |
| _items.add(ListItem("Title 5", "Message 5")); | |
| _items.add(ListItem("Title 6", "Message 6")); | |
| _items.add(ListItem("Title 7", "Message 7")); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text("ListView Demo"), | |
| ), | |
| body: Column( | |
| children: [ | |
| Expanded( | |
| child: Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: ListView.builder( | |
| itemBuilder: (BuildContext context, int index) { | |
| return listItem(_items[index].title, _items[index].message); | |
| }, | |
| itemCount: _items.length, | |
| ), | |
| ), | |
| ), | |
| Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: SizedBox( | |
| height: 200, | |
| child: ListView.builder( | |
| itemBuilder: (BuildContext context, int index) { | |
| return listItem(_items[index].title, _items[index].message); | |
| }, | |
| itemCount: _items.length, | |
| reverse: true, | |
| scrollDirection: Axis.horizontal, | |
| ), | |
| ), | |
| ) | |
| ], | |
| )); | |
| } | |
| Widget listItem(String title, String message) { | |
| return Card( | |
| elevation: 8, | |
| child: SizedBox( | |
| height: 200, | |
| width: 250, | |
| child: Center( | |
| child: Column( | |
| mainAxisSize: MainAxisSize.min, | |
| children: [ | |
| Text( | |
| title, | |
| style: TextStyle(fontWeight: FontWeight.bold), | |
| ), | |
| Text(message), | |
| ], | |
| ))), | |
| ); | |
| } | |
| } | |
| class ListItem { | |
| String title; | |
| String message; | |
| ListItem(this.title, this.message); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment