How to align flat buttons in flutter? - flutter

I'm wondering how do I align buttons in flutter. I'm new to flutter and for my current code I'm not able to throw in any rows, column or wrap. I would like to align my buttons in a vertical manner but current its aligned horizontally and giving me a "RIGHT OVERFLOW BY 256 PIXELS" display error
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text("You have $counts routes!"),
actions: <Widget>[
new FlatButton(
textColor: Colors.black,
padding:
EdgeInsets.fromLTRB(20, 0, 0, 10),
child: Text(
"This is route 1! \n" +
rList[0] + "\n"),
onPressed: () {},
),
new FlatButton(
textColor: Colors.black,
padding:
EdgeInsets.fromLTRB(20, 0, 0, 10),
child: Text(
"This is your second route! \n" +
rList[1] + "\n"),
onPressed: () {},
),
],
);
});

just add your buttons to Column
actions: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
button,
button
]
)
],
or if you want that buttons was aligned to left side, move these buttons to content:
builder: (BuildContext context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("You have routes!"),
new FlatButton(
textColor: Colors.black,
child: Text(
"This is route 1! \n" +
"\n"),
onPressed: () {},
),
new FlatButton(
textColor: Colors.black,
child: Text(
"This is your second route! \n" +
"\n"),
onPressed: () {},
)
],
),
);
});

You can use ListView for synchronizing row by row automatically.
Check below sample for ListView.You should paste it on body segment of your code.
//some of codes..
body: Padding(
padding: EdgeInsets.only(top: 15, left: 15, right: 15),
child: ListView(
children: <Widget>[
RaisedButton(
child: Text("Confirm"),
color: Colors.lightGreen,
elevation: 5.0,
onPressed: () {
save(1,context);
/* some of codes */
},
),
RaisedButton(
child: Text("Cancel"),
color: Colors.redAccent,
elevation: 5.0,
onPressed: () {
save(0,context);
/* some of codes */
},
),
RaisedButton(
child: Text("Pass"),
color: Colors.yellowAccent,
elevation: 5.0,
onPressed: () {
save(2,context);
/* some of codes */
},
),
],
),
));

Related

Flutter - How to give dynamic width to buttons in a row

AlertDialog(
insetPadding: EdgeInsets.all(10),
title: Text(
'Job Added',
textAlign: TextAlign.center,
),
content: Builder(builder: (context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
return SizedBox(
height: height / 5,
width: width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
//// All three buttons take equal width, I don't want that
Expanded(
child: ElevatedButton(
style: Styles.greenElevatedButtonStyle,
onPressed: () => () {
Get.toNamed(Routes.jobList);
},
child: const Text('Job List'),
),
),
Dimens.boxWidth10,
Expanded(
child: ElevatedButton(
style: Styles.yellowElevatedButtonStyle,
onPressed: () => () {
Get.toNamed(Routes.jobDetails);
},
child: const Text('View Job'),
),
),
Dimens.boxWidth10,
Expanded(
child: ElevatedButton(
style: Styles.redElevatedButtonStyle,
onPressed: () => () {},
child: const Text(' Add New Job '),
),
),
],
),
],
),
);
}),
actions: [],
),
I want that the buttons should take dynamic width according to the text. So the button that says "Add New Job" should be wider than the other 2; the text on the button shouldn't get wrapped, it should stay in a single line.

How to make paginated page on listview like datatable flutter

I'm a new flutter user, here I'm learning how to create an attractive display. One of them is I want to add a paginated page to the listview, how do I add the paginated page? I have tried to make like using datatable but can't and error occurs. with the source code that I have I hope my friends can help me. thank you very much good friend
sample image i want
display the program that I have
Source Code :
Widget nasabahListView(List<Nasabah> listnasabah) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
Nasabah nasabah = listnasabah[index];
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
nasabah.nama_debitur,
style: Theme.of(context).textTheme.bodyText1,
),
Text(nasabah.alamat),
Text(nasabah.no_ktp),
Text(nasabah.no_telp),
Text(nasabah.no_selular),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
apiService
.deleteNasabah(nasabah.id)
.whenComplete(() {
Alert(
context: context,
type: AlertType.success,
title: "Sukses",
desc: "Data berhasil dihapus",
buttons: [
DialogButton(
child: Text(
"OK",
style: TextStyle(
color: Colors.white, fontSize: 20),
),
onPressed: () {
Navigator.pop(context);
},
width: 120,
)
],
).show();
});
},
child: Text(
"Hapus",
style: TextStyle(color: Colors.red),
),
),
TextButton(
onPressed: () async {
var result = await Navigator.push(context,
MaterialPageRoute(builder: (context) {
return FormAddNasabah(nasabah: nasabah);
}));
},
child: Text(
'Edit',
style: TextStyle(color: Colors.blue),
),
),
],
)
],
),
),
),
);
},
itemCount: listnasabah.length,
),
);
}

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

How to remove the background when clicking on the text of a link?

