Skip to content

Instantly share code, notes, and snippets.

@Sp4Rx
Created September 27, 2022 21:31
Show Gist options
  • Save Sp4Rx/97f87f6b4dfda55ccb11d97f539ebf9b to your computer and use it in GitHub Desktop.
Save Sp4Rx/97f87f6b4dfda55ccb11d97f539ebf9b to your computer and use it in GitHub Desktop.
Sticky bottom sheet
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: const Scaffold(
body: Page1(),
),
);
}
}
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: StickyPageWrapper(
child: Container(
color: Colors.white38,
child: Center(
child: ElevatedButton(
child: const Text('Go to page 2'),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const Page2()));
},
)),
)),
);
}
}
class StickyPageWrapper extends StatelessWidget {
final Widget child;
const StickyPageWrapper({Key? key, required this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Stack(
fit: StackFit.expand,
children: [
child,
Positioned(
left: 0,
bottom: 0,
child: Hero(
tag: 'bottom_sheet',
child: Container(
color: Colors.orange,
height: size.height / 4,
width: size.width,
),
),
)
],
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: StickyPageWrapper(
child: Container(
color: Colors.white38,
child: Center(
child: ElevatedButton(
child: const Text('Go back to page 1'),
onPressed: () {
Navigator.of(context).pop();
},
)),
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment