Skip to content

Instantly share code, notes, and snippets.

@matuella
Created May 28, 2021 17:29
Show Gist options
  • Save matuella/0ed3658f9d9de6f8c135ef5217e91462 to your computer and use it in GitHub Desktop.
Save matuella/0ed3658f9d9de6f8c135ef5217e91462 to your computer and use it in GitHub Desktop.
[Question] How to maintain same bottom sheet state between multiple scaffolds?
import 'package:flutter/material.dart';
void main() => runApp(NestedBottomSheetApp());
class NestedBottomSheetApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Builder(
builder: (context) => ElevatedButton(
child: Text('Go To Details'),
onPressed: () {
Navigator.of(context).push<dynamic>(
MaterialPageRoute<dynamic>(
builder: (context) => NestedPage(),
),
);
},
),
),
bottomSheet: MyDraggableSheet(),
),
);
}
}
class MyDraggableSheet extends StatefulWidget {
@override
_MyDraggableSheetState createState() => _MyDraggableSheetState();
}
class _MyDraggableSheetState extends State<MyDraggableSheet> {
int _totalPressed = 0;
@override
Widget build(BuildContext context) {
return DraggableScrollableSheet(
minChildSize: 0.2,
initialChildSize: 0.2,
expand: false,
builder: (context, scrollController) {
return Container(
width: double.infinity,
color: Colors.blue.shade100,
child: ListView.builder(
controller: scrollController,
itemCount: 50,
itemBuilder: (context, index) => TextButton(
onPressed: () => setState(() => _totalPressed++),
child: Text('Button $index | all pressed: $_totalPressed'),
),
),
);
},
);
}
}
class NestedPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Details'),
),
body: Center(
child: ElevatedButton(
child: Text('Go Back'),
onPressed: Navigator.of(context).pop,
),
),
bottomSheet: MyDraggableSheet(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment