我有一个简单的图标按钮,如下所示,出于某种原因,无论我做什么,可单击区域都不与图标对齐。试图在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,
),
),
),
]),
)));
答案 0 :(得分:1)
你在这里。
发生这种情况的原因,您的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,
),
),
),
]),
),
),
),
);
}