Created with <3 with dartpad.dev.
Created
July 26, 2022 09:57
-
-
Save Chralu/465d5d71af1cdcd3f4ec6234469ae032 to your computer and use it in GitHub Desktop.
Flutter101 - Layout example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class MyAppBar extends StatelessWidget { | |
const MyAppBar({required this.title, super.key}); | |
// Dans un widget, les propriétés sont toujours marquées "finales". | |
final Widget title; | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: 56.0, // les tailles sont exprimées en points | |
padding: const EdgeInsets.symmetric(horizontal: 8.0), | |
decoration: BoxDecoration(color: Colors.blue[500]), | |
// Row est un empilement horizontal de Widgets | |
child: Row( | |
children: [ | |
const IconButton( | |
icon: Icon(Icons.menu), | |
tooltip: 'Navigation menu', | |
onPressed: null, // null désactive le bouton | |
), | |
// Expanded étend son enfant | |
// pour remplir tout l'espace disponible. | |
Expanded( | |
child: title, | |
), | |
const IconButton( | |
icon: Icon(Icons.search), | |
tooltip: 'Search', | |
onPressed: null, | |
), | |
], | |
), | |
); | |
} | |
} | |
class MyScaffold extends StatelessWidget { | |
const MyScaffold({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
// Material est un élément de base | |
// sur lequel les Widgets material peuvent se dessiner. | |
return Material( | |
// Column est un empilement vertical de Widgets | |
child: Column( | |
children: [ | |
MyAppBar( | |
title: Text( | |
'Example title', | |
style: Theme.of(context) // | |
.primaryTextTheme | |
.headline6, | |
), | |
), | |
const Expanded( | |
child: Center( | |
child: Text('Hello, world!'), | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
void main() { | |
runApp( | |
const MaterialApp( | |
title: 'My app', // Utilisé par l'OS | |
home: SafeArea( | |
child: MyScaffold(), | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment