点击Get.snackbar,出现错误 No Overlay widget found. Some widgets require an Overlay widget ancestor for correct operation.
点击Get.snackbar,出现错误 No Overlay widget found. Some widgets require an Overlay widget ancestor for correct operation.
happens since upgrade to flutter 3.38.1
add builder inside GetMaterialApp will fix
Added this in your main.dart
// ----------- BUILDER UNTUK OVERLAY -----------
builder: (context, child) {
return Overlay(
initialEntries: [OverlayEntry(builder: (_) => child!)],
);
},
EXAMPLE
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:hazard_komik/home_webview_screen.dart';
import 'package:hazard_komik/test.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
// home: HomeWebViewScreen(),
home: TextScreen(),
// ----------- BUILDER UNTUK OVERLAY -----------
builder: (context, child) {
return Overlay(
initialEntries: [OverlayEntry(builder: (_) => child!)],
);
},
//
);
}
}
class TextScreen extends StatelessWidget {
const TextScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: GestureDetector(
onTap: () {
Get.snackbar("OK", "Snackbar Show");
print("Snackbar Print");
},
child: Container(
decoration: BoxDecoration(color: Colors.green),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 10),
child: Text(
"Button Snackbar",
style: TextStyle(color: Colors.white),
),
))),
)
],
),
);
}
}
Added this in your main.dart
// ----------- BUILDER UNTUK OVERLAY ----------- builder: (context, child) { return Overlay( initialEntries: [OverlayEntry(builder: (_) => child!)], ); }, EXAMPLE
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get/get_navigation/src/root/get_material_app.dart'; import 'package:hazard_komik/home_webview_screen.dart'; import 'package:hazard_komik/test.dart';
void main() { runApp(const MyApp()); }
class MyApp extends StatelessWidget { const MyApp({super.key});
// This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // TRY THIS: Try running your application with "flutter run". You'll see // the application has a purple toolbar. Then, without quitting the app, // try changing the seedColor in the colorScheme below to Colors.green // and then invoke "hot reload" (save your changes or press the "hot // reload" button in a Flutter-supported IDE, or press "r" if you used // the command line to start the app). // // Notice that the counter didn't reset back to zero; the application // state is not lost during the reload. To reset the state, use hot // restart instead. // // This works for code too, not just values: Most code changes can be // tested with just a hot reload. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), // home: HomeWebViewScreen(), home: TextScreen(),
// ----------- BUILDER UNTUK OVERLAY ----------- builder: (context, child) { return Overlay( initialEntries: [OverlayEntry(builder: (_) => child!)], ); }, // );} }
class TextScreen extends StatelessWidget { const TextScreen({super.key});
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Center( child: GestureDetector( onTap: () { Get.snackbar("OK", "Snackbar Show"); print("Snackbar Print"); }, child: Container( decoration: BoxDecoration(color: Colors.green), child: Padding( padding: const EdgeInsets.symmetric( horizontal: 20, vertical: 10), child: Text( "Button Snackbar", style: TextStyle(color: Colors.white), ), ))), ) ], ), ); } }
it works with this. thanks! :)
I've sent a patch to fix this, please check if it resolves the issue for you.
@jonataslaw it resolves the issue for me,thanks!
@jonataslaw Please fix in prerelease 5.0.0-release-candidate-9.3.2 too
my code works again using this hot fix / workaround 👍
// ----------- BUILDER UNTUK OVERLAY -----------
builder: (context, child) {
return Overlay(
initialEntries: [OverlayEntry(builder: (_) => child!)],
);
},
please notify me how to migrate from using the workaround when this bug solved, thank you 👍