Skip to content

Commit

Permalink
Merge pull request #380 from TheCarpetMerchant/master
Browse files Browse the repository at this point in the history
Allow disabling the upload of images taken from the device
  • Loading branch information
tneotia committed Mar 5, 2023
2 parents e998658 + 8e2b99c commit 90bf8a8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 60 deletions.
135 changes: 75 additions & 60 deletions lib/src/widgets/toolbar_widget.dart
Expand Up @@ -1943,64 +1943,77 @@ class ToolbarWidgetState extends State<ToolbarWidget> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Select from files',
style: TextStyle(
fontWeight: FontWeight.bold)),
SizedBox(height: 10),
TextFormField(
controller: filename,
readOnly: true,
decoration: InputDecoration(
prefixIcon: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context)
.dialogBackgroundColor,
padding: EdgeInsets.only(
left: 5, right: 5),
elevation: 0.0),
onPressed: () async {
result = await FilePicker.platform
.pickFiles(
type: FileType.image,
withData: true,
allowedExtensions: widget
.htmlToolbarOptions
.imageExtensions,
);
if (result?.files.single.name !=
null) {
setState(() {
filename.text =
result!.files.single.name;
});
}
},
child: Text('Choose image',
style: TextStyle(
color: Theme.of(context)
.textTheme
.bodyText1
?.color)),
),
suffixIcon: result != null
? IconButton(
icon: Icon(Icons.close),
onPressed: () {
setState(() {
result = null;
filename.text = '';
});
})
: Container(height: 0, width: 0),
errorText: validateFailed,
errorMaxLines: 2,
border: InputBorder.none,
)),
SizedBox(height: 20),
Text('URL',
style: TextStyle(
fontWeight: FontWeight.bold)),
SizedBox(height: 10),
if (widget
.htmlToolbarOptions.allowImagePicking)
Text('Select from files',
style: TextStyle(
fontWeight: FontWeight.bold)),
if (widget
.htmlToolbarOptions.allowImagePicking)
SizedBox(height: 10),
if (widget
.htmlToolbarOptions.allowImagePicking)
TextFormField(
controller: filename,
readOnly: true,
decoration: InputDecoration(
prefixIcon: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context)
.dialogBackgroundColor,
padding: EdgeInsets.only(
left: 5, right: 5),
elevation: 0.0),
onPressed: () async {
result = await FilePicker.platform
.pickFiles(
type: FileType.image,
withData: true,
allowedExtensions: widget
.htmlToolbarOptions
.imageExtensions,
);
if (result?.files.single.name !=
null) {
setState(() {
filename.text =
result!.files.single.name;
});
}
},
child: Text('Choose image',
style: TextStyle(
color: Theme.of(context)
.textTheme
.bodyText1
?.color)),
),
suffixIcon: result != null
? IconButton(
icon: Icon(Icons.close),
onPressed: () {
setState(() {
result = null;
filename.text = '';
});
})
: Container(height: 0, width: 0),
errorText: validateFailed,
errorMaxLines: 2,
border: InputBorder.none,
)),
if (widget
.htmlToolbarOptions.allowImagePicking)
SizedBox(height: 20),
if (widget
.htmlToolbarOptions.allowImagePicking)
Text('URL',
style: TextStyle(
fontWeight: FontWeight.bold)),
if (widget
.htmlToolbarOptions.allowImagePicking)
SizedBox(height: 10),
TextField(
controller: url,
focusNode: urlFocus,
Expand All @@ -2025,8 +2038,10 @@ class ToolbarWidgetState extends State<ToolbarWidget> {
if (filename.text.isEmpty &&
url.text.isEmpty) {
setState(() {
validateFailed =
'Please either choose an image or enter an image URL!';
validateFailed = widget.htmlToolbarOptions
.allowImagePicking
? 'Please either choose an image or enter an image URL!'
: 'Please enter an image URL!';
});
} else if (filename.text.isNotEmpty &&
url.text.isNotEmpty) {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/options.dart
Expand Up @@ -189,6 +189,7 @@ class HtmlToolbarOptions {
this.toolbarItemHeight = 36,
this.gridViewHorizontalSpacing = 5,
this.gridViewVerticalSpacing = 5,
this.allowImagePicking = true,
});

/// Allows you to set the allowed extensions when a user inserts an audio file
Expand Down Expand Up @@ -403,6 +404,10 @@ class HtmlToolbarOptions {
final Color? buttonSelectedBorderColor;
final BorderRadius? buttonBorderRadius;
final double? buttonBorderWidth;

/// Allow the user to choose an image from their device when image selection
/// is enabled. Inserting images via URL will still be possible if this is false.
final bool allowImagePicking;
}

/// Other options such as the height of the widget and the decoration surrounding it
Expand Down

0 comments on commit 90bf8a8

Please sign in to comment.