How to make split buttons - flutter

I'm trying to make a similar UI. Tell me how you can implement split custom buttons as shown on the screen. I'm more interested in how to make a custom form for buttons. As you can see, it blends into the background of the container.
I understand that ordinary containers can not do here. We need something more custom.
I am considering using ordinary containers, but here it should be noted that something is more custom. It is possible to look towards CustomPainter??

Here is an example how you can use flex in a row to have two seperate buttons next to each other. Hope this is what you are looking for.
Row(
children: <Widget> [
Expanded(
// with the flex you give the width
flex: 9,
child: Align(
alignment: Alignment.center,
child: ElevatedButton(
child: const Text('Configuration'),
onPressed: () {
},
//styling button
style: ElevatedButton.styleFrom(
),
),
)
),
Expanded(
// with the flex you give the width
flex: 9,
child: Align(
alignment: Alignment.center,
child: ElevatedButton(
child: const Text('Rack 3'),
onPressed: () {
},
// styling button
style: ElevatedButton.styleFrom(
),
),
)
)
]
),

Related

Flutter - make button fill the entire container

I am very new to Flutter and I trying to make button fill the entire parent container. I want to create two buttons each of them 50% of device height and 100% width using two Flexible with flex parameters.
This is What I have:
Flexible(
flex: 1,
child: Container(
color: Color.fromARGB(255, 20, 20, 20),
alignment: Alignment.center,
child: ElevatedButton(
child:Text('$_counter1'),
onPressed: _pop1,
),
),
),
Everyday I create websites using CSS and HTML and here in Flutter everything works completly diferent. It's hard to understand the code at the begining. With CSS I cloud only add 100% width and height and done but here I am little bit lost.
This is What I am trying to get:
With flutter, you can use Column, Expanded
Column(
children: [
Expanded(
flex: 1,
child: Container(
color: const Color.fromARGB(255, 20, 20, 20),
alignment: Alignment.center,
child: ElevatedButton(
child: Text('Button 1'),
onPressed: (){},
),
),
),
Expanded(
flex: 1,
child: Container(
color: Colors.green,
alignment: Alignment.center,
child: ElevatedButton(
child: Text('Button 2'),
onPressed: (){},
),
),
),
],
)
you are almost right. Flexible takes only the needed space. if you want to takes all space available like 100% then you need to add another properties for Flexible :
https://stackoverflow.com/a/52646036/12838877
Flexible(
fit: FlexFit.tight,
child: Foo(),
);
or is just shorthand with Expanded
Flexible(
fit: FlexFit.tight,
child: Foo(),
);
for comparison : reff
as you can see, Flexible will only take as minimum space needed.
and by default expanded will take 100% available space.
if you want to modify, you just change the flex value
You could combine Column with cross axis alignment strech to achieve this.
Like so:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.grey,
child: TextButton(
onPressed: () {},
child: Text(
'Button 1',
style: Theme.of(context).textTheme.headline2!.copyWith(
color: Colors.grey.shade700,
),
),
),
),
),
Expanded(
child: Container(
color: Colors.grey.shade700,
child: TextButton(
onPressed: () {},
child: Text(
'Button 2',
style: Theme.of(context).textTheme.headline2!.copyWith(
color: Colors.grey,
),
),
),
),
),
],
Running example:
https://dartpad.dev/?enchanted-pomelo-5708

Add a button with a size and add icon inside it in flutter

I am trying to add an icon to a button.
What steps I followed: Created an icon button filled in the properties.
After this, I wrapped it in a container because I don't want my button to be big.
Here is the image of what I am getting: [![enter image description here][1]][1]
I want that Icon to be fit inside the button,I even tried to wrap the child with centre but still the same results.
My Code Snippet:
Container(
height:18,
width:20,
decoration:BoxDecoration(
border:Border.all(color:Colors.orangeAccent,width:1),
shape:BoxShape.rectangle,
),
child:Centre(child:IconButton(
icon:Icon(Icons.add,
size:16,
color:Colors.orange,),
onPressed:(){},
),),
You can try this code block
Container(
width: 50,
height: 50,
child: FlatButton(
onPressed: () {},
child: Row(
children: [
Icon(Icons.ac_unit),
Text('Add More')
],
),
),
)
The easiest way you can do like below code.
Container(
width: 50,
height: 50,
child: GestureDetector(
onTap: () {},
child: Row(
children: [
Icon(Icons.ac_unit),
Padding(
padding: EdgeInsets.only(left: 10),
child: Text('Add More'),
)
],
),
),
)
You can use TextIconButton instead of IconButton.

How to center AlertDialog FlatButton in Flutter

How can I style the FlatButton to be aligned on center without having to create a custom AlertDialog?
AlertDialog(
title: Text(
'Alert Dialog'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28.0),
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog' * 100),
],
),
),
actions: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
This is how it looks like
I tried removing 'actions' and adding the FlatButton inside a row in 'content', but the scroll stopped working and the content overflowed.
content: Column(
children: <Widget>[
SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog.' * 50),
],
),
),
Row(
children: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 16.0
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
],
),
Google's material design guidelines don't want you to place that in center, which is why Flutter team used ButtonBar for actions and you have no control over ButtonBar's alignment inside actions property, so if you really want to center your single button, there are 2 simple workarounds:
Make changes in source code, in dialog.dart's AlertDialog's build method, you'll see a ButtonBar, widget, add alignment to it.
ButtonBar(
alignment: MainAxisAlignment.center, // add this line
// ...
)
You can use dummy widgets like SizedBox and provide some width to it.
actions: [
SizedBox(width: space),
FlatButton(
onPressed: () {},
child: Text('Button'),
),
SizedBox(width: space),
]
Screenshot:
Try to play around with actionsPadding which is one of the property of AlertDialog.
final horizontalPadding=50;
horizontal padding is the one that is transparent to the left and right of the dialog.
actionsPadding: EdgeInsets.only(right: MediaQuery.of(context).size.width/2-horizontalPadding*2),
Also, go through How to style AlertDialog Actions in Flutter . You can change and play around within the library as mentioned here. But, this may not work for your colleagues who are sharing the git repo. They will have to update their libraries as well in their local machines.
actions: <Widget>[
Align(
alignment: Alignment.center,
child: ElevatedButton(
onPressed: () {
//your logic
Navigator.pop(context);
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Close'),
),
),
),
],
Just use actionsAlignment property in AlertDialog Widget.
AlertDialog(
actions: ...,
actionsAlignment: MainAxisAlignment.spaceBetween
)

