0

I would like to horizontally center the trailing icon of my expansionTile,

Here is my expansionTile with the trailing on the bottom right :

enter image description here

I already tried to encapsulate the Icon in a Align and a Container but doesn't work, I also tried Padding but it's not stable if you change the size of the screen.

Code with Align :

trailing : Align(
    alignment: Alignment.center,
    child: Icon(
      BeoticIcons.clock,
      color: BeoColors.lightGreyBlue
    )
  ),

With Container :

trailing: Container(
        alignment: Alignment.center,
        child: Icon(
          BeoticIcons.clock,
          color: BeoColors.lightGreyBlue
        )
      ),

Thanks for your help.

JS1
  • 631
  • 2
  • 7
  • 23

4 Answers4

1
you can use column and align your icon like this way hope this code will help you, thank you

import 'package:flutter/material.dart';

class Hello extends StatelessWidget {
   const Hello({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            height: 140,
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5.0),
              color: Colors.deepPurple[200],
            ),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Center(child: Text("Hello")),
                  Text("Hello"),
                  SizedBox(height: 50,),
                  Align(
                    alignment: Alignment.center,
                      child: Icon(Icons.lock_clock))
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Vishal_VE
  • 1,852
  • 1
  • 6
  • 9
1

This will work for you. Use LayoutBuilder to get parent widget width, and set relative padding using constraints. For example, use constraints.maxWidth * 0.5, to center across width. Your padding will be stable if you change the size of the screen:)

trailing: LayoutBuilder(builder: (ctx, constraints) {
              return Padding(
                padding: EdgeInsets.only(
                  right: constraints.maxWidth * 0.5,
                ),
                child: Icon(
                  Icons.menu,
                ),
              );
            }),
Varun Kumar
  • 524
  • 3
  • 13
0

Ok, I found how to do it.

Simply put the icon in the title attribute of the ExpansionTile :

return ExpansionTile(
  title: Icon(
    BeoticIcons.simply_down,
    color: BeoColors.lightGreyBlue,
  ),
JS1
  • 631
  • 2
  • 7
  • 23
0

you can use the tilePadding property

tilePadding: EdgeInsets.symmetric(horizontal: size.width * .3),