当前位置:网站首页>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.dart
List 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
);
}
}
边栏推荐
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Buuctf, web:[geek challenge 2019] buyflag
- [untitled]
- Teach you how to run two or more MySQL databases at the same time in one system
- ADB related commands
- Introduction to the gtid mode of MySQL master-slave replication
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- Ansible common usage scenarios
- The difference between SRAM and DRAM
- Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
猜你喜欢
QT creator source code learning note 05, how does the menu bar realize plug-in?
Hcip day 16 notes
1 Introduction to spark Foundation
How can enterprises and developers take advantage of the explosion of cloud native landing?
Harbor integrated LDAP authentication
The reason why the computer runs slowly and how to solve it
The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
Overview of Yunxi database executor
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
How to understand the gain bandwidth product operational amplifier gain
随机推荐
Buuctf, misc: n solutions
JarPath
How to solve win10 black screen with only mouse arrow
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
webAssembly
Flutter internationalized Intl
Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
In 2022, 6G development has indeed warmed up
Text replacement demo
33 restrict the input of qlineedit control (verifier)
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
Sort merge sort
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
How to understand the gain bandwidth product operational amplifier gain
Summary of fluent systemchrome
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
Common mode interference of EMC
Leetcode: a single element in an ordered array