How to Show Snackbar at Top of the Screen

Flutter showsnackbar at top of the screen?

You can use Getx library for it, you can customize it accordingly.

 Get.snackbar(
"Title of Snackbar",
"Message of SnackBar",
icon: Icon(Icons.person, color: Colors.white),
snackPosition: SnackPosition.TOP,
);

Is there any way to achieve a Flutter top snackbar?

You can use https://pub.dartlang.org/packages/flushbar

RaisedButton(
child: Text('Show Top Snackbar'),
onPressed: () {
Flushbar(
flushbarPosition: FlushbarPosition.TOP,
)
..title = "Hey Ninja"
..message = "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
..duration = Duration(seconds: 3)
..show(context);
},
),

Display SnackBar on top of AlertDialog widget

The issue here is that showDialog uses the root navigator provided by MaterialApp. So when you show your dialog it is pushed completely over your scaffold. To solve this you need the navigator that is used to be a child of the scaffold that's showing the snackbars. So the following code adds this navigator, sets useRootNavigator to false to use this navigator, and importantly uses a BuildContext under the newly created navigator:

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Navigator( //New navigator added here
initialRoute: '/',
onGenerateRoute: (setting) {
return MaterialPageRoute(
builder: (context) => Center(
child: Builder(builder: (context) {
WidgetsBinding.instance!
.addPostFrameCallback((_) async {
showDialog(
context: context,
useRootNavigator: false,//Dialog must not use root navigator
builder: (BuildContext dialogContext) =>
AlertDialog(
content: GestureDetector(
onTap: () {
ScaffoldMessenger.of(dialogContext)
.showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () {},
),
));
},
child: Center(
child: Text('Show SnackBar!'),
),
),
),
);
});
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
]);
}),
));
}),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

Result:
Sample Image


Note that this solution does constrain the dialog size a bit and the app bar and floating action button is above the content, which may be undesirable. This can be solved just by adding another scaffold below the newly created navigator and moving those appbar/FAB properties down as desired. Example with AppBar below the modal:

@override
Widget build(BuildContext context) {
return Scaffold(
body: Navigator(
initialRoute: '/',
onGenerateRoute: (setting) {
return MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Builder(builder: (context) {
WidgetsBinding.instance!
.addPostFrameCallback((_) async {
showDialog(
context: context,
useRootNavigator: false,
builder: (BuildContext dialogContext) =>
AlertDialog(
content: GestureDetector(
onTap: () {
ScaffoldMessenger.of(dialogContext)
.showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () {},
),
));
},
child: Center(
child: Text('Show SnackBar!'),
),
),
),
);
});
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
]);
}),
)));
}),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

Result:
Sample Image

SnackBar from top. Is this possible?

No it is not possible. The documentation states that

They show a brief message at the bottom of the screen on mobile and
lower left on larger devices. Snackbars appear above all other
elements on screen and only one can be displayed at a time.

You could use a third part library, like Crouton for instance

How to show a Snackbar in Android on the top when using ConstraintLayout in a nice way

You can use the first piece of code and do the following:

Add this line to resolve the animation issue :

snackbar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE);

If this doesn't works try this:

snackbar.setAnchorView(*mention viewId above whom you want to show SnackBar*)

Is there a way/workaround to show a snackbar on top of the blurred dialog with Flutter?

you can bring up the dialog by creating a custom one, like my code below

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
bool isShow = false;
void _incrementCounter() {
setState(() {
isShow = !isShow;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: [
ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: const FlutterLogo(),
),
isShow
? GestureDetector(
onTap: _incrementCounter,
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.black38,
alignment: Alignment.center,
child: GestureDetector(
onTap: () {},
child: AlertDialog(
content: const Text("dsd"),
actions: [
ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Awesome Snackbar!'),
action: SnackBarAction(
label: 'Action',
onPressed: () {
// Code to execute.
},
),
),
);
},
child: const Text("Show Snackbar"),
)
],
),
),
),
)
: const SizedBox()
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.notifications),
),
);
}
}

screenshot : https://i.stack.imgur.com/inX4I.png

I can't display the image due to lack of reputation points



Related Topics



Leave a reply



Submit