Last active
June 1, 2020 19:16
-
-
Save giuliano-macedo/319e4002dab25032354b2f03f65fe28c to your computer and use it in GitHub Desktop.
A String path form field for the file_picker package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:file_picker/file_picker.dart'; | |
import 'package:flutter/material.dart'; | |
class FilePickerFormField extends StatelessWidget { | |
final Function(String) validator; | |
final Function(String) onSaved; | |
final String labelText; | |
final String hintText; | |
final List<String> allowedExtensions; | |
const FilePickerFormField( | |
{Key key, | |
this.validator, | |
this.onSaved, | |
this.labelText, | |
this.hintText, | |
this.allowedExtensions}) | |
: super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return FormField<String>( | |
initialValue: "", | |
validator: validator, | |
onSaved: onSaved, | |
builder: (FormFieldState<String> state) => InkWell( | |
onTap: () => FilePicker.getFile( | |
allowedExtensions: allowedExtensions, | |
type: FileType.custom, | |
).then((File f) { | |
if (f == null) return; | |
state.didChange(f.path); | |
state.save(); | |
}), | |
child: InputDecorator( | |
decoration: InputDecoration( | |
icon: Icon(Icons.folder), | |
labelText: state.value.isNotEmpty ? labelText : hintText, | |
errorText: state.hasError ? state.errorText : null, | |
), | |
isEmpty: state.value.isEmpty, | |
child: state.value.isEmpty ? Container() : Text(state.value), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment