If you try to use ScaffoldManager to display a SnackBar and you have multiple scaffold you will get multiple snackbar
Code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Scaffold(
bottomNavigationBar: BottomAppBar(
color: Colors.red,
child: Row(
children: [
SizedBox(height: 45,)
],
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Secondpage(),
),
);
},
child: Text('change to next page'),
)
],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {},
),
),
);
}
}
class Secondpage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomAppBar(
color: Colors.red,
child: Row(
children: [
SizedBox(height: 45,)
],
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(
onPressed: () {
final snackBar = SnackBar(
content: Text('Snackbar'),
behavior: SnackBarBehavior.floating,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: Text('Display snackbar'),
),
TextButton(
onPressed: () {
Navigator.pop(context, null);
},
child: Text('Back'),
)
],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {},
),
);
}
}
flutter doctor -v
[√] Flutter (Channel stable, 2.0.0, on Microsoft Windows [Versione 10.0.19042.844], locale it-IT)
• Flutter version 2.0.0 at C:\flutter
• Framework revision 60bd88df91 (20 hours ago), 2021-03-03 09:13:17 -0800
• Engine revision 40441def69
• Dart version 2.12.0
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\Damia\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.4)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.7.30517.126
• Windows 10 SDK version 10.0.18362.0
[√] Android Studio (version 4.1.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] VS Code (version 1.53.2)
• VS Code at C:\Users\Damia\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.20.0
[√] Connected device (4 available)
• Mi 9T (mobile) • e58120aa • android-arm64 • Android 10 (API 29)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Versione 10.0.19042.844]
• Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.72
• Edge (web) • edge • web-javascript • Microsoft Edge 88.0.705.81
• No issues found!
Hi @ferraridamiano thanks for reporting this! The ScaffoldMessenger is supposed to handle nested Scaffolds for you, but it looks like you found a place where it missed this case. Thank you!
It looks like it also causes an exception to be thrown since the hero tags are the same for those duped SnackBars.
I have a fix already on the way for this, just writing the tests now. 🙂