import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(title: 'contaniner', home: HomePage());
}
}
//创建有状态组件
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//创建集合
final List _dataList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => {
//添加数据
setState(() => _dataList.add({'name': '小刀刀'}))
},
tooltip: '添加数据',
child: const Icon(Icons.add),
),
appBar: AppBar(
title: const Center(child: Text('flutter bar')),
),
//带索引的集合循环
body: ListView(
physics:const BouncingScrollPhysics(),
children: _dataList
.asMap()
.entries
.map((item) =>
ListTile(title: Text('${item.value}${item.key}')))
.toList()),
);
}
}
flutter动态集合
于 2024-03-20 17:09:01 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)