I couldn’t find a solution for displaying a snackbar after clicking on an IconButton which is inside the AppBar. The difficulty here is that the appBar() is shifted to an own file/class called myAppBar.dart.
Calling the appBar from a specific view:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const PreferredSize(
preferredSize: Size.fromHeight(50),
child: **MyAppBar**(
title: "My Home",
),
),
AppBar.dart Code:
import 'package:flutter/material.dart';
class MyAppBar extends StatelessWidget {
const MyAppBar({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return AppBar(
elevation: 0,
title: Text(title),
backgroundColor: Color.fromRGBO(72, 75, 80, 1),
actions: <Widget>[
Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(
Icons.refresh,
color: Colors.white,
size: 30,
),
onPressed: () {
//print("success");
**final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));**
// *What can I write here??*
},
);
},
),
],
centerTitle: true,
bottom: const PreferredSize(
preferredSize: Size.fromHeight(10),
child: Divider(
thickness: 0.3,
height: 0,
color: Color(0xffAEADB2),
)),
);
}
}
The following solution doesn’t work:
Scaffold.of(context).showSnackBar(snackBar);
child: MyAppBar(
title: "Nik's Home",
Scaffold.of(context).showSnackBar(snackBar); || **Expected to find ")".**
),
3