flutter - IconButton 与可点击区域不对齐
问题描述
我有一个简单的图标按钮,如下所示,无论我做什么,可点击区域都不与图标对齐。尝试在 Linux、Web 和 Android 上运行该应用程序.. 都遇到同样的问题。
home: Scaffold(
body: Column(children: [
IconButton(
onPressed: () => {},
icon: Icon(
Icons.highlight_off,
size: 70,
color: Colors.red,
),
),
])));
我的代码的简化版本:
MaterialApp(
home: Scaffold(
body: Center(
child: Stack(
alignment: AlignmentDirectional.center,
overflow: Overflow.visible,
children: [
Container(
width: 500,
height: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3),
),
]),
),
Positioned(
top: -40,
right: 5,
child: IconButton(
onPressed: () => {},
icon: Icon(
Icons.highlight_off,
size: 70,
color: Colors.red,
),
),
),
]),
)));
解决方案
干得好。
发生这种情况的原因,你iconSize
的很大。因此,根据这一点,您需要确保您的按钮本身调整其大小。为此,您可以使用BoxConstraints
它来提供它minHeight
。
import 'package:flutter/material.dart';
void main() async {
runApp(
MaterialApp(
home: Scaffold(
body: Center(
child: Stack(
alignment: AlignmentDirectional.center,
overflow: Overflow.visible,
children: [
Container(
width: 500,
height: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3),
),
]),
),
Positioned(
top: -40,
right: 5,
child: IconButton(
constraints: BoxConstraints(
minHeight: 100,
),
onPressed: () => {},
icon: Icon(
Icons.highlight_off,
size: 70,
color: Colors.red,
),
),
),
]),
),
),
),
);
}
推荐阅读
- serialization - 如何找到最简单的人类可读的浮点字符串,它在转换回浮点数时会产生相同的字节?
- mysql - 如何使用docker连接spring-boot和mysql?
- struts2 - Struts2 命名空间配置
- sql-server - 使用合并(更新/插入)和分组依据的 SQL 存储过程
- scala - 为什么迭代RDD后局部变量值不可见?
- django - 我们可以一次将树结构上传到django服务器吗?就像我们使用 git 一样?
- python - 如何删除字符串列表中列出的所有字符串,但只删除后跟空格的字符串
- javascript - 如何向 ajax api vue js 显示一些用户帖子
- c - linux c共享内存:为什么读写时内容的顺序是相反的
- php - PhpStorm 中的 Foreach 循环