I am building an application using Flutter, and I am using Firebase as an backend. Suddenly, I need to use snackbar to show the user message about authentication. For example: user-not-found, password or email is wrong. Then I used default snackbar, it worked pretty well, but I wanted it to be appealing on the eye. So eye created custom snackbar, needless to say I used chatgpt for some parts. Here is how you can create custom snackbar with the gradient background color.
import 'package:flutter/material.dart';
class CustomGradientSnackbar {
final String text;
final Gradient gradient;
final Color textColor;
CustomGradientSnackbar({
required this.text,
required this.gradient,
required this.textColor,
});
void show(BuildContext context) {
OverlayState? overlayState = Overlay.of(context);
OverlayEntry overlayEntry;
overlayEntry = OverlayEntry(
builder: (BuildContext context) => Positioned(
bottom: 0,
left: 0,
right: 0,
child: Material(
elevation: 6.0,
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
decoration: BoxDecoration(
gradient: gradient,
),
child: Text(
text,
style: TextStyle(
color: textColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
overlayState.insert(overlayEntry);
Future.delayed(const Duration(seconds: 2)).then((_) {
overlayEntry.remove();
});
}
}
Result:
If anyone knows better way of creating custom gradient snackbar, feel free to answer.
ElevatedButton(
onPressed: () {
Flushbar(
// title: "User not found",
// message: 'User not found.',
messageText: const Text(
'User not found',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500),
),
duration: const Duration(seconds: 3),
backgroundGradient: const LinearGradient(colors: [
Color(0xffD95F30),
Color.fromRGBO(181, 120, 44, 1),
Color.fromRGBO(167, 149, 48, 1)
]), //here's the gradient support
).show(context);
},
child: const Text('click'))