Make custom RisedButton expand - flutter

i am new to flutter and building custom RisedButton
I have this button
import 'package:flutter/material.dart';
class EasyButton extends StatelessWidget {
final onPressed;
final text;
EasyButton(this.onPressed, this.text);
#override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: this.onPressed,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60),
),
padding: EdgeInsets.all(0),
elevation: 2,
splashColor: Colors.blue[300],
child: Ink(
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(60),
gradient: new LinearGradient(
colors: [Colors.blue, Colors.blue[800]],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
child: new Text(this.text, style: TextStyle(color: Colors.white)),
),
);
}
}
But i when i am using it with Expanded widget i get this result
How can I make it 100% parent width expanded?
With double.infinity
With container

Wrap the RaisedButton inside ButtonTheme and specify minWidth.
ButtonTheme(
minWidth: double.infinty, //takes up all the width
child: RaisedButton(),
)

Related

I'm getting a color and decoration null error

HELP!!
Please i'm getting this error
color == null || decoration == null
"Cannot provide both a color and a decoration\nTo provide both, use "decoration:
BoxDecoration(color: color)"."
The relevant error-causing widget was:
AppIconText
AppIconText:file:///Users/jessezephyr/projects/flutter/ticketapp/lib/screens/search_screen.dart:67:17
This is the code
`flutter`
{
```
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:ticketapp/utils/app_layout.dart';
import '../utils/app_styles.dart';
class AppIconText extends StatelessWidget {
final IconData icon;
final String text;
const AppIconText({super.key, required this.icon, required this.text});
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(
vertical: AppLayout.getWidth(12),
horizontal: AppLayout.getHeight(12)),
color: Colors.white,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppLayout.getWidth(10)),
),
child: Row(
children: [
Icon(
icon,
color: const Color(0XFF8FC20F),
),
Gap(AppLayout.getWidth(10)),
Text(
text,
style: Styles.textStyle,
)
],
),
);
}
}
}
{
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:ticketapp/utils/app_layout.dart';
import 'package:ticketapp/utils/app_styles.dart';
import 'package:ticketapp/widgets/icon_text_widget.dart';
class SearchScreen extends StatelessWidget {
const SearchScreen({super.key});
#override
Widget build(BuildContext context) {
final size = AppLayout.getSize(context);
return Scaffold(
backgroundColor: Styles.bgColor,
body: ListView(
padding: EdgeInsets.symmetric(
horizontal: AppLayout.getWidth(20),
vertical: AppLayout.getHeight(20)),
children: [
Gap(AppLayout.getHeight(40)),
Text(
'What are you \nlooking for?',
style:
Styles.headLineStyle.copyWith(fontSize: AppLayout.getWidth(35)),
),
Gap(AppLayout.getHeight(20)),
FittedBox(
child: Container(
padding: const EdgeInsets.all(3.5),
child: Row(
children: [
/*
Airline TICKETS
*/
Container(
width: size.width * .44,
padding:
EdgeInsets.symmetric(vertical: AppLayout.getHeight(7)),
decoration: BoxDecoration(
borderRadius: BorderRadius.horizontal(
left: Radius.circular(AppLayout.getHeight(50))),
color: Colors.white,
),
child: Center(child: Text('Airline tickets')),
),
Container(
width: size.width * .44,
padding:
EdgeInsets.symmetric(vertical: AppLayout.getHeight(7)),
decoration: BoxDecoration(
borderRadius: BorderRadius.horizontal(
right: Radius.circular(AppLayout.getHeight(50))),
color: Colors.transparent,
),
child: Center(child: Text('Hotels ')),
),
],
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppLayout.getHeight(50)),
color: const Color(0xFFF4F6FD),
),
),
),
Gap(AppLayout.getHeight(25)),
const AppIconText(
icon: Icons.flight_takeoff_rounded, text: 'Departure'),
Gap(AppLayout.getHeight(15)),
const AppIconText(icon: Icons.flight_land_rounded, text: 'Arrival'),
],
),
);
}
}
```
}
It's supposed to display an icon and text
It's supposed to display a text and an icon but it's not working
In AppIconText widget, the Container widget being returned has both color and decoration defined. This is not allowed as stated in the error message. A container should either have a color or decoration or none. You can not have both color and decoration on a container. This is because a box decoration itself has a color property. If you are using a decoration, you should use this property to assign color to the Container.
Container(
padding: EdgeInsets.symmetric(
vertical: AppLayout.getWidth(12),
horizontal: AppLayout.getHeight(12)),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppLayout.getWidth(10)),
color: Colors.white,
),
......
);
Just move the color property inside the BoxDecoration and it will work.

It is Showing context is undefined

