SnackBar

Technology for ecology

SnackBar

Flutter SnackBar
Flutter SnackBar

Shows 2 snackbars (small window popping up from the bottom of the screen): 1 simple snackbar displaying a message, and another snackbar displaying a message and an action button.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import 'package:flutter/material.dart';
 
class ShowSnackBar extends StatefulWidget {
  const ShowSnackBar({Key? key}) : super(key: key);
 
  @override
  _ShowSnackBarState createState() => _ShowSnackBarState();
}
 
class _ShowSnackBarState extends State<ShowSnackBar> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("Elevated Buttons")),
        body: Container(
            padding: const EdgeInsets.all(20),
            alignment: Alignment.center,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                buttonForSimpleSnackBar(context),
                const SizedBox(height: 50),
                buttonForSnackBarWithAction(context),
              ],
            )
        )
    );
  }
 
  /// **********************************************************************
  /// Button for simple snackbar example
  /// **********************************************************************
  Widget buttonForSimpleSnackBar(BuildContext context) {
    return ElevatedButton(
        onPressed: () {
          displaySnackBar(context);
        },
        style: ElevatedButton.styleFrom(
            backgroundColor: Colors.blue,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0))
        ),
        child: const Text("Press to display a snackbar")
    );
  }
 
  /// **********************************************************************
  /// Simple snackbar
  /// **********************************************************************
  void displaySnackBar(BuildContext context) {
 
      var snackBar = const SnackBar(content: Text("Simple snackbar"));
 
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }
 
  /// **********************************************************************
  /// Button for snackbar with action example
  /// **********************************************************************
  Widget buttonForSnackBarWithAction(BuildContext context) {
    return ElevatedButton(
        onPressed: () {
          displaySnackBarAction(context);
        },
        style: ElevatedButton.styleFrom(
            backgroundColor: Colors.red,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0))
        ),
        child: const Text("Press to display a snackbar with action")
    );
  }
 
  /// **********************************************************************
  /// Snackbar with action example
  /// **********************************************************************
  void displaySnackBarAction(BuildContext context) {
 
    var snackBar = SnackBar(
        content: const Text("Action launched..."),
        action: SnackBarAction(
            label: "Cancel",
            onPressed: () {
              print("Action canceled"); },
            ),
    );
 
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
 
  }
}

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *