Skip to content

Instantly share code, notes, and snippets.

@enzo-santos
Created August 5, 2023 14:17
Show Gist options
  • Save enzo-santos/0862e3c8fac387cc35e846c8ae3e5652 to your computer and use it in GitHub Desktop.
Save enzo-santos/0862e3c8fac387cc35e846c8ae3e5652 to your computer and use it in GitHub Desktop.
ExemploIntrinsicHeight
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: Tela(),
));
}
class Tela extends StatelessWidget {
const Tela({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Neste exemplo, a altura do Container '
'se adapta ao conteúdo do texto, mas '
'os Containers não ficam na mesma altura:',
),
Row1(),
SizedBox(height: 20),
Text(
'Neste exemplo, os Containers ficam na '
'mesma altura, mas a altura do Container '
'não se adapta ao conteúdo do texto (dá overflow):',
),
Row2(),
SizedBox(height: 20),
Text(
'Neste exemplo, os Containers ficam na '
'mesma altura e a altura do Container '
'se adapta ao conteúdo do texto (usando '
'IntrinsicHeight)',
),
Row3(),
],
),
);
}
}
class Child extends StatelessWidget {
final String texto;
final bool alturaFixa;
const Child({
super.key,
required this.texto,
required this.alturaFixa,
});
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
margin: const EdgeInsets.symmetric(
horizontal: 10,
),
width: size.width / 5,
height: alturaFixa ? (size.height / 15) : null,
color: Colors.red,
child: Text(texto),
);
}
}
const List<String> textos = [
'um texto muito grande grande grande ',
'abc',
'teste',
];
class Row1 extends StatelessWidget {
const Row1({super.key});
@override
Widget build(BuildContext context) {
return Row(
children: [
for (String texto in textos)
Child(
texto: texto,
alturaFixa: false,
),
],
);
}
}
class Row2 extends StatelessWidget {
const Row2({super.key});
@override
Widget build(BuildContext context) {
return Row(
children: [
for (String texto in textos)
Child(
texto: texto,
alturaFixa: true,
),
],
);
}
}
class Row3 extends StatelessWidget {
const Row3({super.key});
@override
Widget build(BuildContext context) {
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (String texto in textos)
Child(
texto: texto,
alturaFixa: false,
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment