Flutter borders - flutter

Hello I very new to Flutter and I am trying to create a button, but I have a problem with this.
This is the code:
Align(
alignment: AlignmentDirectional(-0.03, 0.13),
child: FFButtonWidget(
onPressed: () {
print('Login pressed ...');
},
text: 'Login',
options: FFButtonOptions(
width: 130,
height: 40,
color: FlutterFlowTheme.of(context).primaryColor,
textStyle: FlutterFlowTheme.of(context).subtitle2.override(
fontFamily: 'Poppins',
color: Colors.white,
),
borderSide: BorderSide(
color: Colors.transparent,
width: 1,
),
borderRadius: BorderRadius.circular(8),
),
),
),
This is the error:
The argument type 'BorderRadius' can't be assigned to the parameter type 'double?'.
Do you have any suggestion?

I think you are using FlutterFlow.
Here you don't need to give the border radius like this
borderRadius: BorderRadius.circular(8)
Do it like this
borderRadius: 8
This is because FlutterFlow button widget is not like the usual Flutter button widget. They are modified versions of actual Flutter widgets.
In your case, properties of FFButtonWidget is set using this class
FFButtonOptions()
This class probably takes borderRadius as a double value and internally sets it like this
borderRadius: BorderRadius.circular(8)
So you have to give the required borderRadius as a double value.

The borderRadius field in the FFButtonOptions require a double parameter. Try
borderRadius: 8.0
It is hard to say, because I do not know how the FFButtonWidget looks like. Normally, your way of setting the border radius would be good for material widgets, for instance:
Align(
alignment: AlignmentDirectional(-0.03, 0.13),
child: Container(
width: 130,
height: 40,
decoration: BoxDecoration(
color: FlutterFlowTheme.of(context).primaryColor,
borderRadius: BorderRadius.circular(8),
),
child: TextButton(
onPressed: () {
print('Login pressed ...');
},
child: Text('Login'),
style: ButtonStyle(
textStyle: FlutterFlowTheme.of(context).subtitle2.override(
fontFamily: 'Poppins',
color: Colors.white,
),
),
),
),
),
Probably the FFButtonWidget takes a double parameter and finally transforms it into
borderRadius: BorderRadius.circular(8.0)
But if you want to use material widgets, you can check how the buttons work in Flutter here.

Related

How to convert a "FlatButton" to "TextButton" in Flutter?

I got following code:
FlatButton.icon(
minWidth: double.infinity,
padding: EdgeInsets.symmetric(
vertical: kDefaultPadding,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: kPrimaryColor,
onPressed: () {},
icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
label: Text(
"New message",
style: TextStyle(color: Colors.white),
),
).addNeumorphism(
topShadowColor: Colors.white,
bottomShadowColor: Color(0xFF234395).withOpacity(0.2),
),
It seems the FlatButtun is now deprecated and I must convert it to TextButton but when I try that, it gives me errors for minWith parameter. When I try to wrap it with ConstrainedBox I get other errors for padding shape, etc.
I don't know how make this old code work as expected before?
As I've commented, you can't just "convert" a FlatButton.icon to a TextButton.icon. The changes in the buttons are breaking changes made to Flutter:
A new set of basic material button widgets and themes have been added
to Flutter. The original classes have been deprecated and will
eventually be removed. The overall goal is to make buttons more
flexible, and easier to configure via constructor parameters or
themes.
So, to solve your problem, you'll have to compose your own widgets to get close to FlatButton.icon.
For your example, you can use the Padding widget for padding, and SizedBox for the width. for rounded corners, you can use the style property.
Your button code can look something like this using TextButton.icon:
SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 10,
),
child: TextButton.icon(
//rounded corners
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
onPressed: () {},
icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
label: Text(
"New message",
style: TextStyle(color: Colors.white),
),
),
),
)

Can't set primary colour of ElevatedButton in Flutter

I have an elevated button in Flutter, generally the button works fine.
However, the setting of the primary: Colors.red in this example does not work, the button is transparent for some reason... All the other params of the ElevatedButton.styleFrom() are working as expected.
Any assistance would be appreciated!
Widget _btnGood() {
return SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(
onPressed: null,
style: ElevatedButton.styleFrom(
primary: Colors.purple,
side: const BorderSide(width: 8.0, color: Colors.white),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(32)),
padding: const EdgeInsets.all(25),
),
child: const Text(
"Im Feeling Good",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
);
}
Example of Button
Simply change on pressed to-
on_pressed: (){} instead of null.

flutter: how to get a rounded container in flutter?

