3

Good morning,

from a lot of time I'm facing with a big problem regarding responsivity with Flutter GridView widget. This is my card list on iPad Pro 12.9

enter image description here

and this is on iPad Air 2

enter image description here

This is the card code

class HomeCard extends StatelessWidget {
  final String title;
  final String subtitle;
  final String category;
  final String imgUrl;

  const HomeCard({
    Key key,
    this.title,
    this.subtitle,
    this.category,
    this.imgUrl,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Brightness _brightness = Theme.of(context).brightness;

    return SingleChildScrollView(
      physics: const NeverScrollableScrollPhysics(),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          AspectRatio(
            aspectRatio: 16 / 9,
            child: CachedNetworkImage(
              imgUrl: imgUrl,
              fit: BoxFit.fitWidth,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10.0, left: 25.0),
            child: Text(
              title,
              textAlign: TextAlign.left,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  color: Themes.cardTitleBlue,
                  fontWeight: FontWeight.w500,
                  fontSize: 16.0),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 25.0, right: 25.0),
            child: Text(
              subtitle, //'Something else only for fill this space. It can be a simply description',
              textAlign: TextAlign.left,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontWeight: FontWeight.w400,
                fontSize: 15.0,
                height: 1.2,
                color: _brightness == Brightness.light
                    ? Colors.black45
                    : Colors.white54,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

this is the GridView code

GridView.builder(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 4.0,
                        mainAxisSpacing: 4.0,
                        childAspectRatio:
                            MediaQuery.of(context).size.aspectRatio/0.8,
                      ),
                      controller: _scrollController,
                      itemCount: state.hasReachedMax
                          ? state.tours.length
                          : state.tours.length + 1,
                      itemBuilder: (context, index) {
                        if (index >= state.tours.length) {
                          return const MyLoader();
                        } else {
                          return _getCard(state.tours[index]);
                        }
                      },
                    ),

In every device aspect ratio is different and also if I apply a correction based on this there are also some devices in which the card layout is cut or over height. My card has not a fixed height because I want that it adapts itself due to images and text. Please do not advise me to use staggered_grid_view package because sometimes I've low performance using it. I need only to show a list of card with 2/3 items for row/line and need that the height is inherited from card height. There are a lot of posts on StackOverflow that discuss about this but no one consider the all devices layout and a dynamic card height.

Hope I was clear

Thank you

E.Benedos
  • 1,585
  • 14
  • 41
  • may be this helps:https://medium.com/flutter-community/flutter-effectively-scale-ui-according-to-different-screen-sizes-2cb7c115ea0a –  Jun 24 '20 at 20:02

1 Answers1

0

I have an Idea ... First please Look at this answer for finding size of widget during the build with overlayEntery

How to get a height of a Widget?

after that set the right aspect ratio (Measured with overlayEntery ) to your Grid View in childAspectRatio.

for sending data from Child to Parent this Article can help ...

https://medium.com/flutter-community/flutter-communication-between-widgets-f5590230df1e

I hope this help you.

EDIT : I make a Full working Example based on this Idea at Here:

How to calculate childAspectRatio for GridView.builder in flutter

Sajjad
  • 2,593
  • 16
  • 26