I am new to flutter.I have a grid-view with multiple floating-action-buttons. Is there any way I can position my grid-view widget on the y-Axis. I want the grid-view-widget in the middle of the screen.
How can I position layouts in flutter in general ?
Thank you for your help
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: GridView.count(
padding: const EdgeInsets.all(20),
crossAxisSpacing: 15,
mainAxisSpacing: 20,
crossAxisCount: 4,
children: <Widget>[
FloatingActionButton(
onPressed: () {
press_1_button();
},
child: Text("1"),)
,FloatingActionButton(
onPressed: (){
press_2_button();
},
child: Text("2"),)
,FloatingActionButton(
onPressed: () {
press_3_button();
},
child: Text("3"),)
,FloatingActionButton(
onPressed: (){
press_4_button();
},
child: Text("4"),)
,FloatingActionButton(
onPressed: () {
press_5_button();
},
child: Text("5"),)
,FloatingActionButton(
onPressed: (){
press_6_button();
},
child: Text("6"),)
,FloatingActionButton(
onPressed: () {
press_7_button();
},
child: Text("7"),)
,FloatingActionButton(
onPressed: (){
press_8_button();
},
child: Text("8"),)
,FloatingActionButton(
onPressed: () {
press_9_button();
},
child: Text("9"),)
],
),
),
);
}
}
Try wrapping it with padding
Padding(
padding:const EdgeInsets.only(top:20.0),
child:GridView(...)
)
Related
I am a little new to Flutter and are trying to add a searchfield to output of a piece of script i have from a tutorial.
The data is from a sqlite db and i want to add a searchfield that is looking in the output from the database. Can someone help me a little of where to begin of do you know an example of this kind of question?
code where i want to add it is:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Joost mag het weten'),
),
body: _isLoading
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: _journals.length,
itemBuilder: (context, index) => Card(
color: Colors.orange[200],
margin: const EdgeInsets.all(15),
child: ListTile(
leading: Text(_journals[index]['aantal']),
title: Text(_journals[index]['title']),
subtitle: Text(_journals[index]['description']),
trailing: SizedBox(
width: 100,
child: Row(
children: [
IconButton(
icon: const Icon(Icons.edit),
onPressed: () => _showForm(_journals[index]['id']),
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: () =>
_deleteItem(_journals[index]['id']),
),
],
),
)),
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => _showForm(null),
),
);
}
}
I wanna put GestureDetector with container alert but it show error. anyone know how to make this code works? Here the code below which i try to put GestureDetector for the alert container.
Without GestureDetector it works fine but i wanna make whole screen touch able to return to other page.
showPopup(BuildContext context) {
// set up the buttons
// ignore: deprecated_member_use
// set up the AlertDialog
GestureDetector(
Container alert = Container(
child: Stack(
children: <Widget>[
if (controllers!.isNotEmpty)
CarouselSlide2(
controllers: controllers!,
),
Padding(
padding: const EdgeInsets.only(top:688.0,left: 90),
child: GestureDetector(
onTap: () async {
isPop = false;
Navigator.pop(context);
_checkTimer();
},
// child: Icon(Icons.arrow_back,color: Colors.white,size: 100,),
child: DefaultTextStyle(
style: TextStyle(color: Colors.white,fontSize: 30),
child: Text("Tap to return",),
)
),
)
],
)));
// show the dialog
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async {
const shouldPop = true;
isPop = false;
Navigator.pop(context);
_checkTimer();
return shouldPop;
},
child: alert);
},
);
}
You are using widget in a wrong way, try this:
Widget alert = GestureDetector(
onTap: () {
print("tap");
},
child: Container(
child: Stack(
children: <Widget>[
if (controllers!.isNotEmpty)
CarouselSlide2(
controllers: controllers!,
),
Padding(
padding: const EdgeInsets.only(top: 688.0, left: 90),
child: GestureDetector(
onTap: () async {
isPop = false;
Navigator.pop(context);
_checkTimer();
},
// child: Icon(Icons.arrow_back,color: Colors.white,size: 100,),
child: DefaultTextStyle(
style: TextStyle(color: Colors.white, fontSize: 30),
child: Text(
"Tap to return",
),
)),
)
],
)),
)
I need to display a set of options on a pop-up.
The issue that I am having is that the column is taking up all the space.
is there a way around this or another widget I should be using?
I tried using list view but it nothing ends up showing up
_displayJobTypeAlert(BuildContext context) {
return showDialog(
builder: (context) {
return AlertDialog(
title: Text('What type of job is this?'),
content: Column(
//shrinkWrap: true,
// ignore: prefer_const_literals_to_create_immutables
children: [
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Handyman');
},
child: const Text('Handyman'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Landscaping');
},
child: const Text('Landscaping'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Plumming');
},
child: const Text('Plumming'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Remodeling');
},
child: const Text('Remodeling'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Roofing');
},
child: const Text('Roofing'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Electrical');
},
child: const Text('Electrical'),
),
],
),
);
},
context: context,
);
}
Instruct the Column widget to take the minimum size it requires:
Column(
mainAxisSize: MainAxisSize.min,
children: [...]
)
I took out column
I used actions instead of children and it worked!
Future _displayJobTypeAlert(BuildContext context) {
return showDialog(
builder: (context) {
return AlertDialog(
elevation: 24.0,
title: Text('What type of job is this?'),
actions:
//Column(
//shrinkWrap: true,
// ignore: prefer_const_literals_to_create_immutables
//children:
[
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Handyman');
Navigator.pop(context);
},
child: const Text('Handyman'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Landscaping');
Navigator.pop(context);
},
child: const Text('Landscaping'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Plumming');
Navigator.pop(context);
},
child: const Text('Plumming'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Remodeling');
Navigator.pop(context);
},
child: const Text('Remodeling'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Roofing');
Navigator.pop(context);
},
child: const Text('Roofing'),
),
TextButton(
onPressed: () {
_updateJobField(_jobType, jobTypeColumn, 'Electrical');
Navigator.pop(context);
},
child: const Text('Electrical'),
),
],
//),
);
},
context: context,
);
}
I want to call 2 API endpoints at the same time, and I try to use MultiBlocProvider but it does not work, and here is some code.
https://pub.dev/documentation/flutter_bloc/latest/flutter_bloc/MultiBlocProvider-class.html
how could I use it in the correct ways, or I should another ways.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("${allTranslations.text('filterHistory')}"),
),
body: MultiBlocProvider(
providers: [
BlocProvider<HistoryBloc>(
create: (BuildContext context) => historyBloc,
child: BlocListener<HistoryBloc, HistoryState?>(
listener: (context, state) {
print('State loading ${state.runtimeType}');
if(state is HistoryStateSuccess) {
nextPageData(state.historyResModel);
}
if (state is HistoryStateError) {
appShowSnackBar(context, state.runtimeType.toString());
}
},
),
),
BlocProvider<PackingBloc>(
create: (BuildContext context) => packingBloc,
child: BlocListener<PackingBloc, PackingState?>(
listener: (context, state) {
print('State loading ${state.runtimeType}');
if(state is GetPackingStateSeccess) {
getParkingList(state.packingList);
}
if (state is GetPackingStateError) {
appShowSnackBar(context, state.runtimeType.toString());
}
},
),
),
],
child: _bodyBuilder()
),
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
heroTag: 'selectDateRange',
elevation: 10.0,
child: const Icon(Icons.calendar_today),
onPressed: () => {_selectDateRange()},
),
const SizedBox(height: 6),
FloatingActionButton(
heroTag: 'onMapTypeButtonPressed',
elevation: 10.0,
child: const Icon(Icons.map),
onPressed: _onMapTypeButtonPressed,
),
const SizedBox(height: 6),
FloatingActionButton(
heroTag: 'vehicleLocation',
child: const Icon(Icons.car_repair_sharp),
onPressed: () => _controller.animateCamera(
CameraUpdate.newCameraPosition(VehicleLocationPint())),
),
const SizedBox(height: 6),
FloatingActionButton(
heroTag: 'inittialCameraPosition',
elevation: 10.0,
child: const Icon(Icons.gps_fixed),
onPressed: () => _controller.animateCamera(
CameraUpdate.newCameraPosition(_inittialCameraPosition())),
),
],
),
);
};
Thanks you for your answer.
I'm new in flutter. I want to adjust the dimensions and other properties like background color of the CupertinoAlertDialog. I have searched in the documents and tried some properties but didn't work. For background color example, I have tried to put it into a Container and set its color. Definitely it didn't work. So how can I achieve it? Thanks in advance.
Container(
color: Colors.red,
child: CupertinoAlertDialog(
title: Text('dropOut'), //对话框标题
content: SingleChildScrollView(
),
actions: [
CupertinoDialogAction(
child: Text('cancel'),
onPressed: () {},
),
CupertinoDialogAction(
child: Text('OK'),
onPressed: () {},
),
],
),
),
Edited: I tried the method suggested by #Salim Murshed, but it didn't seem work so well. here is my code and the display!
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: RaisedButton(
child: Text("Pick Me !!!"),
onPressed: () {
showDialog(
context: context,
builder: (_) => Center(
child: Container(
width: 270, height: 140,
color: Colors.red,
child: CupertinoAlertDialog (
title: new Text("Material Dialog"),
content: new Text("Hey! I'm Coflutter!"),
actions: <Widget>[
FlatButton(
child: Text('Close me!'),
onPressed: () {
Navigator.pop(_);
},
)
],
),
),
));
},
)
),
);
}
You can check the below code.
RaisedButton(
child: Text("Pick Me !!!"),
onPressed: () {
showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("Material Dialog"),
content: new Text("Hey! I'm Coflutter!"),
actions: <Widget>[
FlatButton(
child: Text('Close me!'),
onPressed: () {
Navigator.pop(_);
},
)
],
));
},
)