Skip to content

Instantly share code, notes, and snippets.

@maheshmnj
Last active January 24, 2022 06:40
Show Gist options
  • Save maheshmnj/ea1f48220c7f3f0b687a71501a8334a4 to your computer and use it in GitHub Desktop.
Save maheshmnj/ea1f48220c7f3f0b687a71501a8334a4 to your computer and use it in GitHub Desktop.
Example showing dynamic height of rounded bottomSheet,This sheet gets scrollable when it exceeds the Max available height originally posted: https://stackoverflow.com/a/64504274/8253662
// 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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget _item(int index) {
return SwitchListTile(
title: Text('Item $index'), value: false, onChanged: (x) {});
}
Future<void> _showBottomSheet() async {
return showModalBottomSheet(
isScrollControlled: true,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(13)),
backgroundColor: Colors.white,
context: context,
builder: (context) => Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
// mainAxisSize: MainAxisSize.min,
children: List.generate(kItems, (index) => _item(index))))),
);
}
int kItems = 6;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Example showing dynamic height of Rounded bottom Sheet \n\n modify kItems to see the dynamic height of the bottom sheet',
textAlign: TextAlign.center,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _showBottomSheet(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment