@@ -25,46 +25,52 @@ class MainWidget extends StatefulWidget {
25
25
}
26
26
27
27
class MainWidgetState extends State <MainWidget > {
28
- double currentSliderValue = 20 ;
29
- ScaffoldFeatureController <MaterialBanner , MaterialBannerClosedReason >?
30
- controller;
28
+
29
+ final FocusNode dismissButtonFocusNode = FocusNode ();
30
+ final FocusNode showButtonFocusNode = FocusNode ();
31
+
32
+ @override
33
+ void dispose () {
34
+ dismissButtonFocusNode.dispose ();
35
+ showButtonFocusNode.dispose ();
36
+ super .dispose ();
37
+ }
38
+
39
+ void hideBanner () {
40
+ ScaffoldMessenger .of (context).hideCurrentMaterialBanner ();
41
+ showButtonFocusNode.requestFocus ();
42
+ }
43
+
44
+ void showBanner () {
45
+ ScaffoldMessenger .of (context).showMaterialBanner (
46
+ MaterialBanner (
47
+ padding: const EdgeInsets .all (20 ),
48
+ content: const Text ('Hello, I am a Material Banner' ),
49
+ leading: const Icon (Icons .agriculture_outlined),
50
+ backgroundColor: Colors .yellowAccent,
51
+ actions: < Widget > [
52
+ TextButton (
53
+ focusNode: dismissButtonFocusNode,
54
+ onPressed: hideBanner,
55
+ child: const Text ('DISMISS' ),
56
+ ),
57
+ ],
58
+ ),
59
+ );
60
+ dismissButtonFocusNode.requestFocus ();
61
+ }
31
62
32
63
@override
33
64
Widget build (BuildContext context) {
34
- VoidCallback ? onPress;
35
- if (controller == null ) {
36
- onPress = () {
37
- setState (() {
38
- controller = ScaffoldMessenger .of (context).showMaterialBanner (
39
- MaterialBanner (
40
- padding: const EdgeInsets .all (20 ),
41
- content: const Text ('Hello, I am a Material Banner' ),
42
- leading: const Icon (Icons .agriculture_outlined),
43
- backgroundColor: Colors .green,
44
- actions: < Widget > [
45
- TextButton (
46
- onPressed: () {
47
- controller! .close ();
48
- setState (() {
49
- controller = null ;
50
- });
51
- },
52
- child: const Text ('DISMISS' ),
53
- ),
54
- ],
55
- ),
56
- );
57
- });
58
- };
59
- }
60
65
return Scaffold (
61
66
appBar: AppBar (
62
67
backgroundColor: Theme .of (context).colorScheme.inversePrimary,
63
68
title: Semantics (headingLevel: 1 , child: const Text ('MaterialBanner Demo' )),
64
69
),
65
70
body: Center (
66
71
child: ElevatedButton (
67
- onPressed: onPress,
72
+ focusNode: showButtonFocusNode,
73
+ onPressed: showBanner,
68
74
child: const Text ('Show a MaterialBanner' ),
69
75
),
70
76
),
0 commit comments