How do I change the position of the alert dialog in top in Flutter, Align TopCenter does not work - flutter

i'm new in Flutter. How do I change the position of the alert dialog in top of Screen in Flutter, I've used align TopCenter but it doesn't work. is there a solution?
this is my code:
body: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () {
ErrorAlert(context: context, message: "error message");
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.only(top: appDimens.paddingw6),
child: Text(
"Show Dialog",
style: TextStyle(
color: Color(0xffFF9900),
),
),
),
),
],
),
),
);
This is ErrorAlert widget
Future<dynamic> ErrorAlert({
required BuildContext context,
String? message,
}) {
return showDialog(
context: context,
barrierColor: Colors.white.withOpacity(0),
barrierDismissible: false,
builder: (contex) {
return Align(
alignment: Alignment.topCenter,
child: AlertDialog(
backgroundColor: Color(0xffFFF4F2),
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
content: Container(
height: 20,
width: MediaQuery.of(context).size.width - 40,
child: Row(
// mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Image.asset(
"assets/icon/padlock.png",
height: 20,
),
Text(message ?? "Error",
style: TextStyle(color: Color(0xffFC6762))),
],
),
Center(
child: IconButton(
..........
),
),
],
),
),
),
);
});
}
this is the result:

Just remove AlertDialog from ErrorAlert function, dialog will be shown as you expected. As we know AlertDialog is a kind of Dialog. So we shouldn't use AlertDialog inside showDialog widget.
Future<dynamic> ErrorAlert({
required BuildContext context,
String? message,
}) {
return showDialog(
context: context,
barrierColor: Colors.white.withOpacity(0),
barrierDismissible: false,
builder: (contex) {
return Align(
alignment: Alignment.topCenter,
child: Material(
child: Container(
color: Color(0xffFFF4F2),
height: 20,
width: MediaQuery.of(context).size.width - 40,
child: Row(
// mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(Icons.desktop_mac, color: Colors.blue,),
Text(message ?? "Error",
style: TextStyle(color: Color(0xffFC6762))),
],
),
Center(
child: IconButton(onPressed: () { }, icon: Icon(Icons.read_more),
),
),
],
),
),
),
);
});
}

You can use insetPadding of AlertDialog, like this:
AlertDialog(
insetPadding: EdgeInsets.only(bottom: 300),//<---- add this
backgroundColor: Color(0xffFFF4F2),
elevation: 0,
...
)
Or you can Wrap your AlertDialog with Column like this:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
AlertDialog(
backgroundColor: Color(0xffFFF4F2),
elevation: 0,
...
)
]
)

Related

How to stack two bottom sheet in flutter?

I want to stack two bottom sheet each other in flutter as show in photo. The upper one is shown when in error state. In photo, it build with alert dialog. I want is with bottom sheet. How can I get it?
Edit:
Here is my code that I want to do. Lower bottom sheet is with pin field, autoComplete. autoComplete trigger StreamController, and then streamBuilder watch Error state and show dialog.
confirmPasswordModalBottomSheet(
BiometricAuthRegisterBloc biometricAuthRegBloc) {
showMaterialModalBottomSheet(
context: context,
builder: (BuildContext context) {
return StreamBuilder(
stream: biometricAuthRegBloc.biometricAuthRegisterStream,
builder: (context,AsyncSnapshot<ResponseObject>biometricAuthRegSnapShot) {
if (biometricAuthRegSnapShot.hasData) {
if (biometricAuthRegSnapShot.data!.messageState ==
MessageState.requestError) {
showModalBottomSheet(context: context, builder:
(BuildContext context){
return Container(
width: 200,height: 200,
child: Center(child: Text('Helllllllllo'),),);
});
}
}
return SizedBox(
width: 100,
height: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: margin30,
),
Text(CURRENT_PIN_TITLE),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.only(
left: margin60, right: margin60),
child: PinCodeField(
pinLength: 6,
onChange: () {},
onComplete: (value) {
biometricAuthRegBloc.biometricAuthRegister(
biometricType:_biometricAuthTypeForApi,
password: value);
},
),
),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal:
margin80),
child: AppButton(
onClick: () {},
label: CANCEL_BTN_LABEL,
),
),
Container(
padding: const EdgeInsets.all(8.0),
margin:
EdgeInsets.symmetric(vertical: 8.0,
horizontal: 30),
decoration: BoxDecoration(
color: Colors.grey,
border: Border.all(color: Colors.black),
),
child: const Text(
FINGER_PRINT_DIALOG,
textAlign: TextAlign.center,
),
)
],
),
);
});
},
);
}
When I do like that above, I get setState() or markNeedsBuild() called during build. Error and why? Sorry for my previous incomplete question.
I am bit confused with your question but stacking two bottomsheet is just easy. You just need to call the showModalBottomSheet whenever you want it shown to user. You can check out the following implementation:
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ElevatedButton(
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 500,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 1'),
ElevatedButton(
child: const Text('Show second modal 2'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.redAccent,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 2'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
),
],
),
),
);
},
);
},
),
],
),
),
);
},
);
},
child: Text('Show bottom sheet 1'),
),
);
}
}
I have solution. All I need to do is, need to add WidgetBinding.insatance.addPostFrameCallback((timeStamp){showModalBottomSheet()}); in the StreamBuilder return.

