How to send data to Modal bottom sheet in Flutter?

This Article is posted by seven.srikanth at 3/6/2019 5:28:04 PM




A modal bottom sheet is an alternative to a menu or a dialog and prevents the user from interacting with the rest of the app.

This is how it looks like.


Below is an example code of how to send data to Modal bottom sheet in Flutter. All we are doing is a calling a function ShowModalBottomSheet wrapper inside setState to send data into the function. We are also incrementing the number by one to see how the value increments as we continue to open the Modal sheet. This is very simple as shown in the below example.

Code from Main.dart file. In order to run this project locally, all you need to do is to create a Project and copy paste the below code to Lib\main.dart file.

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(home:ModalBottomSheetDemo()));
}

class ModalBottomSheetDemo extends StatefulWidget {
@override
_ModalBottomSheetDemoState createState() => _ModalBottomSheetDemoState();
}

class _ModalBottomSheetDemoState extends State<ModalBottomSheetDemo> {
int _index = 0;
void _showModalSheet(int i) {
showModalBottomSheet(context: context, builder: (builder) {
return Container(
child: Text('You have opened this Modal $i times..'),
padding: EdgeInsets.all(40.0),
);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Modal bottom sheet')
),
body: Center(
child: RaisedButton(
onPressed: () {
setState(() {
_index = _index+1;
_showModalSheet(_index);
});
},
child: const Text('Show Modal Bottom Sheet')
)
)
);
}
}

Please let me know if you have any further queries.

Thanks,
Srikanth


Tags: Send data to Modal bottom sheet; setState example;








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com