I have a Text widget with a link to another screen, when clicked, the background appears. How do I remove the background when clicked?
More in the photo
Align(
alignment: AlignmentDirectional.topStart,
child: FlatButton(
//color: Colors.redAccent,
onPressed: () => Navigator.of(context).push(
new MaterialPageRoute(builder: (context){
return new SettingPage();
}
),
),
padding: EdgeInsets.only(left:20.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child:SvgPicture.asset(iconSvgS5, height: 30.0, color:Colors.blueAccent),
),
Padding(
padding: const EdgeInsets.only(left:20.0),
child: GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => FaqPage()),
),
child:Text(
"Вопросы и ответы",
style: TextStyle(
fontSize: 18.0
),
),
),
),
],
),
),
If you are wrapping your text widget inside of a button then the button by default have feedback to let users know when they are pressed, if you don't need the feedback consider wrapping the button with a GestureDetector widget, and passing a function to the onTap property
Removed the background color on click with this code:
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
You can use the hoverColor property of the FlatButton to set the hoverColor to your liking, here is the modified code that disables the hoverColor (simply sets it to transparent Colors.transparent) -
Align(
alignment: AlignmentDirectional.topStart,
child: FlatButton(
//setting the hover color to transparent.
hoverColor : Colors.transparent,
onPressed: () => Navigator.of(context).push(
new MaterialPageRoute(builder: (context) {
return new SettingPage();
}),
),
padding: EdgeInsets.only(left: 20.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: SvgPicture.asset(iconSvgS5,
height: 30.0, color: Colors.blueAccent),
),
Padding(
padding: const EdgeInsets.only(left: 20.0),
child: GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => FaqPage()),
),
child: Text(
"Вопросы и ответы",
style: TextStyle(fontSize: 18.0),
),
),
),
],
),
),
),
Also side note having a GestureDetector inside of a FlatButton is redundant, and in your case it is ambiguous as well since they are do two separate navigation.
Set all color properties of the FlatButton to transparent.
The example below also includes a convenient hack, how to make the clickable area wider, so if the user will press near the icon, the button will work.
It improves button responsiveness.
Example
FlatButton(
color: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
padding: const EdgeInsets.all(0.0),
minWidth: 24,
onPressed: () => Get.back(),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
CupertinoIcons.clear,
color: Theme.of(context).colorScheme.pureBlack,
size: 18,
),
],
),
),

How to add a FAB using stack when floatingActionButtonLocation is defined?

In my application I have defined a FAB with the property floatingActionButtonLocation set to FloatingActionButtonLocation.centerDocked. I have defined a bottom app bar that holds the centerDocked FAB. I also want to use a Stack widget to position two additional FABs on the top right of the screen. It should look something similar to this - expected result
But when I tried to use a Stack, the FAB which is present in the bottom app bar is displayed but the two FABs under the stack widget are invisible.
docked FAB
FABs under stack widget
code where I have defined the Stack
return new Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Stack(
children: <Widget>[
Positioned(
// the below values for stack are experimental at the moment
left: MediaQuery.of(context).copyWith().size.width * 0.60,
right: MediaQuery.of(context).copyWith().size.width * 0.01,
// top: MediaQuery.of(context).copyWith().size.height * 0.20,
// bottom: MediaQuery.of(context).copyWith().size.height * 0.7,
child: Row(
children: <Widget>[
// invisible FABs
FloatingActionButton(
onPressed: (){
_animate2Pagenegative();
// print(widget.date);
},
child: Icon(Icons.fast_rewind),
heroTag: UniqueKey(),
),
FloatingActionButton(
onPressed: (){
_animate2Pageforward();
// print(widget.date);
},
heroTag: UniqueKey(),
child: Icon(Icons.fast_forward),
),
],
),
),
// FAB which is displayed correctly
FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () async{
String result1 = await Navigator.push( // string which stores the user entered value
context,
MaterialPageRoute(
builder: (context) => InputScreen(), //screen which has TextField
));
setState(() {
addItem(result1, false, "");
});
},
tooltip: 'Add a task',
child: Icon(Icons.add),
elevation: 0.0,
),
],
),
)
body of Scaffold:
body: new Column(
children: <Widget>[
new Expanded(
child: new DaysPageView(
// onDaysChanged: getDatestring,
physics: new NeverScrollableScrollPhysics(),
dayoverride: getDate(widget.date),
scrollDirection: _scrollDirection,
pageSnapping: _pageSnapping,
reverse: _reverse,
controller: _daysPageController,
pageBuilder: _daysPageBuilder,
),
),
],
),
You don't need other FloatingActionsButtons to do that design. What I understand is that you need the upper part not to scroll with the content hence you thought of FABs. There are multiple ways to achieve that. Here is an example, I'm using colors to show you the different parts of your screen. And of course these are random sizes to show the case.
return new Scaffold(
body: Column(
children: <Widget>[
Container(
height: 150,
color: Colors.red,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Text(
"- 2 TASKS MORE",
style: TextStyle(
fontSize: 35
),
),
),
GestureDetector(
onTap: () {
print('left arrow pressed');
},
child: Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Icon(Icons.arrow_left)
),
),
GestureDetector(
onTap: () {
print('right arrow pressed');
},
child: Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Icon(Icons.arrow_right)
),
)
],
),
SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Column(
children: <Widget>[
Container(
height: 600,
color: Colors.deepPurpleAccent,
),
],
),
)
],
),
bottomNavigationBar: BottomAppBar(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
shape: CircularNotchedRectangle(),
color: Colors.red,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: ()
async {
print('FAB pressed');
},
tooltip: 'Add a task',
child: Icon(Icons.add),
elevation: 0.0,
),
);
And here is the result