This dart project it is showing an error that context is undefined and when i sent it as an argument it is showing error i am new to flutter and my mentor wrote the same(as i have seen)
i am facing this problem
import 'package:flutter/material.dart';
class Category_item extends StatelessWidget {
final String title;
final Color color;
Category_item(this.title, this.color);
void selectCategory() {
Navigator.of(context);
}
#override
Widget build(BuildContext context) {
return InkWell(
onTap: selectCategory,
splashColor: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(15), //wave ripple waves
child: Container(
padding: const EdgeInsets.all(15),
child: Text(
title,
style: Theme.of(context).textTheme.headline6,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
),
);
}
}
void selectCategory() {
Navigator.of(context);
}
this function is outside the build.
try this:
import 'package:flutter/material.dart';
class Category_item extends StatelessWidget {
final String title;
final Color color;
Category_item(this.title, this.color);
void selectCategory() {
Navigator.of(context);
}
#override
Widget build(BuildContext context) {
return InkWell(
onTap: selectCategory,
splashColor: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(15), //wave ripple waves
child: Container(
padding: const EdgeInsets.all(15),
child: Text(
title,
style: Theme.of(context).textTheme.headline6,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
),
);
}
}
Try this-
selectCategory(BuildContext context) {
Navigator.of(context);
}
The function needs to have an input for the context in order for it to know what the context is.
void selectCategory(BuildContext context) {
Navigator.of(context);
}
And then call it like this:
onTap: () => selectCategory(context),

Flutter reuse custom widget

Let's say I created a custom widget that emulates button functions.
#override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.lightGreenAccent,
child: SizedBox (
width: 90 ,
height: 90,
child: Column (
children: [
Icon(Icons.face,
size: 50,
color: Colors.red,),
Text('Enable ->'),
Container(
height: 20,
width: 80,
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
),
),
],
),
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
);
}
How can I create a group of such widgets, but so that each has a different icon or text by reusing my widget?
You can customize your widget like this:
import 'package:flutter/material.dart';
class CustomWidget extends StatelessWidget {
CustomWidget({this.icon, this.text});
final Widget icon;
final Widget text;
#override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.lightGreenAccent,
child: SizedBox (
width: 90 ,
height: 90,
child: Column (
children: [
icon,
text,
Container(
height: 20,
width: 80,
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
),
),
],
),
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
);
}
}

How to invert a radius in Flutter

I am trying to have a Radius border in the text widget and I don't know how to "invert" the borderRadius so that it's not white:
I would like that the blue section and the green section stay together with the corners with radius.
Container(
padding: const EdgeInsets.fromLTRB(_hPad, 10.0, _hPad, _hPad),
child: Text(_body),
width: appWidth / 2,
height: middleSectionHeight,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(70.0),
topLeft: Radius.circular(70.0),
),
color: Color(0xff99AAAB),
),
),
You can pass the BorderRadius as an argument:
The example class:
class ExampleContainer extends StatelessWidget {
final String text;
final BorderRadius borderRadius;
final Color color;
static const double _hPad = 10.0;
const ExampleContainer(
{#required this.text, #required this.borderRadius, #required this.color,});
#override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.fromLTRB(_hPad, 10.0, _hPad, _hPad),
child: Text(text),
width: appWidth / 2, //change to the width you want
height: middleSectionHeight, //change to the height you want
decoration: BoxDecoration(
borderRadius: borderRadius, //passing here the borderRadius
color: color,
),
);
}
}
Utilization:
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ExampleContainer(
borderRadius: BorderRadius.vertical(top: Radius.circular(70.0)),
color: Colors.blueGrey,
text: 'title',
),
ExampleContainer(
borderRadius: BorderRadius.vertical(bottom: Radius.circular(70.0)),
color: Color(0xff99AAAB),
text: 'something2',
),
],
),
);
}

Adding text to custom button

I am able to create custom button widget and now i want add text to this. I tried several option adding it under return container under Flatbutton but it does't work.
Can someone please guide me where exactly i can add text which i can pass to this widget along with icon and will be displayed below icon.
I can add either text or icon right now and I am looking for text below icon.
import 'package:tirthankar/core/const.dart';
import 'package:flutter/material.dart';
class CustomButtonWidget extends StatelessWidget {
final Widget child;
final double size;
final double borderWidth;
final String image;
final bool isActive;
final VoidCallback onTap;
CustomButtonWidget({
this.child,
#required this.size,
#required this.onTap,
this.borderWidth = 2,
this.image,
this.isActive = false
});
#override
Widget build(BuildContext context) {
var boxDecoration = BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(200),
),
border: Border.all(
width: borderWidth,
color: isActive ? AppColors.darkBlue : AppColors.mainColor,
),
boxShadow: [
BoxShadow(
color: AppColors.lightBlueShadow,
blurRadius: 10,
offset: Offset(5,5),
spreadRadius: 3,
),
BoxShadow(
color: Colors.white60,
blurRadius: 10,
offset: Offset(-5,-5),
spreadRadius: 3,
)
],
);
if (image != null){
boxDecoration = boxDecoration.copyWith(
image: DecorationImage(
image: ExactAssetImage(image),
fit: BoxFit.cover
),
);
}
if (isActive){
boxDecoration = boxDecoration.copyWith(
gradient: RadialGradient(
colors: [
AppColors.lightBlue,
AppColors.darkBlue,
]
),
);
} else {
boxDecoration = boxDecoration.copyWith(
gradient: RadialGradient(
colors: [
AppColors.mainColor,
AppColors.mainColor,
AppColors.mainColor,
Colors.white
]
),
);
}
return Container(
width: size,
height: size,
decoration: boxDecoration,
child: FlatButton(
padding: EdgeInsets.all(0),
onPressed: onTap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(200),)
),
child: child ?? Container(),
)
);
}
}
you can replace your child ?? Container() with Column like below :
return Container(
width: size,
height: size,
decoration: BoxDecoration(),
child: FlatButton(
padding: EdgeInsets.all(0),
onPressed: onTap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(200),
)),
child: Column(
children: <Widget>[
iconWidget, // child ?? Container()
textWidget,
],
),
),
);
also you can handle icon only or text only button with wrapping each iconWidget or textWidget with Visibility widget ...