Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active September 2, 2024 13:25
Show Gist options
  • Save hectorAguero/ab49cce60a46581e197023149ab6d309 to your computer and use it in GitHub Desktop.
Save hectorAguero/ab49cce60a46581e197023149ab6d309 to your computer and use it in GitHub Desktop.
keys in widgets?
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: const Text('Material App Bar'),
),
body: const Row(
children: [
DesignSystemMolecule(title: 'Column 1'),
DesignSystemMolecule(title: 'Column 2'),
],
),
),
);
}
}
class DesignSystemMolecule extends StatelessWidget {
const DesignSystemMolecule({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
DesignSystemBox(
// This key is to avoid break outside proyect tests,
// so if we change the widget we should mantain the key value
key: ValueKey('${title}_atom_1'),
title: 'Atom 1',
color: Colors.blue,
),
DesignSystemBox(
// This key is to avoid break outside proyect tests,
// so if we change the widget we should mantain the key value
key: ValueKey('${title}_atom_2'),
title: 'Atom 2',
color: Colors.blue,
),
// Text wrapper widgets shouldn't use keys, use findText on tests
DesignSystemText('title')
],
),
);
}
}
/// Shows a Box with the text and color
class DesignSystemBox extends StatelessWidget {
const DesignSystemBox({
required this.title,
required this.color,
super.key,
});
final String title;
final Color color;
@override
Widget build(BuildContext context) {
return ColoredBox(
color: color,
child: Text(
title,
),
);
}
}
/// Wrapper of Flutter Text Widget
class DesignSystemText extends StatelessWidget {
const DesignSystemText(this.title, {
super.key,
});
final String title;
@override
Widget build(BuildContext context) {
return Text(
title,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment