Le Tutoriel de Flutter SnackBar
View more Tutorials:


Dans les applications mobiles, SnackBar est un petit composant d'interface qui fournit une brève réponse après une action de l'utilisateur. Il apparaît en bas de l'écran et disparaît automatiquement lorsque le temps est écoulé ou lorsque l'utilisateur interagit ailleurs sur l'écran.
SnackBar fournit également un bouton en option pour effectuer une action. Par exemple, annuler une action que vous venez d'effectuer ou réessayer l'action que vous venez d'effectuer si elle échoue.
SnackBar Constructor:
SnackBar Constructor
const SnackBar(
{Key key,
@required Widget content,
Color backgroundColor,
double elevation,
EdgeInsetsGeometry margin,
EdgeInsetsGeometry padding,
double width,
ShapeBorder shape,
SnackBarBehavior behavior,
SnackBarAction action,
Duration duration: _snackBarDisplayDuration,
Animation<double> animation,
VoidCallback onVisible}
)
L'application Flutter suit les directives de cohérence de Material Design pour s'assurer que lorsque SnackBar apparaît au bas de l'écran, il ne chevauche pas d'autres widgets enfants importants, tels que FloatingActionButton. Par conséquent, SnackBar doit être appelé via Scaffold.
Observer un exemple simple ci-dessous:

main.dart (ex1)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'o7planning.org',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter SnackBar Example'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {},
),
body: Center(
child: Builder(
builder: (BuildContext ctxOfScaffold) {
return ElevatedButton(
child: Text('Show SnackBar'),
onPressed: () {
this._showSnackBarMsgDeleted(ctxOfScaffold);
}
);
}
)
)
);
}
void _showSnackBarMsgDeleted(BuildContext ctxOfScaffold) {
// Create a SnackBar.
final snackBar = SnackBar(
content: Text('Message is deleted!'),
action: SnackBarAction(
label: 'UNDO',
onPressed: () {
this._showSnackBarMsgRestored(ctxOfScaffold);
},
),
);
// Find the Scaffold in the widget tree
// and use it to show a SnackBar.
Scaffold.of(ctxOfScaffold).showSnackBar(snackBar);
}
void _showSnackBarMsgRestored(BuildContext ctxOfScaffold) {
// Create a SnackBar.
final snackBar = SnackBar(
content: Text('Message is restored!')
);
// Find the Scaffold in the widget tree
// and use it to show a SnackBar.
Scaffold.of(ctxOfScaffold).showSnackBar(snackBar);
}
}
content - Le contenu principal affiché sur le SnackBar, normalement un objet Text.
@required Widget content
backgroundColor - La couleur d'arrière-plan de SnackBar.
Color backgroundColor
elevation - Les coordonnées de l'axe Z du SnackBar, leur valeur affecte la taille de l'ombre (shadow) du SnackBar.
double elevation
Remarque: la propriété elevation ne fonctionne que pour SnackBar flottant (behavior: SnackBarBehavior.floating).
elevation (ex1)
final snackBar = SnackBar(
content: Text('Message is deleted!'),
elevation: 15,
behavior: SnackBarBehavior.floating,
action: SnackBarAction(
label: 'UNDO',
onPressed: () {
},
),
)

La propriété margin est utilisée pour créer un espace vide autour de SnackBar. Cependant, cette propriété ne fonctionne que si la valeur de behavior est SnackBarBehavior.floating et que width n'est pas spécifiée. Sa valeur par défaut est EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 10.0).
EdgeInsetsGeometry margin
La propriété padding est utilisée pour créer un espace vide dans SnackBar et entourer content et action.
EdgeInsetsGeometry padding
La propriété width est la largeur de SnackBar et elle ne fonctionne que si le behavior est SnackBarBehavior.floating. Si width est spécifiée, SnackBar sera placé horizontalement au centre.
double width
ShapeBorder shape
- TODO Link?
La propriété behavior spécifie le comportement et la position de SnackBar.
SnackBarBehavior behavior
// Enum values:
SnackBarBehavior.fixed
SnackBarBehavior.floating

action est affichée sous forme de bouton sur SnackBar. L'utilisateur peut cliquer dessus pour effectuer une action.
SnackBarAction action
Duration duration: _snackBarDisplayDuration
Animation<double> animation
SnackBar withAnimation (
Animation<double> newAnimation,
{Key fallbackKey}
)
VoidCallback onVisible