How to download local file in Flutter - flutter

I'm making my portfolio website on flutter i want that if someone click on download cv button,it download on his device.CV is place in the assets folder.
downloadFile(url) {
AnchorElement anchorElement =
AnchorElement(href: url);
anchorElement.download = "CV";
anchorElement.click();
}
TextButton(
onPressed: () =>
downloadFile("/assets/files/Hassan_Naveed_CV.pdf"),
child: FittedBox(
child: Row(
children: [
Text(
"Download CV",
style: TextStyle(
color: Theme.of(context)
.textTheme
.bodyText1!
.color),
),
const SizedBox(
width: defaultPadding / 2,
),
const Icon(Icons.download, color: Color(0xFF8B8B8D))
],
),
)),
when i click on the download button ,it shows download failed

Try checking the path of your file against where you call it and whether you have given access to that 'files' folder in your pubspec.yml. Your code looks good to me, I think it comes from the given path.
Also check that there are no spaces or unwanted characters in the file name.

Related

How to fix "Exception caught by image resource service"

I have created a resources folder to store my images.
part of 'resources.dart';
class IconImages {
IconImages._();
static const String test1 = 'assets/images/test1.jpg';
static const String test2 = 'assets/images/test2.jpg';
static const String test3 = 'assets/images/test3.jpg';
}
I write them to the Map collection
final imagesIcon = <int, String>{
1: IconImages.test1,
2: IconImages.test2,
3: IconImages.test3,
};
and then pass the recorded images to the button.
icon: Image(image: AssetImage(imagesIcon.values.toString())),
// full button
Column(
children: [
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
],
),
When I execute my code, I get the following exception
Everything works when I transmit the image in this form
icon: Image(image: AssetImage(IconImages.test2)),
But when I try to get a path from Map in this way, the above error is obtained
icon: Image(image: AssetImage(imagesIcon.values.toString())),
You are using your map wrong:
IconButton(
icon: Image(image: AssetImage(imagesIcon[1].toString())),
onPressed: () {},
),
I think it may be because you're passing icons as strings. Instead of using images with a class like:
ImageIcon(
AssetImage("images/icon.png"),
color: Colors.red,
size: 24,
),
Edit: after recreating the situation in my project I've found the reason behind that. You use imagesIcon.values.toString() to pass the value of an Icon. Did you check what this function returns? In my case it returned:
(assets/images/test1.jpg, assets/images/test2.jpg,
assets/images/test3.jpg)
No wonder the image is unreadable according to flutter because that is not a proper path or file.
Instead you can use your solution like this:
imagesIcon.values.elementAt(1) //0,1,2 whatever index you need from map

Flutter web Instance of 'minified:eU<void>'

I am building a Flutter web app, which runs flawlessly in debug mode, but whenever I try to run it in release mode or deploy it to the hosting I see a grey box.
I see this:
Instead of this:
As you may see, this is an alertDialog, here is the code of it:
class TeamDetailsDialog extends StatelessWidget {
final Tournament tournament;
final Team team;
final String matchId;
TeamDetailsDialog(this.team, this.matchId, this.tournament);
#override
Widget build(BuildContext context) {
return Theme(
data: ThemeData(buttonBarTheme: ButtonBarThemeData(alignment: MainAxisAlignment.spaceBetween)),
child: AlertDialog(
backgroundColor: Color(0xFF333D81),
title: Text(
"Csapatnév: ${team.name}",
style: TextStyle(color: Colors.white),
),
content: DefaultTextStyle(
style: TextStyle(color: Colors.white),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Align(alignment: Alignment.centerLeft, child: Text("A csapat tagjai:")),
),
for (Player player in team.players) Text("${player.displayName}(${player.inGameDisplayName})")
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(
"Bezárás",
style: TextStyle(color: Colors.white),
)),
Spacer(),
TextButton(
onPressed: () {
// Retrieving the match object from the Cubit.
final Match matchWithoutWinner =
BlocProvider.of<TournamentCubit>(context).getMatchOfTournamentById(tournament, matchId);
// Creating a new match instance containing the winner team.
if (matchWithoutWinner is DoubleEliminationLoserBranchMatch) {
final DoubleEliminationLoserBranchMatch matchWithWinner = DoubleEliminationLoserBranchMatch(
matchWithoutWinner.id,
matchWithoutWinner.team1,
matchWithoutWinner.team2,
team,
matchWithoutWinner.parent1id,
matchWithoutWinner.parent2id);
BlocProvider.of<TournamentCubit>(context).setWinnerOfMatch(tournament, matchWithWinner);
}
else {
final Match matchWithWinner = Match(matchWithoutWinner.id, matchWithoutWinner.team1,
matchWithoutWinner.team2, team, matchWithoutWinner.parent1id, matchWithoutWinner.parent2id);
BlocProvider.of<TournamentCubit>(context).setWinnerOfMatch(tournament, matchWithWinner);
}
Navigator.pop(context);
},
child: Text(
"Beállítás győztesnek",
style: TextStyle(color: Colors.white),
))
],
),
);
}
}
I've found out that the grey box is the release version of the red screen of death. After that, I checked, none of the injected variables are null. There is only one problem in debug:
What could be the problem? Could this cause the issue and how can I fix it?
The cause of the issue was the Spacer() between the two buttons in the actions list, removing it fixed the problem, without changing the UI.

Image does not load on Flutter Draw Header

I'm new to flutter-dart and I've been trying to load a image to my draw header, so far unsuccessfully. Can anyone shed some light on it? Please find my code below.
class MainDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
//color: Theme.of(context).backgroundColor,
image: DecorationImage(
image: AssetImage('assets/images/title_fb_600.png'),
fit: BoxFit.cover,
)),
child: Text("Some Text"),
//ListTile(
// leading: Icon(Icons.monetization_on),
// title: Text(
// "TRIPLE A",
// style: TextStyle(color: Colors.white, fontSize: 20),
// ),
),
ItemDrawer("Carteira Recomendada", Icons.star, 16),
ItemDrawer("Carteira R\$10", Icons.ten_k, 16),
ItemDrawer("Minha Carteira", Icons.favorite, 16),
ItemDrawer("Notícias", Icons.new_releases, 16),
ItemDrawer("Minha Conta", Icons.account_circle, 16),
ItemDrawer("Configurações", Icons.settings, 16),
],
),
);
}
}
My pubspec.yaml....
> flutter:
>
uses-material-design: true
>
assets:
> - assets/images/title3a_fb_600.png
And folder structure...
folder structure
Your pubspec file looks good. I'm assuming you only want to include this single image as an asset.
Try using this package, and replacing the DrawerHeader class with a ProgressiveImage component here.
https://pub.dev/packages/progressive_image
This package helps you to cache the image, to prevent having to reload it again, and it also adds a blur animation while it loads this asset.
If you need to layer the drawer header (and add other information, such as an avatar icon, or email information), you can add this component into a Stack collection, then make sure that the ProgressiveImage is the first in the Stack
If you also need to set a custom height to the drawer header, then make sure to wrap this Stack with a Container and manually set the height of this element

flutter for web: text cut off at bottom

With web flutter my text is cut at the bottom when I display text I tried to put padding but it doesn't work.
this problem this product everywhere even on the TextField
new Container(
width: menuRightWidthDesktop,
height: getSize == 0 ? heightHeaderDesktop : getSize == 1 ? heightHeaderTablette : heightHeaderMobile,
color: Colors.red,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new GestureDetector(
child: new Container(
color: Colors.indigoAccent,
child: new Text("Surfeur >"),
),
onTap: () {},
),
new GestureDetector(
child: new Text("Photographe >", ),
onTap: () {},
),
],
),
),
I've been having the same issue and solved it by increasing the height property of the text by a small value
Text("Surfeur >",
style: TextStyle(
height: 1.1,
),
),
There is a new pull request https://github.com/flutter/engine/pull/13929
experimental implementation of text measurement that's based on Canvas2d
You can use the following command
flutter run -d web-server --release --dart-define=FLUTTER_WEB_USE_EXPERIMENTAL_CANVAS_TEXT=true
#madebyAyan's answer works for me. Moreover, I added this as a theme to avoid repeating parts in multiple places.
TextTheme _buildTextTheme(TextTheme base) {
return base.copyWith(
bodyText2: base.bodyText2?.copyWith(
height: 1.1,
),
);
}
use text property TextBaseline.ideographic :
Text("Surfeur >",
style: TextStyle(
textBaseline: TextBaseline.ideographic,
),
),
I had the same Problem in FireFox so I tried a few workarounds but none seemed to work so I checked wich version of flutter I was using with flutter doctor. I was already using the beta channel (Version 1.25.0...). Since there were newer beta versions available I ran flutter upgrade and upgraded to version 1.26.0....pre.
The issue just disappeared so I guess it got fixed in a newer version.
So... just upgrade flutter to the newest beta version.

how to change the location of my flatbutton in flutter... (from most left to most right)

I have build a simple app to learn flutter. I want to move a flat button to the most right of the screen.
I cant seem to use padding property and I don't know why.
new FlatButton(
onPressed: () => debugPrint("pressed"),
child: new Text("DONATE",
style: new TextStyle(
fontSize: 16,
color: Colors.green[400]
)
)
)
Try this:
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () => debugPrint("pressed"),
child: new Text("DONATE",
style: new TextStyle(
fontSize: 16,
color: Colors.green[400]
)
)
),
],
),
Have you checked out flutter layouts,here
plus padding property is for inside the button to move the button manipulate the margin property.
Hope that helps.