在 flutter 列表视图中显示选取的文件
Showing picked files in list view in flutter
我正在研究 flutter。当我选择一个新文件时,我实现了 filePicker() 来选择文件并在 listView.But 中显示它们,listview 删除以前的文件并仅显示新文件。代码附在下面。如果有人帮助我很高兴。
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
class pickFile extends StatefulWidget {
const pickFile({Key? key}) : super(key: key);
@override
State<pickFile> createState() => _pickFileState();
}
class _pickFileState extends State<pickFile> {
List<PlatformFile> files = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add"),
actions: [
IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
final result = await FilePicker.platform
.pickFiles(withReadStream: true, allowMultiple: true);
if (result == null) return;
files = result.files;
setState(() {});
},
),
],
),
body: Container(
color: Colors.blue,
width: 420,
height: 200,
child: show(files: files),
),
);}}
/////////////////////////////////////////// /////////////
import 'package:file_picker/src/platform_file.dart';
import 'package:flutter/material.dart';
Widget show({
required List<PlatformFile> files,
}) {
return ListView.builder(
itemCount: files.length,
itemBuilder: (context, index) {
final file = files[index];
return buildFile(file);
},
);}
Widget buildFile(PlatformFile file) {
final kb = file.size / 1024;
final mb = kb / 1024;
final size =
(mb >= 1) ? '${mb.toStringAsFixed(2)} MB' : '${kb.toStringAsFixed(2)} KB';
return Container(
color: Colors.amber,
child: InkWell(
onTap: () => null,
child: Container(
height: 100,
width: 200,
color: Colors.red,
child: ListTile(
leading: (file.extension == 'jpg' || file.extension == 'png')
? Image.file(
File(file.path.toString()),
width: 80,
height: 80,
)
: Container(
width: 80,
height: 80,
),
title: Text('${file.name}'),
subtitle: Text('${file.extension}'),
trailing: Text(
'$size',
style: TextStyle(fontWeight: FontWeight.w700),
),
),
),
),);}
那是因为你每次添加文件时都会为列表设置一个新值
并删除旧文件
,
您需要像下面的代码一样将文件添加到现有列表中
onPressed: () async {
final result = await FilePicker.platform
.pickFiles(withReadStream: true, allowMultiple: true);
if (result == null) return;
files.add(result.files);
setState(() {});
},
我正在研究 flutter。当我选择一个新文件时,我实现了 filePicker() 来选择文件并在 listView.But 中显示它们,listview 删除以前的文件并仅显示新文件。代码附在下面。如果有人帮助我很高兴。
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
class pickFile extends StatefulWidget {
const pickFile({Key? key}) : super(key: key);
@override
State<pickFile> createState() => _pickFileState();
}
class _pickFileState extends State<pickFile> {
List<PlatformFile> files = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add"),
actions: [
IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
final result = await FilePicker.platform
.pickFiles(withReadStream: true, allowMultiple: true);
if (result == null) return;
files = result.files;
setState(() {});
},
),
],
),
body: Container(
color: Colors.blue,
width: 420,
height: 200,
child: show(files: files),
),
);}}
/////////////////////////////////////////// /////////////
import 'package:file_picker/src/platform_file.dart';
import 'package:flutter/material.dart';
Widget show({
required List<PlatformFile> files,
}) {
return ListView.builder(
itemCount: files.length,
itemBuilder: (context, index) {
final file = files[index];
return buildFile(file);
},
);}
Widget buildFile(PlatformFile file) {
final kb = file.size / 1024;
final mb = kb / 1024;
final size =
(mb >= 1) ? '${mb.toStringAsFixed(2)} MB' : '${kb.toStringAsFixed(2)} KB';
return Container(
color: Colors.amber,
child: InkWell(
onTap: () => null,
child: Container(
height: 100,
width: 200,
color: Colors.red,
child: ListTile(
leading: (file.extension == 'jpg' || file.extension == 'png')
? Image.file(
File(file.path.toString()),
width: 80,
height: 80,
)
: Container(
width: 80,
height: 80,
),
title: Text('${file.name}'),
subtitle: Text('${file.extension}'),
trailing: Text(
'$size',
style: TextStyle(fontWeight: FontWeight.w700),
),
),
),
),);}
那是因为你每次添加文件时都会为列表设置一个新值 并删除旧文件
, 您需要像下面的代码一样将文件添加到现有列表中
onPressed: () async {
final result = await FilePicker.platform
.pickFiles(withReadStream: true, allowMultiple: true);
if (result == null) return;
files.add(result.files);
setState(() {});
},