Problem
So basically it's quite an old problem, which I couldn't fix with Google. The problem is that DraggableScrollableSheet
doesn't size its maximum size based on its content size, but with only a static value maxChildSize
, which is just not good if you don't want to look at a lot of empty spaces in your sheet.
Any Solution?
Does anyone know some hack to set DragabbleScrollableSheet maxChildSize
based on its content size or give any alternatives to solve this issue?
Test project
I created a small application just for demonstrating the issue.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () => showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (_) => DraggableScrollableSheet(
initialChildSize: 0.8,
minChildSize: 0.3,
maxChildSize: 0.8,
expand: false,
builder: (_, controller) =>
ListView(
shrinkWrap: true,
controller: controller,
children: <Widget>[
Container(
color: Colors.red,
height: 125.0
),
Container(
color: Colors.white,
height: 125.0
),
Container(
color: Colors.green,
height: 125.0
),
],
)
)
),
child: const Text("Show scrollable sheet"),
),
],
),
),
);
}
}
- I tried with GlobalKey to get the size of Listview, which I don't think is possible, because it's just buggy and slow.
- I also tried LayoutBuilder, but with no serious results.