How to limit draggable scrollable sheet to take height according to its child height in flutter?
4,834
I was struggling with this for a while, and then discovered that the correct way to achieve this is to use ClampingScrollPhysics as the physics parameter of the scroll view.
https://api.flutter.dev/flutter/widgets/ClampingScrollPhysics-class.html
Author by
Umair
Updated on December 18, 2022Comments
-
Umair over 1 year
I am using
draggableScrollableSheet
. I am giving these parametersDraggableScrollableSheet(initialChildSize: 0.4,maxChildSize: 1,minChildSize: 0.4,builder: (BuildContext context, ScrollController scrollController) { return SingleChildScrollView(controller: scrollController, child: Theme( data: Theme.of(context).copyWith(canvasColor: Colors.transparent), child: Opacity( opacity: 1, child: IntrinsicHeight( child: Column(mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ SizedBox(height: 10,), Container( margin: EdgeInsets.only(right: 300), decoration: BoxDecoration( border: Border( top: BorderSide( color: Colors.blue, width: 3, style: BorderStyle.solid), ), ), ), Card( child: Row( children: <Widget>[ Padding( padding: const EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( S .of(context) .we_have_found_you_a_driver, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), SizedBox( height: 10, ), Text(S .of(context) .driver_is_heading_towards + ' ${widget.order.foodOrders.first.food.restaurant.name}') ], ), ), ], ), elevation: 5, ), SizedBox(height: 10,), Card( elevation: 5, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ CircleAvatar( radius: 50.0, backgroundColor: Colors.white, child: Image.asset( 'assets/img/image_not_available.jpg'), ), Expanded( child: Column(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Expanded( child: Text('Test', textAlign: TextAlign.start, style: new TextStyle( color: Colors.black, fontSize: 16.0, )), ), Icon(Icons.star, color: Colors.yellow.shade700,) ], ), SizedBox(height: 30,), Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Expanded( child: Container( child: Text('Mobile number', textAlign: TextAlign.start, style: new TextStyle( color: Colors.black, fontSize: 16.0, )), ), ), Icon(Icons.phone,), SizedBox(width: 10,), Icon(Icons.message), ], ), ], ), ) ]), ), SizedBox(height: 10,), Card( child: Align( alignment: Alignment(-1,1), child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[ Padding( padding: const EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( S .of(context) .you_ordered_from + ' ${widget.order.foodOrders.first.food.restaurant.name}', style: TextStyle( color: Colors.grey, ), ), SizedBox( height: 5, ), Column(children: List.generate(widget.order.foodOrders.length,(index) { return Text( '${widget.order.foodOrders[index].food.name}' ); },),), Row( children: <Widget>[ Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text('See details', style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue),), ], ), ], ), ], ), ), Padding( padding: const EdgeInsets.all(8.0), child: Column( children: <Widget>[ SizedBox(height: 40,), Row( children: <Widget>[ Icon(Icons.monetization_on), Text(widget.order.foodOrders .first.price .toString()), ], ), ], ), ), ], ), ), elevation: 5, ), ], ), ), ), ), )
and I also used a single child scroll view and column so that I can show my cards in that column of draggableScrollableSheet. But I want draggableScrollableSheet to take height dynamically instead of defining size. Like now I want to show only 2 to 3 cards and that is taking full screen. But I want it to take the minimum height of the screen. How can we achieve this?
-
Umair about 4 yearsHow you had given height to draggablescrollablesheet. Like if in your case, if i add another textformfield then layout adjust itself in that dynamically or we have to set height statically?
-
Pompidou about 4 yearsim using SingleChildScrollView with Column , the column main axis size is set to minimun, that means your column size grows with respect to your children
-
Umair about 4 yearsI just reeditted my question. Now i also included code in my question. I had given minimum height to column too. Can you please see where i am doing wrong?
-
Pompidou about 4 yearsthe child card column isn't set to minimum
-
Umair about 4 yearsyes, i check that. But did expanded widget also creating problem in above code?
-
Victor Roberti Camolesi over 3 yearsI'm having the same problem, I need my sheet height to adjust to its children height, aka a way to get children height before it's rendered.
-
pjdupreez over 3 yearspoor answer. no example