shyndman / flutter_layout_grid

A grid-based layout system for Flutter, inspired by CSS Grid Layout
MIT License
441 stars 43 forks source link

GridFit.expand doesn't apply to Text #100

Open zhxst opened 10 months ago

zhxst commented 10 months ago

Hi. Thank you for this beautiful layout tool. It makes the layout work for flutter much easier.

GridFit.expand will apply the size constraint to some Widgets, like Container and Image. But it doesn't apply to Text, which makes Text always use smallest size to just fit the text inside. So the textAlign has no effect.

Here is an example code.

import 'package:flutter/material.dart';
import 'package:flutter_layout_grid/flutter_layout_grid.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF2AB06F)),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(color: Colors.blue, height: 20),
              const Text('abc', textAlign: TextAlign.center),
              const Text('abc', textAlign: TextAlign.center),
              const Text('abc', textAlign: TextAlign.center),
            ],
          ),
          const SizedBox(height: 40),
          LayoutGrid(
            gridFit: GridFit.expand,
            areas: '''
              t1   b1
              t2   b2
              t3   b3
              ''',
            columnSizes: [1.fr, 1.fr],
            rowSizes: const [auto, auto, auto],
            children: [
              Align(
                  alignment: Alignment.topCenter,
                  child: const Text('abc').inGridArea('t1')),
              const Text('abc', textAlign: TextAlign.center).inGridArea('t2'),
              const Text('abc', textAlign: TextAlign.center).inGridArea('t3'),
              Container(color: Colors.blue.shade300).inGridArea('b1'),
              Container(color: Colors.blue).inGridArea('b2'),
              Container(color: Colors.blue.shade700).inGridArea('b3'),
            ],
          )
        ],
      ),
    );
  }
}

And here is outcome: image

The LayoutGrid should set the Text fit size of cell, just like the Column or so.

The workaround is Align(alignment: Alignment.topCenter) , which is verbose if you want to make many Text to align center in cell. The Center doesn't fit because I want to remain the vertical position of the Text to the top. And of course neither Expanded nor Flexiable.