Today I'm going to show you 4 very useful Flutter tricks that you can apply to your projects in no time. I won't show you any packages or extensions like I usually do, but very simple, but very useful tips!
original
https://tomicriedel.medium.com/4-extremely-useful-flutter-tips-57686f1e3707
text
Today I'm going to show you 4 very useful Flutter tricks that you can apply to your projects in no time. I won't show you any packages or extensions like I usually do, but very simple, but very useful tips!
Simplify Assert Management
Managing Asserts can be very difficult. If you want to use an image multiple times in your application, you have to specify the path again and again. But there is a much simpler solution. Create an App Assets class to store all App Asserts. Now you can easily call Assert with AppAssets.appLogo
or AppAssets.noConnection
.
easier imports
It's really annoying to see and manage thousands of imports at the beginning of a file. That's why I'm showing you an easy way to reduce imports.
假设你有一个文件夹叫做Constants, app_colors.dart
, app_fonts.dart
, app_theme.dart
, app_constants
and app_assets.dart
are.
In this folder, you now create a new file called constant.dart.
Here, you write an export statement for each file. Now you can access each of your files by simply importing constant.dart:
Remove splash effect from button
Everyone knows this splash effect when you click a button, and I don't like it at all.
So I'm going to show you how to remove this effect with a single line.
For this you have to use splashFactory
:
Now your button looks like this when pressed:
(I keep clicking the button)
Simpler platform widgets
Every Flutter developer probably knows what happens when you query if a user is iOS or Android. So you then display a specific wdiget, such as Switch or CupertinoSwitch. But what if I told you that you don't need one query and you don't need two gadgets? How did you do it? This is what I'm going to show you now:
Many gadgets available for Android and iOS have one. Adaptive extension for Android version. For example, let's use . Adapted to:
OK, that's good enough, but the best is yet to come: this can also be used for icons. To do this, you just need to use Icons.adaptive.share
to display a share icon on Android and iOS.
I don't know which parts these gadgets are for, but anyway, Slider
, SwitchListTile
, and CircularProgressIndicator
can use this feature.
Visibility widget
Using bool to query whether a widget should be visible usually looks like this:
But there's also a little tool called Visibility that does just that:
This looks so much better, right?
end
Thanks for reading and have a nice day!
© Cat Brother
- WeChat ducafecat
- Blogducafecat.tech
- github
- bilibili
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。