Skip to content

Change height of GridView row to fixed height #55290

@perfoWorks

Description

@perfoWorks

I've noticed that GridView makes each child a square.. I know we can change dynamically height of grid view row by setting childAspectRatio. That however doesn't make it fixed so when the device rotate then the height changed as well
In this example I want to have each input next to each other and keep it's height same on all devices and all display orientation. (nobody wants their text input change its height)

List<Map<String, dynamic>> list = [
  {'description': 'des1'},
  {'description': 'des2'},
  {'description': 'des3'},
  {'description': 'des4'},
  {'description': 'des5'},
  {'description': 'des6'}
];

class _MaterialScreenState extends State<MaterialScreen> {
  String text = '';

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
        body: Column(
      children: <Widget>[
        Text(text),
        GridView.builder(
            shrinkWrap: true,
            itemCount: list.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 5,
                crossAxisSpacing: 5,
                mainAxisSpacing: 5,
                childAspectRatio: width / (height / 4)),
            itemBuilder: (BuildContext context, int index) {
              return TextFormField(
                textAlign: TextAlign.center,
                onTap: () => setState(() {
                  text = list[index]['description'];
                }),
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    //isDense: true,
                    contentPadding: EdgeInsets.all(8)),
              );
            }),
      ],
    ))

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityf: scrollingViewports, list views, slivers, etc.frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework teamworkaround availableThere is a workaround available to overcome the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions