Unable to multi-select check boxes in bottom sheet - flutter

I am trying to list out a few texts after a bottom sheet opens, this is dynamic and comes from an API. Once the bottom sheet function is triggered, the API is called and the list view updates. In this list view I used CheckboxListTile, the problem is I am not able to do multiple selections (nor single select) on the checkboxes.
This is what I have so far:
var selectedIndex = [];
The above code is in the _MainScreenState and the function for the bottom screen is triggered in one of the buttons as:
...
onPressed: () {
_showModalBottomSheet(context).then((value) => setState(() {
index = value;
}));
}
...
Bottom sheet code
Future<AllApps?> _showModalBottomSheet(BuildContext context) {
return showModalBottomSheet<AllApps>(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
clipBehavior: Clip.antiAliasWithSaveLayer,
context: context,
isScrollControlled: true,
builder: (context) {
return FractionallySizedBox(
heightFactor: 0.9,
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(150.0, 20.0, 150.0, 20.0),
child: Container(
height: 8.0,
width: 80.0,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(20.0),
),
),
),
FutureBuilder<AllApps>(
future: getAllApps(), // <- API call
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: snapshot.data?.data.length,
itemBuilder: (context, index) {
final app = snapshot.data?.data[index];
return CheckboxListTile(
enableFeedback: true,
title: Text(app!.name),
value: selectedIndex.contains(app.id),
onChanged: (_) {
if (selectedIndex.contains(app.id)) {
setState(() {
selectedIndex.remove(app.id);
});
} else {
setState(() {
selectedIndex.add(app.id);
});
}
},
);
},
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return const Center(child: CircularProgressIndicator());
},
),
Padding(
padding: const EdgeInsets.only(top: 20.0, right: 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: ElevatedButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
),
ElevatedButton(
child: const Text('Save'),
onPressed: () {
Navigator.pop(context);
},
),
],
),
)
],
),
);
});
}
I am able to see the lists being built but I am not able to select any one of them (gif screenshot):
How should I enable multiple selections?

In order to apply changes in the state to the modal, use StateFulBuilder:
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return StatefulBuilder( // this is new
builder: (BuildContext context, StateSetter setState) {
return FractionallySizedBox(
heightFactor: 0.9,
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(150.0, 20.0, 150.0, 20.0),
child: Container(
height: 8.0,
width: 80.0,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(20.0),
),
),
),
FutureBuilder<AllApps>(
future: getAllApps(), // <- API call
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: snapshot.data?.data.length,
itemBuilder: (context, index) {
final app = snapshot.data?.data[index];
return CheckboxListTile(
enableFeedback: true,
title: Text(app!.name),
value: selectedIndex.contains(app.id),
onChanged: (_) {
if (selectedIndex.contains(app.id)) {
setState(() {
selectedIndex.remove(app.id);
});
} else {
setState(() {
selectedIndex.add(app.id);
});
}
},
);
},
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return const Center(child: CircularProgressIndicator());
},
),
Padding(
padding: const EdgeInsets.only(top: 20.0, right: 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: ElevatedButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
),
ElevatedButton(
child: const Text('Save'),
onPressed: () {
Navigator.pop(context);
},
),
],
),
)
],
),
);
});
});

You can use StatefulBuilder for providing setState in bottom sheet. Try as follows:
return StatefulBuilder(
builder:(BuildContext context,setState){
return FractionallySizedBox(....);
})

Related

listview can not scrolling down inside sliding_up_panel flutter