Gesture detector on tap not working in flutter release app but working in debug app

I am using firestore and created 4 cards on which the user taps on goes to the next screen. Everything is working fine in debug mode, in debug mode both Inkwell and Gesture Detector are working but when I make a release version I don't know why but both Inkwell and Gesture Detector are not working. Have no idea what's causing this. Please help.
class _RestaurantDashboardState extends State<RestaurantDashboard> {
Widget buildRestaurantCards(String title, IconData iconData, int orderCount) {
return Expanded(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
if (title == "Menu\nManagement") {
Navigator.push(context,
MaterialPageRoute(builder: (context) => MenuManagement()));
} else if (title == "Current Orders") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OrderScreen("In Progress"),
),
);
} else if (title == "Order History") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OrderHistory("In Progress"),
),
);
} else if (title == "Update Profile") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantSignUp(Utils.restaurant!.restaurantId, true),
),
);
}
},
child: Container(
height: double.infinity,
child: Card(
margin: EdgeInsets.all(8),
elevation: 8,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
margin: EdgeInsets.only(top: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
orderCount > 0
? Row(
children: [
Container(
alignment: Alignment.center,
width: 30,
height: 30,
margin: EdgeInsets.all(4),
padding: EdgeInsets.all(4),
child: FittedBox(
child: Text(
orderCount.toString(),
style: TextStyle(
color: Colors.white, fontSize: 18),
)),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(16),
),
)
],
)
: SizedBox(),
Container(
margin: EdgeInsets.symmetric(horizontal: 16),
alignment: Alignment.centerRight,
child: Icon(
iconData,
size: 40,
),
),
],
),
),
Container(
width: double.infinity,
margin:
EdgeInsets.symmetric(horizontal: 16, vertical: 16),
alignment: Alignment.center,
child: FittedBox(
child: Text(
title,
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.w600),
textAlign: TextAlign.center,
),
)),
],
)),
),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dashboard'),
),
body: Container(
child: Column(
children: [
Container(
height: MediaQuery.of(context).size.height * 0.25,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
isFirebaseInitialized
? StreamBuilder(
stream: FirebaseFirestore.instance
.collection("orders")
.snapshots(),
builder: (context,
AsyncSnapshot<
QuerySnapshot<Map<String, dynamic>>>
snapshot) {
if (!snapshot.hasData) {
setState(() {
_isLoading = false;
});
return Container();
}
if (snapshot.hasError) {
setState(() {
_isLoading = false;
});
}
List<DocumentSnapshot> itemsList = [];
for (DocumentSnapshot doc
in snapshot.data!.docs) {
if (doc['restaurantId'] ==
Utils.restaurant!.restaurantId &&
doc['orderStatus'] == "In Progress") {
itemsList.add(doc);
}
}
return buildRestaurantCards("Current Orders",
Icons.list_alt, itemsList.length);
},
)
: buildRestaurantCards(
"Current Orders", Icons.list_alt, 0),
buildRestaurantCards("Order History", Icons.history, 0),
],
),
),
Container(
height: MediaQuery.of(context).size.height * 0.25,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildRestaurantCards(
"Menu\nManagement", Icons.restaurant_menu_rounded, 0),
buildRestaurantCards("Update Profile", Icons.person, 0),
],
),
),
],
),
)
);
}
}
Check if there are any exceptions caught in the debug console, If there are then fix those, then the issue will be resolved

How to show custom dialog at the top center of the screen in flutter?

I am going to show a custom dialog at the top center of the screen.
I've attached a screenshot .
How can I accomplish this? Default position of Dialog is center of the screen.
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
You can use Align and Material widget. (Alignment.topCenter) i.e
void showCustomDialog(BuildContext context, String message) {
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext cxt) {
return Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.all(16),
child: Material(
color: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
InkWell(
onTap: () {
Navigator.of(context).pop();
},
child: Image.asset("assets/close.png")),
SizedBox(width: 16),
Expanded(
child: Text(
message,
style: TextStyle(
color: Colors.white,
),
),
),
],
),
],
),
),
),
),
);
},
);
}

Flutter error:Failed assertion: line 1785 pos 12: 'hasSize'

I have a code of recording Audio in flutter. The code should work perfectly without errors, whenever I try to call it from an alert dialog I end up with an error
The following assertion was thrown during performLayout():
RenderShrinkWrappingViewport does not support returning intrinsic
dimensions.
Calculating the intrinsic dimensions would require instantiating every
child of the viewport, which defeats the point of viewports being
lazy.
If you are merely trying to shrink-wrap the viewport in the main axis
direction, you should be able to achieve that effect by just giving
the viewport loose constraints, without needing to measure its
intrinsic dimensions.
Am trying to pass an option to record audio to an alert dialog. Users to record audio from a pop up instead of a main screen. What am I doing wrong?
What i have so far
Widget setupAlertDialoadContainer() {
return ListView(
shrinkWrap: true, //just set this property
padding: const EdgeInsets.all(8.0),
//children: listItems.toList(),
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 24.0, bottom: 16.0),
child: Text(
this._recorderTxt,
style: TextStyle(
fontSize: 48.0,
color: Colors.black,
),
),
),
_isRecording
? LinearProgressIndicator(
value: 100.0 / 160.0 * (this._dbLevel ?? 1) / 100,
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
backgroundColor: Colors.red,
)
: Container()
],
),
Row(
children: <Widget>[
Container(
width: 56.0,
height: 56.0,
margin: EdgeInsets.all(10.0),
child: FloatingActionButton(
heroTag: "Record",
onPressed: () {
if (!this._isRecording) {
return this.startRecorder();
}
this.stopRecorder();
},
child: this._isRecording ? Icon(Icons.stop) : Icon(Icons.mic),
),
),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 60.0, bottom: 16.0),
child: Text(
this._playerTxt,
style: TextStyle(
fontSize: 48.0,
color: Colors.black,
),
),
),
],
),
Row(
children: <Widget>[
Container(
width: 56.0,
height: 56.0,
margin: EdgeInsets.all(8.0),
child: FloatingActionButton(
heroTag: "Start",
onPressed: () {
startPlayer();
},
child: Icon(Icons.play_arrow),
),
),
Container(
width: 56.0,
height: 56.0,
margin: EdgeInsets.all(8.0),
child: FloatingActionButton(
heroTag: "Pause",
onPressed: () {
pausePlayer();
},
child: Icon(Icons.pause),
),
),
Container(
width: 56.0,
height: 56.0,
margin: EdgeInsets.all(8.0),
child: FloatingActionButton(
heroTag: "Stop",
onPressed: () {
stopPlayer();
},
child: Icon(Icons.stop),
),
),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
Container(
height: 56.0,
child: Slider(
value: slider_current_position,
min: 0.0,
max: max_duration,
onChanged: (double value) async {
await flutterSound.seekToPlayer(value.toInt());
},
divisions: max_duration.toInt()))
],
);
}
How am passing the data to the Alert dialog
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Record the data collection'),
content: setupAlertDialoadContainer(),
);
});
is the error is showing when alert box is opened??
if yes my suggestion is using
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Record the data collection'),
content: Container(
child:setupAlertDialoadContainer(),
height:200,
width:200,
),
);
});
change the width and height of container according to your need

flutter: how to customize cuperinoAlertDialog style?

I'm working with flutter. I want to make a CupertinoAlertDialog(iOS style is required). My problem is UI designers require the background color of the alert dialog should be #F0F0F0. But I can only adjust its theme into dark or light(e.g. following picture). The code I completed is placed below.
showCupertinoDialog(
context: context,
builder: (BuildContext context){
return Theme(
data: ThemeData.dark(),
child: CupertinoAlertDialog(
title: Text('Title'),
content: Text('Some message here'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancle'),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
),
);
}
);
Is that possible? Thanks for any advice.
If I recall correctly, the background color for CupertinoAlertDialog is hardcoded. However, you can create a custom dialog that can change the background color of it as well as the functions of the buttons.
You need to create a type Dialog for the showDialog function instead of showCupertinoDialog:
Dialog customDialog = Dialog(
backgroundColor: Color(0xfff0f0f0), // your color
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)), // change 40 to your desired radius
child: CustomAlertDialog(),
);
I also created a stateless widget called CustomAlertDialog, but if you don't want to, you can replace the CustomAlertDialog() with its content.
class CustomAlertDialog extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 150,
child: Column(
children: [
Expanded(
flex: 2,
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey, width: 1),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: Center(
child: Text(
"Title",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
),
),
Container(
child: Center(
child: Text("Some message here"),
),
),
],
),
),
),
Expanded(
flex: 1,
child: Row(
children: [
Expanded(
flex: 1,
child: GestureDetector(
child: Container(
decoration: BoxDecoration(
border: Border(
right: BorderSide(color: Colors.grey, width: 1),
),
),
child: Center(
child: Text("Cancel"),
),
),
onTap: () {
Navigator.of(context).pop(); // replace with your own functions
},
),
),
Expanded(
flex: 1,
child: GestureDetector(
child: Container(
child: Center(
child: Text("OK"),
),
),
onTap: () {
Navigator.of(context).pop(); // replace with your own functions
},
),
),
],
),
),
],
),
);
}
}
Lastly, replace your whole showCupertinoDialog with this showDialog function:
showDialog(
barrierDismissible: true, // set false if you dont want the dialog to be dismissed when user taps anywhere [![enter image description here][1]][1]outside of the alert
context: context,
builder: (context) {
return customDialog;
},
);
Result: https://i.stack.imgur.com/cV13A.png