当前位置:网站首页>Flutter--Button浅谈
Flutter--Button浅谈
2022-06-11 19:25:00 【antu58】
作为一个常用到不能再常用的组件,material库中的button组件都有一点奇怪——存在无法设置的内外边距,我们做一个简单的展示,选几个常见button,统一使用Text做child,外加一个带红色的Container组件,观察margin和padding.
你会发现,除了InkWell,即使padding设置为0,仍然无法消除消除上下padding,这其实不是padding属性没有生效,而是这些按钮有默认大小的约束。这点可以通过源码和不设置child的UI来佐证。


MaterialButton设置它的高度属性,看源码的使用,是修改高度约束,但是小到一定程度还是无法修改,是因为有别的地方进一步作出了限制,这就是我觉得奇怪的一些地方。这会给使用者带来不便,所以我设计基础组件的基本原则之一就是不要去干涉外界的使用,比如我做了一个Card组件,我就不会给它一个默认margin。
MaterialButton(
onPressed: () {
},
// child: Text("MaterialButton"),
padding: EdgeInsets.all(0),
height: 10,
)解决之法
其实就是前面提到的唯一正常的组件,Inkwell,其实上面所有的button都是对它的进一步封装,它包含点击特效(水波纹),以及各种手势识别,而它的本质也是在GestureDetector的基础上加了一个动画层,所以如果不需要动画效果,你大可以直接使用GestureDetector。进阶一点的做法就是基于这两者来创建你自己的Button。
InkWell的build部分源码(ink_well.dart)

测试的代码
Column(children: [
SizedBox(width: double.infinity,),
addSpacer(),
InkWell(child: Text("InkWell"), onTap: () {
},).addRedBackground(),
addSpacer(),
TextButton(
onPressed: () {},
child: Text("TextButton"),
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero)
),
).addRedBackground(),
addSpacer(),
MaterialButton(
onPressed: () {
},
child: Text("MaterialButton"),
padding: EdgeInsets.all(0),
).addRedBackground(),
addSpacer(),
OutlinedButton(
onPressed: () {},
child: Text("OutlinedButton"),
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
).addRedBackground(),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
//空button
Column(children: [
SizedBox(width: double.infinity,),
addSpacer(),
InkWell(child: Text(""), onTap: () {
},).addRedBackground(),
addSpacer(),
TextButton(
onPressed: () {},
child: Text(""),
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero)
),
).addRedBackground(),
addSpacer(),
MaterialButton(
onPressed: () {
},
// child: Text("MaterialButton"),
padding: EdgeInsets.all(0),
).addRedBackground(),
addSpacer(),
OutlinedButton(
onPressed: () {},
child: Text(""),
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
).addRedBackground(),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
)边栏推荐
- SQL injection vulnerability learning 1: phpstudy integrated environment building DVWA shooting range
- First acquaintance with enterprise platform
- leetcode:66. add one-tenth
- Anaconda installation, jupyter notebook default startup path modification and nbextensions plug-in installation
- Judge whether it is a balanced binary tree
- 7-3 组合问题(*)
- Hyper parameter optimization of deep neural networks using Bayesian Optimization
- cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】
- 激活函数公式、导数、图像笔记
- Web3 Games: exploring and reshaping the game experience
猜你喜欢
![Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]](/img/0b/1d3cd06e3d593a997a993a4d96e441.png)
Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]

Qubicle notes: Hello voxel

Cf:d. black and white stripe

CMU 15 - 445 cours de base de données Leçon 5 version texte - Pool tampon
![leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]](/img/6b/f2e04cd1f3aaa9fe057c292301894a.png)
leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
![[signal denoising] speech adaptive denoising based on nonlinear filter with matlab code](/img/fd/07cee3c51ac44ca40f730dd487aa20.png)
[signal denoising] speech adaptive denoising based on nonlinear filter with matlab code
![PIL pilot image processing [1] - installation and creation](/img/21/34856f53c08a5369b834b7ca2fb07b.jpg)
PIL pilot image processing [1] - installation and creation
![[assembly] analysis of Experiment 7 of the fourth edition of assembly language](/img/ac/a3c5bfeb2dcb93b123dd337993bab3.jpg)
[assembly] analysis of Experiment 7 of the fourth edition of assembly language

Understand how to get started with machine learning to quantify transactions?

Key contents that wwdc22 developers need to pay attention to
随机推荐
leetcode:926. 将字符串翻转到单调递增【前缀和 + 模拟分析】
使用贝叶斯优化进行深度神经网络超参数优化
On high availability architecture
cf:A. Print a Pedestal (Codeforces logo?) [simple traversal simulation]
Raki's notes on reading paper: memory replace with data compression for continuous learning
WinCC flexible 2008项目移植到博途WinCC的具体方法
High concurrency architecture design
更换目标检测的backbone(以Faster RCNN为例)
Introduction to go language (V) -- branch statement
CMU 15-445 數據庫課程第五課文字版 - 緩沖池
Judge whether it is a balanced binary tree
Kubernetes binary installation (v1.20.15) (VIII) deploying network plug-ins
highcharts设置柱状图宽度、渐变、圆角、柱子上方数据
Cf:b. array determinations
cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】
Practice of Flink CDC in Dajian cloud warehouse
[signal denoising] speech adaptive denoising based on nonlinear filter with matlab code
YOLOv3 Pytorch代码及原理分析(二):网络结构和 Loss 计算
Review of software testing technology
SLAM APP