I have used panelBuilder to add a ListView and inside ListView, I have used ListView to show my list but when I scrolled down the inside ListView, panel also scrolls down. This is my code.
SlidingUpPanel(
controller: _panelController,
maxHeight: _panelHeightOpen,
minHeight: _panelHeightClosed,
parallaxEnabled: true,
parallaxOffset: .5,
borderRadius: const BorderRadius.only(topLeft: Radius.circular(30.0), topRight: Radius.circular(30.0)),
color: const Color(0xFFD6EDFF),
// Maps
body: Padding(
padding: EdgeInsets.only(bottom: _paddingBottomMap),
child: GoogleMap()
),
panelBuilder: (controller) {
return Builder(builder: (BuildContext context) {
return ListView(padding: EdgeInsets.zero, controller: controller,
children: <Widget>[
//My List
Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
child: const Text("My Favorite")
),
const SizedBox(height: 10),
Container(
height: _panelHeightOpen - 370,
width: double.maxFinite,
child: ListView.builder(
controller: _listFavContronller,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: _favLocation.length,
itemBuilder: (context, index) {
return Container(
ListTile(
onTap: () async {
_panelController.close();
await goToCurrentLocation();
},
title: Text("${_favLocation[index].locationName}")
)
);
}
)
)
]
)
}
},
onPanelSlide: (position) async {
setState(() {});
},
onPanelClosed: () {
FocusScope.of(context).requestFocus(FocusNode());
},
))
How do I fix this problem to panel will not scroll down when users scroll the inside ListView down?
Here is Video : https://drive.google.com/file/d/1mOEns6ie3KqRmGXQRenJT560brNjlQni/view?usp=sharing
In My Case it's working fine for me:
I Have use static itemCount:200
SlidingUpPanel(
controller: _panelController,
maxHeight: _panelHeightOpen,
minHeight: _panelHeightClosed,
parallaxEnabled: true,
parallaxOffset: .5,
borderRadius: const BorderRadius.only(topLeft: Radius.circular(30.0), topRight: Radius.circular(30.0)),
color: const Color(0xFFD6EDFF),
// Maps
body: Padding(
padding: EdgeInsets.only(bottom: 10),
// child: GoogleMap()
),
panelBuilder: (controller) {
return Builder(builder: (BuildContext context) {
return ListView(padding: EdgeInsets.zero, controller: controller,
children: <Widget>[
//My List
Container(
color: Colors.yellow,
margin: const EdgeInsets.symmetric(horizontal: 20),
child: const Text("My Favorite")
),
const SizedBox(height: 10),
Container(
color: Colors.blue,
height: _panelHeightOpen - 370,
width: double.maxFinite,
child: ListView.builder(
controller: _listFavContronller,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount:200,
// itemCount: _favLocation.length,
itemBuilder: (context, index) {
return Container(
color: Colors.red,
child: ListTile(
onTap: () async {
_panelController?.close();
// await goToCurrentLocation();
},
// title: Text("${_favLocation[index].locationName}")
title: Text("Printed"),
)
);
}
)
)
]
);
}
);
},
onPanelSlide: (position) async {
setState(() {});
},
onPanelClosed: () {
FocusScope.of(context).requestFocus(FocusNode());
},
)

List view childs get out of container