I'm new in flutter and I want to make a round container. I have done this but I don't know how to adjust the value to adapt it. Is there a better solution? Thanks in advance.
Widget Cube() {
return Container(
width: 70, height: 70,
child: Container(
width: 64, height: 64,
decoration: BoxDecoration(
color: CalendarColor.blue,
borderRadius: BorderRadius.all(
Radius.circular(32),
)
),
),
);
}
what I want is like this.
Container(
decoration: BoxDecoration(
shape: BoxShape.circle //This will make container round
)
)
Is there a better solution?
How about FoatingActionButton?
FloatingActionButton(
backgroundColor: Colors.blue,
elevation: 0.0,
child: Text("15", style: TextStyle(color: Colors.white)),
onPressed: () {})
You can also use RawMaterialButton for more options like this:
RawMaterialButton(
elevation: 2.0,
fillColor: Colors.black,
shape: CircleBorder(),
onPressed: () {});
},
)

How to change border color and width of OutlineButton globally?

I need to change the border color and width of the OutlineButton, I know one way of doing this by directly mentioning it inline as below:
OutlineButton(
child: Text('SIGN IN'),
padding: EdgeInsets.all(8.0),
onPressed: handleSignIn,
borderSide: BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
),
)
If I do it as mentioned below, it's effecting FlatButton and RaisedButton and this is NOT effecting OutlineButton.
MaterialApp(
title: 'MyApp',
theme: ThemeData(
buttonTheme: ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
// this effects to FlatButton and RaisedButton
side: BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
),
),
),
),
);
So, How to change border color and width ONLY of OutlineButton globally (through out the app)?
I don't think you can do for that in OutlineButton. So, what i can suggest you is to create a global theme data in a global file.
var borderData=BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
)
Then you can easily use it any class just by importing that global file.
OutlineButton(
child: Text('SIGN IN'),
padding: EdgeInsets.all(8.0),
onPressed: handleSignIn,
borderSide: borderData,
)
This will reduce your boiler plate code in your case.
Unless borderSide is explicitly added to the OutlineButton, it will default to a width of 1.0, a style of solid and this color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.12)
You have the option of changing onSurface of your app theme, but that will affect other widgets as well.
You can do what Jay suggested in the other answer, but my preference would be to create a different widget. This way, if you ever want to change anything else to all of your OutlineButtons in the future, you only need to change your custom widget:
import 'package:flutter/material.dart';
class OutlineButton2 extends OutlineButton {
const OutlineButton2({
Key key,
#required VoidCallback onPressed,
Widget child,
}) : super(
key: key,
onPressed: onPressed,
child: child,
borderSide: const BorderSide(
color: Colors.white,
style: BorderStyle.solid,
width: 1,
),
);
}
This class is deprecated, please use OutlinedButton instead
OutlinedButton(
style: TextButton.styleFrom(
primary: primaryColor,
side: BorderSide(color: primaryColor),
minimumSize: Size(200, 50),
),
child: Text('SIGN IN'),
),
try deleting the OutlineButton
Try the code below:
GestureDetector(
child: Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 2.0,
color: Theme.of(context).primaryColor)),
),
child: Text('SIGN IN'),
),
onTap: onPressed,
),
basically the GestureDetector will take the onPressed logic code and the container is gonna give the decoration as you ask from it with the Text inside

How to left align the OutlineButton icon in Flutter

How to left align the OutlineButton icon in Flutter?
Icon can be added as follows, but both icon and text are centered aligned in the button. Is there a way to align the icon to the left and text to the center?
return new OutlineButton.icon(
onPressed: onPressed,
label: new Text(title),
icon: icon,
highlightedBorderColor: Colors.orange,
color: Colors.green,
borderSide: new BorderSide(color: Colors.green),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)));
There are many ways you can do it, but it's not possible using the factory constructor that you mentioned OutlinedButton.icon, you can go deeper and check the source code to see how it build the widget.
You can create your own Widget to put an icon to the left and the text centered.
Also you can use the OutlinedButton widget and pass a Stack/Row as a child, check this sample
OutlinedButton(
onPressed: () => null,
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Icon(Icons.access_alarm)
),
Align(
alignment: Alignment.center,
child: Text(
"Testing",
textAlign: TextAlign.center,
)
)
],
),
highlightedBorderColor: Colors.orange,
color: Colors.green,
borderSide: new BorderSide(color: Colors.green),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)
)
)
I Wrap Text Widget with Expanded widget.
OutlinedButton.icon(
icon: Icon(
Icons.lock,
color: MyAppTheme.accentColor,
size: 20,
),
label: Expanded(
child: Text(
' Login',
style: TextStyle(
color: MyAppTheme.primaryColor,
fontSize: 16),
),
),
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)),
side: BorderSide(color: MyAppTheme.primaryColor)
),
),
You can use OutlineButton.icon, and simply wrap your Text with in Center like this:
return new OutlineButton.icon(
onPressed: onPressed,
label: Center(child: new Text(title)),
icon: icon,
)
If you want the text left-aligned, this also works:
return new OutlineButton.icon(
onPressed: onPressed,
label: Align(alignment: Alignment.centerLeft, child: new Text(title)),
icon: icon,
)