Skip to content

Commit 3149bb8

Browse files
author
Jens Becker
committed
feat: add Flashbar extensions to BuildContext
1 parent 1923885 commit 3149bb8

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The goal is that by using this package you have to write less (repeating) and mo
2121
- [Extensions on `List<DateTime>`](#extensions-on-listdatetime)
2222
- [Extensions on `BuildContext` (Adaptive helpers)](#extensions-on-buildcontext-adaptive-helpers)
2323
- [Extensions on `BuildContext` (Navigation helpers)](#extensions-on-buildcontext-navigation-helpers)
24+
- [Extensions on `BuildContext` (Flashbars)](#extensions-on-buildcontext-flashbars)
2425
- [Extensions on `List?`](#extensions-on-list)
2526
- [Extensions on `List`](#extensions-on-list-1)
2627
- [`Date` helper class](#date-helper-class)
@@ -111,6 +112,17 @@ All features with links to their page in the documentation:
111112
- #### [dismissKeyboard()](https://pub.dev/documentation/fleasy/latest/fleasy/NavigationContextExtensions/dismissKeyboard.html)
112113
Dismisses the keyboard.
113114

115+
- ### Extensions on `BuildContext` (Flashbars):
116+
The extensions use the awesome [flash](https://pub.dev/packages/flash) package:
117+
- #### [showSuccessFlashbar(...)](https://pub.dev/documentation/fleasy/latest/fleasy/FlashbarContextExtensions/showSuccessFlashbar.html)
118+
Shows a success flashbar/toast with a customizable text.
119+
- #### [showInfoFlashbar(...)](https://pub.dev/documentation/fleasy/latest/fleasy/FlashbarContextExtensions/showInfoFlashbar.html)
120+
Shows an info flashbar/toast with a customizable text.
121+
- #### [showErrorFlashbar(...)](https://pub.dev/documentation/fleasy/latest/fleasy/FlashbarContextExtensions/showErrorFlashbar.html)
122+
Shows an error flashbar/toast with a customizable text (optional).
123+
- #### [showNoConnectionFlashbar()](https://pub.dev/documentation/fleasy/latest/fleasy/FlashbarContextExtensions/showNoConnectionFlashbar.html)
124+
Shows a no connection flashbar/toast with a customizable text (optional).
125+
114126

115127
- ### Extensions on `List?`:
116128
- #### [isNotBlank](https://pub.dev/documentation/fleasy/latest/fleasy/NullableListExtensions/isNotBlank.html)

lib/src/extensions/context_extensions.dart

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:flash/flash.dart';
2+
import 'package:fleasy/fleasy.dart';
13
import 'package:flutter/material.dart';
24

35
/// Class which defines the form factor breakpoints:
@@ -108,3 +110,98 @@ extension NavigationContextExtensions on BuildContext {
108110
/// (by removing the focus on this node by moving the primary focus to another node).
109111
void dismissKeyboard() => FocusScope.of(this).unfocus();
110112
}
113+
114+
extension FlashbarContextExtensions on BuildContext {
115+
void _showFlashbar(
116+
String message, {
117+
required Color iconColor,
118+
required IconData icon,
119+
int duration = 3,
120+
}) {
121+
showFlash<Flash>(
122+
context: this,
123+
duration: Duration(seconds: duration),
124+
transitionDuration: const Duration(milliseconds: 750),
125+
builder: (context, controller) {
126+
return Flash<Flash>.bar(
127+
controller: controller,
128+
backgroundColor: const Color(0xFF303030),
129+
borderRadius: BorderRadius.circular(10),
130+
margin: const EdgeInsets.all(Insets.l),
131+
child: FlashBar(
132+
content: Text(
133+
message,
134+
style: const TextStyle(fontSize: 14.0, color: Colors.white),
135+
),
136+
icon: Icon(
137+
icon,
138+
color: iconColor,
139+
size: 28,
140+
),
141+
),
142+
);
143+
},
144+
);
145+
}
146+
147+
/// Shows a success flashbar/toast with a customizable text.
148+
///
149+
/// It uses [flash](https://pub.dev/packages/flash).
150+
void showSuccessFlashbar({
151+
required String message,
152+
int duration = 3,
153+
}) {
154+
_showFlashbar(
155+
message,
156+
duration: duration,
157+
icon: Icons.check_rounded,
158+
iconColor: Colors.green[400]!,
159+
);
160+
}
161+
162+
/// Shows an info flashbar/toast with a customizable text.
163+
///
164+
/// It uses [flash](https://pub.dev/packages/flash).
165+
void showInfoFlashbar({
166+
required String message,
167+
int duration = 3,
168+
}) {
169+
_showFlashbar(
170+
message,
171+
duration: duration,
172+
icon: Icons.info_outline_rounded,
173+
iconColor: Colors.blue[400]!,
174+
);
175+
}
176+
177+
/// Shows an error flashbar/toast with a customizable text (optional).
178+
///
179+
/// It uses [flash](https://pub.dev/packages/flash).
180+
void showErrorFlashbar({
181+
String message = 'Oops, something went wrong.',
182+
int duration = 3,
183+
}) {
184+
_showFlashbar(
185+
message,
186+
duration: duration,
187+
icon: Icons.error_outline_rounded,
188+
iconColor: Colors.red[400]!,
189+
);
190+
}
191+
192+
/// Shows a no connection flashbar/toast with a customizable text (optional).
193+
///
194+
/// It uses [flash](https://pub.dev/packages/flash).
195+
void showNoConnectionFlashbar({
196+
String message =
197+
'A connection to the server cannot be established. Are you connected to the internet?',
198+
int duration = 3,
199+
}) {
200+
_showFlashbar(
201+
message,
202+
duration: duration,
203+
icon: Icons.wifi_off_rounded,
204+
iconColor: Colors.red[400]!,
205+
);
206+
}
207+
}

pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ dependencies:
1414
intl: ^0.17.0
1515
universal_platform: ^1.0.0+1
1616
font_awesome_flutter: ^9.1.0
17+
flash: ^2.0.1
1718

1819
dev_dependencies:
1920
test: ^1.17.8
2021
flutter_lints: ^1.0.3
21-
dart_code_metrics: ^4.0.0
22-
23-
22+
dart_code_metrics: ^4.0.0

0 commit comments

Comments
 (0)