I am trying to make a list view only occupy part of the screen, but it keeps growing till the end not respecting the contianer constraints. I tried to use a sizedbox too but it didn' work. List tiles outside the container are shown without any widget inside, but the background is shown anyways
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: pedidos,
builder: (context, AsyncSnapshot<List<Pedido>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: MediaQuery.of(context).size.height * 0.6,
child: ListView.builder(
itemCount: snapshot.data!.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Hero(
tag:
"pedidos_card${snapshot.data![index].idPedido}",
child: ListTile(
tileColor: Colors.white,
leading: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle),
child: Center(
child: Text(
style: Theme.of(context)
.textTheme
.headlineSmall,
"${snapshot.data![index].idPedido}"),
),
),
title: Text(
'Pedido: ${snapshot.data![index].idPedido}'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
'Estado: ${snapshot.data![index].estadoPedido.last.tipoEstadoPedido.name}'),
SizedBox(height: 10),
Text(
"Cliente: ${snapshot.data![index].cliente.nombre}")
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
trailing: Checkbox(
value: pedidosSeleccion
.contains(snapshot.data![index]),
onChanged: (bool? value) {
// value = checkboxList[index];
// setState(() {});
},
),
onTap: () {
bool isSelected = pedidosSeleccion
.contains(snapshot.data![index]);
if (isSelected) {
pedidosSeleccion
.remove(snapshot.data![index]);
} else {
pedidosSeleccion.add(snapshot.data![index]);
}
setState(() {});
},
),
));
}),
),
ElevatedButton(
onPressed: () {}, child: Text('Ver ultima milla')),
],
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
});
}
}
example
you can use Expanded instead of Sizedbox
eg:-
Column(
children:[
Expanded(flex:9,
child: ListView(
padding: const EdgeInsets.only(top: 10.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
),
),
Expanded(flex:1,
child:
ElevatedButton(
// fill in required params
),
)
])

How can I keep the two images in one screen?

How can I split the screen into two different parts. I'm learning filters and I made this code
I want to add images from the server
When I put picture number 1 or 2 for the first time, there is no problem
But when choosing the second image, the first image is deleted. I think because it refreshed the page (rebuild)
How can I keep the two images in one screen? What is the best way?
Thank you
ٍ Split the screen into two parts
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lifetb/constants/my_colors.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class BeforeAfter2 extends StatefulWidget {
const BeforeAfter2({
Key? key,
this.imageSelected1,
this.imageSelected2,
}) : super(key: key);
final imageSelected1;
final imageSelected2;
#override
_BeforeAfter2State createState() => _BeforeAfter2State();
}
/// change it to Cata
var imageSelected1;
var imageSelected2;
var imageBody = true;
var id;
var username;
var email;
getPref() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
username = preferences.getString("username");
id = preferences.getString("id");
email = preferences.getString("email");
if ( id == null) {
print("empty");
}
}
Future getData() async {
getPref();
if ( id == null) {
print("empty 2");
}
var url = "http://10.0.2.2/641984.php";
var data = {"user_id": "$id"};
var response = await http.post(Uri.parse(url), body: data);
var responsebody = jsonDecode(response.body);
return responsebody;
}
class _BeforeAfter2State extends State<BeforeAfter2> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
children: [
Expanded(
flex: 1,
child: SizedBox(
height: double.infinity,
width: double.infinity,
child: widget.imageSelected1 == null
? addImage(true,true)
: showCata()
),
),
Expanded(
flex: 1,
child: SizedBox(
height: double.infinity,
width: double.infinity,
child: widget.imageSelected2 == null
? addImage(false,true)
: showCata1()
),
),
],
),
));
}
}
addImage(addText, goToAdd) {
return Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
addText ? Text("Add 1 Photo") : Text("Add 2 Photo"),
SizedBox(height: 26),
Container(
alignment: Alignment.center,
child: FloatingActionButton(
heroTag: null,
child: Icon(
Icons.add,
color: white,
size: 45,
),
onPressed: () {
goToAdd
? showCata()
: showCata1();
},
)),
],
),
],
);
}
showCata(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Loading...'),
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15),
child: Text("You don't Have any Photo")
);
}
return GridView.builder(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder: (context, i) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: InkWell(
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected1: snapshot.data[i]["post_image"],
);
}));
},
child: Container(
margin: EdgeInsets.only(top: 10, left: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.deepPurple.shade500,
width: 2),
borderRadius:
BorderRadius.all(Radius.circular(35)),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"http://10.0.2.2/upload/" +
snapshot.data[i]["post_image"]),
)),
),
),
);
},
);
},
);
}
showCata1(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Loading...'),
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15),
child: Text("You don't Have any Photo")
);
}
return GridView.builder(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder: (context, i) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: InkWell(
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected2: snapshot.data[i]["post_image"],
);
}));
},
child: Container(
margin: EdgeInsets.only(top: 10, left: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.deepPurple.shade500,
width: 2),
borderRadius:
BorderRadius.all(Radius.circular(35)),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"http://10.0.2.2/upload/" +
snapshot.data[i]["post_image"]),
)),
),
),
);
},
);
},
);
}
remove SizedBox()s and change SafeArea with child
class _BeforeAfter2State extends State<BeforeAfter2> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
children: [
Expanded(
flex: 1,
child: widget.imageSelected1 == null
? addImage(true,true)
: showCata()
),
Expanded(
flex: 1,
child: widget.imageSelected2 == null
? addImage(false,true)
: showCata1()
),
],
),
));
}
}
addImage(addText, goToAdd) {
return Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
addText ? Text("Add 1 Photo") : Text("Add 2 Photo"),
SizedBox(height: 26),
Container(
alignment: Alignment.center,
child: FloatingActionButton(
heroTag: null,
child: Icon(
Icons.add,
color: white,
size: 45,
),
onPressed: () {
goToAdd
? showCata()
: showCata1();
},
)),
],
),
],
);
}
showCata(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Loading...'),
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15),
child: Text("You don't Have any Photo")
);
}
return GridView.builder(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder: (context, i) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: InkWell(
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected1: snapshot.data[i]["post_image"],
);
}));
},
child: Container(
margin: EdgeInsets.only(top: 10, left: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.deepPurple.shade500,
width: 2),
borderRadius:
BorderRadius.all(Radius.circular(35)),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"http://10.0.2.2/upload/" +
snapshot.data[i]["post_image"]),
)),
),
),
);
},
);
},
);
}
showCata1(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Loading...'),
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15),
child: Text("You don't Have any Photo")
);
}
return GridView.builder(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder: (context, i) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: InkWell(
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected2: snapshot.data[i]["post_image"],
);
}));
},
child: Container(
margin: EdgeInsets.only(top: 10, left: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.deepPurple.shade500,
width: 2),
borderRadius:
BorderRadius.all(Radius.circular(35)),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"http://10.0.2.2/upload/" +
snapshot.data[i]["post_image"]),
)),
),
),
);
},
);
},
);
}

How can I insert an initial element into Listview.builder

