当前位置:网站首页>Fluent learning (4) listview
Fluent learning (4) listview
2022-07-03 23:04:00 【Sleep in the wind】
flutter Study (4)ListView
List of articles
One . Basic list components
- Vertical list
- Vertical text list
- Horizontal list
- Dynamic list
- Matrix list
Two . List parameters


3、 ... and . Vertical list
Basic list
import 'package:flutter/material.dart'; import 'package:testforgramma/main_bk2.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FltterDemo')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { @override Widget build(BuildContext context) { return ListView( children: <Widget>[ ListTile( title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), ) ], ); } }
Use the shortcut key of extracting code just learned hey
Draw it out

import 'package:flutter/material.dart'; import 'package:testforgramma/main_bk2.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FltterDemo')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { // const HomeContent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ListView( children: <Widget>[ buildListTile(), buildListTile(), buildListTile(), buildListTile(), ], ); } ListTile buildListTile() { return const ListTile( title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), // Secondary title ); } }
ListTile Icon can be added
leading attribute
ListTile buildListTile() { return const ListTile( leading: Icon(Icons.search, color: Colors.blue), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), // Secondary title ); } }
Play more icons
leading It's the icon at the front
trailing Is the icon behind
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FltterDemo')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { // const HomeContent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ListView( children: const <Widget>[ ListTile( leading: Icon(Icons.search, color: Colors.blue, size: 40), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18))), // Secondary title ListTile( leading: Icon(Icons.ac_unit, color: Colors.blue), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), trailing: Icon(Icons.ac_unit, color: Colors.blue), ), // Secondary title ListTile( leading: Icon(Icons.home_max, color: Colors.blue), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), trailing: Icon(Icons.accessibility_new_outlined, color: Colors.blue), ), ], ); } }
leading It can also be a remote picture
class HomeContent extends StatelessWidget { // const HomeContent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ListView( children: <Widget>[ ListTile( leading: Image.network( "https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7"), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18))), // Secondary title ListTile( leading: Icon(Icons.ac_unit, color: Colors.blue), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), trailing: Icon(Icons.ac_unit, color: Colors.blue), ), // Secondary title ListTile( leading: Icon(Icons.home_max, color: Colors.blue), title: Text(" The Winter Olympics ", style: TextStyle(fontSize: 24)), subtitle: Text("2022 The Beijing Winter Olympics went smoothly ", style: TextStyle(fontSize: 18)), trailing: Icon(Icons.accessibility_new_outlined, color: Colors.blue), ), ], ); } }Loaded this web image , You can't const 了

Graphic list
Intermediate use Container Artificially enlarge the picture spacing
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FltterDemo')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { // const HomeContent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ListView( padding: EdgeInsets.all(10), children: <Widget>[ Image.network( "https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7", colorBlendMode: BlendMode.colorBurn, color: Colors.blue, ), Container( child: Text(' The title is here ', textAlign: TextAlign.center, style: TextStyle(fontSize: 28)), height: 40), Image.network( "https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7", colorBlendMode: BlendMode.colorDodge, color: Colors.cyan, ), Image.network( "https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7", colorBlendMode: BlendMode.darken, color: Colors.blueAccent, ), Image.network( "https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7"), ], ); } }
Four . Horizontal list
from scrollDirection Property to determine whether the list is horizontal or vertical
Horizontal list height It's adaptive
Vertical list width It's adaptive
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView(
scrollDirection: Axis.horizontal,
padding: EdgeInsets.all(10),
children: <Widget>[
Container(height: 100, width: 100, color: Colors.deepOrange),
Container(height: 100, width: 100, color: Colors.deepPurpleAccent),
Container(height: 100, width: 100, color: Colors.cyanAccent)
],
);
}
}

In the Container It can limit its height
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('FltterDemo')),
body: HomeContent(),
));
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 80,
child: ListView(
scrollDirection: Axis.horizontal,
padding: EdgeInsets.all(10),
children: <Widget>[
Container(height: 100, width: 100, color: Colors.deepOrange),
Container(height: 100, width: 100, color: Colors.deepPurpleAccent),
Container(height: 100, width: 100, color: Colors.cyanAccent)
],
));
}
}

class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 480,
child: ListView(
scrollDirection: Axis.horizontal,
padding: EdgeInsets.all(10),
children: <Widget>[
Container(height: 100, width: 100, color: Colors.deepOrange),
Container(
height: 100,
width: 100,
color: Colors.deepPurpleAccent,
child: ListView(
children: <Widget>[
Image.network(
"https://tse4-mm.cn.bing.net/th/id/OIP-C.Hi8-77RG-95MCRQF9gaFxgHaHI?w=214&h=206&c=7&r=0&o=5&dpr=1.38&pid=1.7",
colorBlendMode: BlendMode.colorDodge,
color: Colors.cyan,
),
Text(" The Winter Olympics ")
],
)),
Container(height: 100, width: 100, color: Colors.cyanAccent)
],
));
}
}

5、 ... and . Dynamic list
The previous ones are all static lists
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Flutter list ')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { const HomeContent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ListView( children: <Widget>[ ListTile( tileColor: Colors.cyanAccent, leading: CircleAvatar( backgroundImage: NetworkImage( "https://pic.52112.com/2019/12/23/EPS-191223_124/gBShjDAniH_small.jpg"), ), title: Text( 'Horse', textAlign: TextAlign.end, ), ), Text("1", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("2", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("3", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("4", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), ], ); } }
for Loop traversal
Loop through the data in an array to the page is the loop array
First return the list
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Flutter list ')), body: HomeContent(), )); } } class HomeContent extends StatelessWidget { const HomeContent({Key? key}) : super(key: key); List<Widget> _getData() { return [ ListTile( tileColor: Colors.cyanAccent, leading: CircleAvatar( backgroundImage: NetworkImage( "https://pic.52112.com/2019/12/23/EPS-191223_124/gBShjDAniH_small.jpg"), ), title: Text( 'Horse', textAlign: TextAlign.end, ), ), ListTile( title: Text(" I'm a list "), ), ListTile(title: Text(" I am also a list ")), Text("1", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("2", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("3", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), Text("4", textAlign: TextAlign.center, style: TextStyle(fontSize: 24)), ]; } @override Widget build(BuildContext context) { return ListView( children: this._getData(), ); } }
Loop list
class HomeContent extends StatelessWidget { List<Widget> list = List.empty(growable: true); List<Widget> _getData() { for (var i = 0; i < 20; i++) { list.add(ListTile(title: Text(" The list of the first $i That's ok "))); } return list; } @override Widget build(BuildContext context) { return ListView( children: this._getData(), ); } }Problems encountered :
List<Widget> list = new List();Report errors :
The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'.Solution reference :

Get data from the server
stay lib New in the folder res Folder , newly build
listData.dartList listData=[ { "title": 'Candy Shop', "author": 'Mohamed Chahin', "imageUrl": 'https://www.itying.com/images/flutter/1.png', }, { "title": 'Childhood in a picture', "author": 'Google', "imageUrl": 'https://www.itying.com/images/flutter/2.png', }, { "title": 'Alibaba Shop', "author": 'Alibaba', "imageUrl": 'https://www.itying.com/images/flutter/3.png', }, { "title": 'Candy Shop', "author": 'Mohamed Chahin', "imageUrl": 'https://www.itying.com/images/flutter/4.png', }, { "title": 'Tornado', "author": 'Mohamed Chahin', "imageUrl": 'https://www.itying.com/images/flutter/5.png', }, { "title": 'Undo', "author": 'Mohamed Chahin', "imageUrl": 'https://www.itying.com/images/flutter/6.png', }, { "title": 'white-dragon', "author": 'Mohamed Chahin', "imageUrl": 'https://www.itying.com/images/flutter/7.png', } ];stay main.dart Introduced inside
import './res/listData.dart';Modify what just happened
List <Widget> _getdata()MethodList<Widget> _getData() { var tempList = listData.map((value) { return ListTile(title: Text(value["title"])); }); //map Method to traverse each item in the list return tempList.toList(); }- effect

Perfect that dynamic list
List<Widget> _getData() { var tempList = listData.map( (value) { return ListTile( leading: Image.network(value["imageUrl"]), title: Text(value["title"]), subtitle: Text(value["author"]), ); } ); return tempList.tolist(); }effect

ListView.builder Realize the cycle
Normal list
import 'package:flutter/material.dart';
import './res/listData.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Flutter list ')), body: HomeContent()));
}
}
class HomeContent extends StatelessWidget {
List list_2 = [];
// Constructor to list Add some data
HomeContent() {
for (var i = 0; i < 20; i++) {
this.list_2.add(' I am the first $i strip ');
}
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: this.list_2.length,
// Specify the length
itemBuilder: (context, index) {
return ListTile(title: Text(this.list_2[index]));
// Each item returns the component
},
);
}
}
Just that listData.dart The data in it
class HomeContent extends StatelessWidget {
@override
ListTile _getListData(context, index) {
return ListTile(
leading: Image.network(listData[index]["imageUrl"]),
title: Text(listData[index]["title"]),
subtitle: Text(listData[index]["author"]));
// Each item returns the component
}
Widget build(BuildContext context) {
return ListView.builder(
itemCount: listData.length,
// Specify the length
itemBuilder: this._getListData // assignment
);
}
}
边栏推荐
- Common mode interference of EMC
- Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
- The difference between single power amplifier and dual power amplifier
- 3 environment construction -standalone
- Can you draw with turtle?
- QT creator source code learning note 05, how does the menu bar realize plug-in?
- How to quickly build high availability of service discovery
- Quick one click batch adding video text watermark and modifying video size simple tutorial
- [template summary] - binary search tree BST - Basics
- Hcip day 15 notes
猜你喜欢

Unity shader visualizer shader graph

How to quickly build high availability of service discovery

string

Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)

Opengauss database log management guide

Hcip day 15 notes

QGIS grid processing DEM data reclassification

The difference between single power amplifier and dual power amplifier

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Qtoolbutton - menu and popup mode
随机推荐
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Teach you how to run two or more MySQL databases at the same time in one system
BUUCTF,Misc:LSB
Opengauss database log management guide
Mindmanager2022 serial number key decompression installer tutorial
AST (Abstract Syntax Tree)
Unity shader visualizer shader graph
How about agricultural futures?
2022.02.14
Pyqt5 sensitive word detection tool production, operator's Gospel
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
X Opencv feature point detection and matching
Summary of basic knowledge of exception handling
LeetCode 1647. Minimum deletion times of unique character frequency
[sg function]split game (2020 Jiangxi university student programming competition)
Some 5000+ likes, the development notes of a director of cosmic factory, leaked
Buuctf, misc: sniffed traffic
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)