Flutter - FlatButton Fill available horizontal space

I'm trying to have a UI in such a way that the number of buttons in a row changes depending on available information. More specifically, there will always be one FlatButton that links to an external page, but if a download link is also provided, then there will be a second FlatButton in the row.
On the website that currently exists, we have this working for one button versus two buttons, but I can't get the one button to horizontally expand to fill the available space.
At the moment, I add the FlatButtons to a List<Widget> variable that I pass as the children for a row. I have tried using Flex -> Expanded, SizedBox.expand, CrossAxisAlignment.stretch and every solution I have found on this site so far to get it to expand, but every solution I have tried have all resulted in forced infinite width or unbounded width or non-zero flex but incoming width constraints are unbounded.
Here's my code in the build method as it currently stands:
List<Widget> fullTextDownloadBtns = new List();
if (this.fullArticleUrl != null && this.fullArticleUrl.isNotEmpty) {
fullTextDownloadBtns.add(
FlatButton(
color: Color(0x66629c44),
onPressed: _openFullArticle,
child: Text(
"Full Article",
style: TextStyle(color: Color(0xff629c44)),
),
)
);
}
if (this.downloadUrl != null && this.downloadUrl.isNotEmpty) {
fullTextDownloadBtns.add(
FlatButton(
color: Color(0x664d69b1),
onPressed: _download,
child: Text(
"Download",
style: TextStyle(color: Color(0xff4d69b1)),
),
)
);
}
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xff0096b0)
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// ...related image, title
Container(
padding: EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: fullTextDownloadBtns,
),
), // Read full article & download PDF
// ... Authors, excerpt/description, like/share buttons, comments
],
)
),
);
Wrapping your buttons with Expanded is the way to go here (I tried it with your code) :
Row(
children: [
Expanded(
child: FlatButton(onPressed: (){},
child: Text("1")),
),
Expanded(
child: FlatButton(onPressed: (){},
child: Text("2")),
)
],
),
By the way, since the SDK 2.2.2 (in your pubspec.yaml), you can use conditions within the children list :
children: [
if(true == true)
FlatButton(
onPressed: (){},
child: Text("1")),
],
I just wanted to add that you can adjust the width of the buttons border by using the 'width' property inside 'side: BorderSide(color: Colors.black54, width: 2.2)'.
Row(
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
side: BorderSide(color: Colors.black54, width: 2.2),
),
color: Colors.white,
child: (Text('BUTTON NAME')),
),
),
],
),
Couldn't find how to change the border outline width for a long while on here. So thought I might add it here. Maybe help some other noobie like me in the future.
You can use a Row widget and use two Expanded widget which their child are FlatButton.implement the condition fot them seperately sth like:
Row(
children:[
trueCondition ? Expanded(
child: FlatButton()
) : SizedBox()
]
)

Multiple tab views per page?

I have a container with two columns. When you click on the OPTIONS button, the screen animates half a width to the left and shows additional (clickable!) options.
I'm currently doing this with a Row containing two containers where the second container lives in a Stack that overlays a translated-to-the-right container with the options. Something along those lines:
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.blue,
),
),
Expanded(
child: Container(
child: Stack(
children: <Widget>[
Container(
color: Colors.orange
),
Transform.translate(
offset: const Offset(170.0, 0.0),
child: Container(
color: Colors.green,
child: Column(
children: const <Widget>[
FlatButton(child: Text("Option 1")),
FlatButton(child: Text("Option 2")),
FlatButton(child: Text("Option 3")),
],
),
),
),
],
),
),
)
],
);
Now, since the green container is outside its parent stack, the option buttons won't get any gesture events and hence not work. There is a similar question here and even a GitHub issue. What neither provides is a solution.
Is there way to make gestures outside the stack work, and if not, what are my options for this use case? In a way what I'm trying to achieve is similar to a tab view but with two tab views displayed at the same time.