Skip to main content

Form File

1

Image

import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
File? file;
final allowedExtensions = ['pdf', 'doc', 'docx'];
final fileController = TextEditingController();

void pickFile() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: allowedExtensions,
);
if (result == null) return;
file = File(result.files.single.path!);
fileController.text = result.files.single.name;
}
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: fileController,
style: const TextStyle(
fontSize: 16,
color: Colors.black87,
),
readOnly: true,
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.only(right: 12),
child: FilledButton(
onPressed: pickFile,
style: const ButtonStyle(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
padding: WidgetStatePropertyAll(
EdgeInsets.symmetric(
vertical: 12,
horizontal: 20,
),
),
shape: WidgetStatePropertyAll(
RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16),
bottomLeft: Radius.circular(16),
),
),
),
),
child: const Text('Choose'),
),
),
contentPadding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 12,
),
filled: true,
fillColor: Colors.white,
isDense: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(color: Theme.of(context).primaryColor),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(color: Theme.of(context).primaryColor),
),
),
),
const Gap(8),
Text(
allowedExtensions.join(', '),
style: const TextStyle(
fontWeight: FontWeight.w400,
fontSize: 14,
),
),
],
),