Created with <3 with dartpad.dev.
Last active
February 2, 2023 14:09
-
-
Save oravecz/fa0ad4fce899713d2f10b2b30a0cab28 to your computer and use it in GitHub Desktop.
Flutter Full Page Overlay (Working)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyAppWrapper(title: 'Flutter Full Page Overlay'), | |
); | |
} | |
} | |
class LoadingOverlay { | |
LoadingOverlay(); | |
OverlayEntry? _overlay; | |
void show(BuildContext context) { | |
if (_overlay == null) { | |
_overlay = OverlayEntry( | |
builder: (context) => Container( | |
alignment: Alignment.center, | |
color: const Color(0xc0000000), | |
height: double.infinity, | |
width: double.infinity, | |
child: Container( | |
color: Colors.white, | |
padding: const EdgeInsets.all(24), | |
child: ElevatedButton( | |
onPressed: hide, | |
child: const Text('Close Overlay'), | |
), | |
), | |
), | |
); | |
Overlay.of(context).insert(_overlay!); | |
} | |
} | |
void hide() { | |
if (_overlay != null) { | |
_overlay!.remove(); | |
_overlay = null; | |
} | |
} | |
} | |
class MyAppWrapper extends StatelessWidget { | |
const MyAppWrapper({required this.title}); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text("Nested App Bar"),), | |
bottomNavigationBar: BottomNavigationBar( | |
backgroundColor: Colors.white, | |
items: const [ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.home), | |
label: 'Home', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.business), | |
label: 'Business', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.school), | |
label: 'School', | |
), | |
] | |
), | |
body: const MyHomePage(title: 'Nested Scaffold'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final _overlay = LoadingOverlay(); | |
void _showOverlay() { | |
_overlay.show(context); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: const Center( | |
child: Text('Press the FAB to show the overlay'), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _showOverlay, | |
tooltip: 'overlay', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment