I have applied sweetalert in my project my application is in landscape direction and sweetalert take the full screen I want to set custom width and height to sweetalert.
SweetAlert.show(context,
title: "Just show a message",
subtitle: "Sweet alert is pretty",
style: SweetAlertStyle.confirm,
showCancelButton: true, onPress: (bool isConfirm) {
if (isConfirm) {
SweetAlert.show(context,style: SweetAlertStyle.success,title: "Success");
// return false to keep dialog
return false;
}
});
There is no any option according to my assessment for your question. but I think rflutter_alert is the best option to use instead of sweetalert here you can use the width and height to customize it. Hope that will help you. you can add multiple buttons in buttons: [],
import 'package:rflutter_alert/rflutter_alert.dart';
Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "Flutter is more awesome with RFlutter Alert.",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
Okay, wrap your string with three quote (''' String '''), Add space or new lines what you want base on your expected height.
https://api.dartlang.org/stable/2.5.0/dart-core/String-class.html
SweetAlert.show(
context,
title: '''
Just show a message
''',
subtitle: '''
Sweet alert is pretty
''',
style: SweetAlertStyle.confirm,
showCancelButton: true,
onPress: (bool isConfirm) {
if (isConfirm) {
SweetAlert.show(context,
style: SweetAlertStyle.success, title: "Success");
// return false to keep dialog
return false;
}
},
);
Related
I am new to flutter, and I want to reduce code duplication when it comes to UI.
Let's say that I chose some theme. ElevatedButton will have color of that theme.
Now, I want to have some buttons that will by styled alternatively (e.g. 'Cancel' buttons to be grey or 'Accept' buttons to be bright green).
Normally I do like this:
ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.grey[100]),
onPressed: () {
// onPressed logic here
},
child: const Text(
'Cancel',
style: TextStyle(color: Colors.black54),
),
),
but if I have multiple Cancel buttons, this code would be repeated:
style: ElevatedButton.styleFrom(primary: Colors.grey[100]), and
style: TextStyle(color: Colors.black54),
What is "Flutter" way to achieve it so I can have "Cancel" or "Accepted" style defined in one place? Should I create my own widget CancelButton? or is there better way?
ElevatedButton(
style: Styles.cancelButtonStyle(),
onPressed: () {
// onPressed logic here
},
child: const Text(
'Cancel',
style: Styles.cancelButtonTextStyle(),
),
),
Your Custom Style Class
class Styles{
static TextStyle cancelButtonTextStyle(){
return TextStyle(
fontSize: 16,
color: Colors.black54
);
}
static TextStyle acceptButtonTextStyle(){
return TextStyle(
fontSize: 16,
color: Colors.black54
);
}
static ButtonStyle cancelButtonStyle(){
return ElevatedButton.styleFrom(primary: Colors.grey[100]);
}
static ButtonStyle acceptButtonStyle(){
return ElevatedButton.styleFrom(primary: Colors.green[700]);
}
}
I am trying to display a dialog box with MVC pattern.
I want the dialog box to be a widget. like so:
AlertDialog gameDecisionDialog({
required VoidCallback onClick,
required String strDecision,
required Color decisionColor,
required BuildContext context,
}) {
return AlertDialog(
titleTextStyle: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 20,
),
actionsOverflowButtonSpacing: 20,
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
return onClick();
},
child: const Icon(Icons.next_plan_outlined),
),
],
content: Text(strDecision),
);
}
This dialog will be called in the model layer. Depending what happens during the app, a dialog will appear. The issue, is the context portion.
does it make sense to pass context from the view layer down to controller layer and then to model layer? Seems inefficient.
Any ideas on how to do this? I am trying to avoid having the dialog box in the view layer, its going to get too messy.
---------------- UPDATE
modified my code to the below suggestion, BUT now my alert dialog doesn't show up.
See the following code (when button clicked do some stuff and then display dialog):
elevatedRectButton(
onClick: () {
setState(() {
MyController.stop();
gameDecisionDialog(
onClick: () {
MyController.start();
},
gameDecision: MyController.getGameDecision,
decisionColor: Colors.amber,
context: context,
);
});
},
mIcon: const Icon(Icons.do_not_touch),
subText: 'STOP',
minWidth: 20,
height: 20,
bgColor: Colors.red,
),
I fear that calling a widget within a widget might be causing this issue?
Passing a BuildContext into a model/controller would not be recommended. Try to call the alert from the widget after the work in the model has been done or when an error is thrown.
Example:
onPress: () async {
//Some trigger callback
startLoading();
await controller.doWork().catchError((e) {
stopLoading();
showAlert(context, 'Some Message etc');
});
stopLoading();
}
Hey I want to do a checklist, if you click on the check button, the line should be crossed out.
By now I figured the decoration out to which the Textstyle should change. I have now two problems I don't really know how to add the button in the body of the list tile, next to the text & I don't really understand how I could add the button functionality
style: TextStyle(decoration: TextDecoration.lineThrough))
This is the relevant code
body: ListTile(
title: Text(info.expandedValue, //here should the textstyle change based on the click
),
subtitle: Text("Drücke auf den Mülleimer, um diesen Abschnitt bis zum Neustart der App zu löschen"),
trailing: Icon(Icons.delete),
onTap: () {
setState(() {
infos.removeWhere((currentInfo) => info ==currentInfo);
});
},
),
Thanks for your suggestions!
Assuming your model has some boolean attribute completed you could use a ListTile witha CheckBox.
ListTile(
leading: Checkbox(
value: info.completed,
onChanged: (value) {
setState(() {
// Here you toggle the checked item state
infos.firstWhere((currentInfo) => info==currentInfo)..completed=value;
});}
),
title: Text(info.description,
style: TextStyle(decoration: info.completed?TextDecoration.lineThrough:null))),
),
I am new to flutter and trying to build some basic text editing options like in word. That means, bold, italic, underlined and so on...
I am currently struggling building those three buttons for left, centered and right text alignment.
I´ve tried to build them the same way as I did with bold and so one;
bool isBold = false;
Button:
_NoteButton(Icons.format_bold, Colors.green, () => setState(() => isBold = !isBold)),
In the TextField:
style: new TextStyle(fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
the problem I have is that i only have "one" TextAlignment line for three Alignments, so it´s not possible to do it exactly the same way as with bold or italic.
It would really help if you could share your code of three buttons to align the text on the left, in the center and on the right in a textfield.
Thank you!
Declare a variable,
TextAlign _align;
Then use three buttons in a row,
Row(
children: <Widget>[
FlatButton(
onPressed: () {
setState(() {
_align = TextAlign.left;
});
},
child: Text('Align left'),
),
FlatButton(
onPressed: () {
setState(() {
_align = TextAlign.center;
});
},
child: Text('Align center'),
),
FlatButton(
onPressed: () {
setState(() {
_align = TextAlign.right;
});
},
child: Text('Align right'),
),
],
);
Then in your Text widget,
Text('Align right', textAlign: _align)
I using flutter alert package rflutter_alert 1.1.0 , is it fine when writing the same code on same page. However due to many function making my code is too lengthy on 1 page.
So I try to split the alert function to another dart file, but I get the error of
A value of type 'Future<bool>' can't be returned from method 'build' because it has a return type of 'Widget'.dart(return_of_invalid_type)
if I write like this
dialog dart
import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
class Dialog extends StatefulWidget {
#override
_DialogState createState() => _DialogState();
}
class _DialogState extends State<Dialog> {
#override
Widget build(BuildContext context) {
return Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "Flutter testing Testing testing.",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
}
}
What is correct way to split out the rFlutter alert code and call it when press the button ?
build need a Widget no ant Alert , in order to call the alert but the code inside click callback (Button for example).
#override
Widget build(BuildContext context) {
return Container(
child: FlatButton(onPressed: (){
Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "Flutter testing Testing testing.",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
}, child: Text("CLICK ME!!")),
);
}