I’m trying to align my view so that all the widgets have the same margin that looks uniform but Icon is generating an automatic margin for me, I don’t want to use an app bar for this.
This is my view:
import 'package:flutter/material.dart';
class RegisterView extends StatefulWidget {
const RegisterView({super.key});
@override
State<RegisterView> createState() => _RegisterViewState();
}
class _RegisterViewState extends State<RegisterView> {
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final double marginHorizontal = size.width * 0.09;
return Scaffold(
body: SingleChildScrollView(
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: marginHorizontal),
child: appBar
),
SizedBox(height: size.height * 0.02,),
Container(
margin: EdgeInsets.symmetric(horizontal: marginHorizontal),
child: description
)
],
),
),
),
);
}
Widget get appBar => Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
iconSize: 40.0,
onPressed: (){},
icon: const Icon(Icons.arrow_back_ios_new, color: Colors.white,)
),
const SizedBox(
width: 10.0,
),
Text(
'Registro',
style: Theme.of(context).textTheme.titleLarge,
)
],
);
Widget get description => Text(
'Completa los siguientes datos',
style: Theme.of(context).textTheme.bodySmall,
);
}
I’m trying to get my Row to align completely to the left of my screen but it seems the Icon is automatically adding a margin.
Indeed, when removing the IconButton, the rest of the widgets align correctly
I tried this and it didn’t work
IconButton(
padding: EdgeInsets.zero,
alignment: Alignment.topLeft,
iconSize: 40.0,
onPressed: (){},
icon: const Icon(Icons.arrow_back_ios_new, color: Colors.white,)
),
Padding(
padding: const EdgeInsets.all(0.0),
child: IconButton(
alignment: Alignment.topLeft,
iconSize: 40.0,
onPressed: (){},
icon: const Icon(Icons.arrow_back_ios_new, color: Colors.white,)
),
),
I’m almost sure that the problem is in the Icon Widget since when trying to implement this with a GestureDetector the problem persists.
2
Answers
To remove padding around the IconButton, try also to add constraints and padding to IconButton properties and padding again to ButtonStyle properties.
Below an example:
Set
visualDensity: VisualDensity.compact
to remove unnecessary padding IconButtonAbout the
Icon
widget itself, it’s always visible as a square widget, that’s why it hassize
property instead ofwidth
&height
, this is the requirement of icon design.But arrow_back_ios_new icon doesn’t have width = height in terms of design, that’s the problem
The solution here is maybe you need a custom arrow icon (through icon font) to generate your real square arrow by yourself to serve your expectation. Or you need a trick like your appBar has less margin left than the others