I want to show the defaultUserContainer() in case the stream is empty but also in case it's not, as an initial element of the list.
Currently. I can't seem to make either scenarios work. How can I design this better?
Column(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 16.0, top: 8.0),
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"users",
style: TextStyle(fontSize: 20.0, color: Colors.black87),
)
),
),
),
],
),
SizedBox(
height: 120,
child: FutureBuilder(
builder: (context, snapshot) {
return StreamBuilder(
stream: _firestore.collection('ts').where('userid', isEqualTo: widget.user.id).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
defaultUserContainer(); //If there's no users. tried returning it, or doing like it is here. never shows
return Text("");
} else {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot userDoc = snapshot.data.documents[index];
if(index < snapshot.data.documents.length){
return Padding(
padding: const EdgeInsets.only(bottom: 8.0, left: 8.0, right: 8.0),
child: GestureDetector(
onTap: () => {},
child: Container(
child: FittedBox(
child: Material(
color: Colors.white,
elevation: 4.0,
borderRadius: BorderRadius.circular(8.0),
shadowColor: Colors.grey,
child: Row(
children: <Widget>[
Container(
child: myDetailsContainer(userDoc), //This guy works. it's just a more complicated defaultuserContainer()
),
],
),
)
)
),
),
);
}
return ListTile(leading:defaultuserContainer()); //Doesn't show when there's users users (my goal is to always have this as an initial item)
}
);
}
},
);
},
),
)
]
);
Widget defaultUserContainer() {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Container(
height: 120,
width: 120,
color: myColors.blue,
child: Center(
child: Icon(Icons.add, size: 65, color: Colors.white),
),
),
);
}
You can define it as the first/last element of your ListView/GridView/Column/etc.
Here is a simple example with a GridView:
Full source code
import 'dart:math' show Random;
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Random Generator',
home: RandomGeneratorPage(),
),
);
}
class RandomGeneratorPage extends HookWidget {
final int max;
final random = Random();
RandomGeneratorPage({Key key, this.max = 20}) : super(key: key);
#override
Widget build(BuildContext context) {
final numbers = useState<List<int>>([]);
return Scaffold(
appBar: AppBar(title: Text('Random Generator')),
body: GridView.count(
crossAxisCount: 5,
childAspectRatio: 1,
children: [
InkWell(
onTap: () =>
numbers.value = [...numbers.value, random.nextInt(max)],
child: Card(
color: Colors.blue.shade100,
child: Icon(Icons.add),
),
),
...numbers.value
.map(
(number) => InkWell(
onTap: () => numbers.value =
numbers.value.where((x) => x != number).toList(),
child: Card(
child: Center(child: Text(number.toString())),
),
),
)
.toList(),
],
),
);
}
}
In your case:
For your particular case, it would probably look like this: [NOT TESTED]
FutureBuilder(
builder: (context, snapshot) {
return StreamBuilder(
stream: _firestore
.collection('ts')
.where('userid', isEqualTo: widget.user.id)
.snapshots(),
builder: (context, snapshot) => ListView(
scrollDirection: Axis.horizontal,
children: [
InkWell(
onTap: () {},
child: Card(
color: Colors.blue.shade100,
child: Icon(Icons.add),
),
),
...snapshot.data.documents.map(
(doc) => Padding(
padding:
const EdgeInsets.only(bottom: 8.0, left: 8.0, right: 8.0),
child: GestureDetector(
onTap: () => {},
child: Container(
child: FittedBox(
child: Material(
color: Colors.white,
elevation: 4.0,
borderRadius: BorderRadius.circular(8.0),
shadowColor: Colors.grey,
child: Row(
children: <Widget>[
Container(
child: myDetailsContainer(
doc), //This guy works. it's just a more complicated defaultuserContainer()
),
],
),
),
),
),
),
),
)
],
),
);
},
)

How to set showModalBottomSheet to initally half height but expandable and dismissable

Following code is dismissable but it takes up full height initially due to colorsList having a lot of colors:
onPressed: () {
showModalBottomSheet(
context: this.context,
isScrollControlled: true,
builder: (BuildContext context) {
return Wrap(
children: [
for (List<Color> colors in colorsList)
Container(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("${colorsName[colorsList.indexOf(colors)]}"),
Container(
height: 80,
child: ListView.builder(
padding: EdgeInsets.only(left: 8, right: 8,),
scrollDirection: Axis.horizontal,
itemCount: colors.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(left: 8,),
child: FloatingActionButton(
backgroundColor: colors[index],
tooltip: "Choose this color",
onPressed: () {
setState(() {
this.color = colors[index];
});
Navigator.pop(context);
},
),
);
},
),
),
],
),
),
],
);
}
);
},
wrap the content of the sheet by a stream builder ,and update the stream when drag occurs
StreamController<double> controller = StreamController.broadcast();
RaisedButton(
child: Text('Show Buttom Sheet'),
onPressed: () {
showModalBottomSheet(context: context, builder: (context){
return StreamBuilder(
stream: controller.stream,
builder:(context,snapshot) => GestureDetector(
onVerticalDragUpdate: (DragUpdateDetails details){
position = MediaQuery.of(context).size.height- details.globalPosition.dy;
print('position dy = ${position}');
position.isNegative?Navigator.pop(context)
:controller.add(position);
},
behavior: HitTestBehavior.translucent,
child:
Container(
color: Colors.red,
height: snapshot.hasData ? snapshot.data:200.0,
width: double.infinity,
child: Text('Child'),
)),
);
});
}),