Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active July 30, 2025 18:01
Show Gist options
  • Save slightfoot/a69a98ec1727877caafb1b3ba1582f27 to your computer and use it in GitHub Desktop.
Save slightfoot/a69a98ec1727877caafb1b3ba1582f27 to your computer and use it in GitHub Desktop.
Bottom Sheet with scrollable input fields - by Simon Lightfoot :: #HumpdayQandA on 30th July 2025 :: https://www.youtube.com/watch?v=mvIfPpbZr4c
// MIT License
//
// Copyright (c) 2025 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/material.dart';
void main() {
runApp(const App());
}
class App extends StatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
}
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: ElevatedButton(
onPressed: () {
showModalBottomSheet(
context: context,
enableDrag: false,
isScrollControlled: true,
builder: (_) => const FormInputBottomSheet(count: 10),
);
},
child: const Text('Show Sheet'),
),
),
);
}
}
class FormInputBottomSheet extends StatelessWidget {
const FormInputBottomSheet({
super.key,
required this.count,
});
final int count;
@override
Widget build(BuildContext context) {
final rootViewPadding = MediaQueryData.fromView(View.of(context)).viewPadding;
final viewInsets = MediaQuery.viewInsetsOf(context);
return ViewInsetsPadding(
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.7,
),
child: IntrinsicHeight(
child: SingleChildScrollView(
padding: EdgeInsets.only(
top: viewInsets.bottom > 0.0 ? rootViewPadding.top : 0.0,
),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 24.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
spacing: 16.0,
children: [
for (int i = 0; i < count; i++) //
//
TextFormField(
decoration: InputDecoration(
labelText: 'Field $i',
),
),
Padding(
padding: const EdgeInsets.all(12.0),
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Submit'),
),
),
],
),
),
),
),
),
);
}
}
class ViewInsetsPadding extends StatelessWidget {
const ViewInsetsPadding({
super.key,
required this.child,
});
final Widget child;
@override
Widget build(BuildContext context) {
final viewInsets = MediaQuery.viewInsetsOf(context);
return Padding(
padding: EdgeInsets.only(bottom: viewInsets.bottom),
child: child,
);
}
}
class ChildBuilder extends StatelessWidget {
const ChildBuilder({
super.key,
required this.builder,
this.child,
});
final TransitionBuilder builder;
final Widget? child;
@override
Widget build(BuildContext context) => builder(context, child);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment