File upload
A file input lets someone select a document from their device. Use a label that says what the attachment is for.
Attach a file
#Choose a local file. Its name appears in the preview. The example reads only the file’s name and size and keeps the selection in this browser tab.
components/file-upload/appliance/attach_file.dart
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
import 'package:appliance_ui/appliance_ui.dart';
class AttachFile extends StatefulWidget {
const AttachFile({super.key});
@override
State<AttachFile> createState() => _AttachFileState();
}
class _AttachFileState extends State<AttachFile> {
web.File? attachment;
void chooseFile() {
final input = web.HTMLInputElement()..type = 'file';
input.onChange.first.then((_) {
final file = input.files?.item(0);
if (mounted && file != null) setState(() => attachment = file);
});
input.click();
}
@override
Widget build(BuildContext context) => ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ApplianceDropZone(label: 'Attachment', onPressed: chooseFile),
const SizedBox(height: 12),
Text(attachment?.name ?? 'No file selected'),
],
),
),
);
}
Parameters
ApplianceDropZone API ↗label- Identifies the attachment field.
onPressed- Opens the browser’s file chooser.
Shared behaviour
- Keyboard and focus. Tab to the choose action and activate it. The system file chooser handles keyboard navigation until you pick a file or cancel.
- Screen readers. Read the attachment label before opening the chooser, then locate the selected file name in the example.
- Direction and type. Try a long file name at phone width and 200% text. Its letters keep their own order when the surrounding layout switches to RTL.