Adding text to custom button - flutter

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 ...

Related

want to make circular pie chart shape like buttons in Flutter

Following are the screenshots of the Ui of Buttons which I want to make using Flutter.
There are the packages Pie chart and Syncfusion's package, but these are different for this approach.
Help me to create these.
I've found my solution and now adding on the request of #dijkstra
I've used PieChart widget to implement it.
First of all, create a model class
class PieButtonModel {
PieButtonModel({this.title, required this.onPressed});
final VoidCallback onPressed;
final String? title;
}
Now, create a stateless class for Ui. I've used two PieChart widgets in stack to match the required output.
class PieButtonScreen extends StatelessWidget {
PieButtonScreen({Key? key, required this.buttonsData, this.startsFrom}) : super(key: key);
final List<PieButtonModel> buttonsData;
final double? startsFrom;
int touchedIndex = -1;
#override
Widget build(BuildContext context) {
return Center(
child: StatefulBuilder(builder: (context, stateful) {
return Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: const BoxDecoration(
color: kGreyColor,
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: [
Color(0xff1143A7),
Color(0xff00B6FE),
],
),
),
width: double.infinity,
height: double.infinity,
),
Container(
margin: const EdgeInsets.all(10),
decoration: const BoxDecoration(
color: kWhiteColor,
shape: BoxShape.circle,
),
width: double.infinity,
height: double.infinity,
),
PieChart(
PieChartData(
pieTouchData: PieTouchData(touchCallback: (FlTouchEvent event, pieTouchResponse) {
stateful(() {
if (!event.isInterestedForInteractions ||
pieTouchResponse == null ||
pieTouchResponse.touchedSection == null) {
touchedIndex = -1;
return;
}
touchedIndex = pieTouchResponse.touchedSection!.touchedSectionIndex;
pieButtonPressed(touchedIndex, buttonsData);
});
}),
borderData: FlBorderData(
show: true,
border: const Border(
top: BorderSide(color: Colors.red),
)),
sectionsSpace: 5,
centerSpaceRadius: 60,
centerSpaceColor: const Color(0xFFe7e8e9),
startDegreeOffset: startsFrom ?? 0,
sections: showingSections()),
),
Positioned.fill(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
Get.find<ValveViewController>().removeView();
},
child: Neumorphic(
style: const NeumorphicStyle(
boxShape: NeumorphicBoxShape.circle(),
color: kWhiteColor,
border: NeumorphicBorder(
color: Color(0xFF919192),
width: 2,
)),
child: Container(
height: 90,
width: 90,
decoration: BoxDecoration(
// shape: BoxShape.circle,
borderRadius: BorderRadius.circular(100),
),
child: const CircleAvatar(
backgroundImage: AssetImage('assets/images/icon.jpg'),
),
),
),
),
],
),
)
],
);
}),
);
}
}
And then, create a list of sections
List<PieChartSectionData> showingSections() {
return List.generate(buttonsData.length, (i) {
PieButtonModel pbm = buttonsData[i];
final isTouched = i == touchedIndex;
final fontSize = isTouched ? 14.0 : 11.0;
final radius = isTouched ? 100.0 : 90.0;
var val = buttonsData.length / 100;
List<String>? title = pbm.title?.replaceAll('/ ', '').split(" ");
return PieChartSectionData(
color: kGreyColor,
value: val,
title: title?.isEmpty == true
? ""
: title?.length == 1
? title![0]
: title!.length == 2
? "${title[0]}\n${title[1]}"
: title.length == 3
? "${title[0]}\n${title[1]}\n${title[2]}"
: "${title[0]}\n${title[1]}\n${title[2]}\n${title[3]}",
radius: radius,
titleStyle: TextStyle(
overflow: TextOverflow.ellipsis,
fontSize: fontSize,
color: kBlackColor,
),
);
});
}
Here is the output.

Change the fill color of the container from left to right in Flutter

I am trying to change the color of a container from say white to red. But I want to do it from left to right that is, from the left of the container the color red starts filling up and expands all the way to the right in some duration. I know animated container will change the color but it seems to change the color of the entire container and not how I want it to
Like the 'Next Episode' of Netflix
You could use the Stack Widget and use an AnimatedContainer in between the background container (grey) and the foreground container (displaying icon & text):
class NetflixCustomButton extends StatefulWidget {
final Duration animationDuration;
final double height;
final double width;
final double borderRadius;
const NetflixCustomButton({
this.animationDuration = const Duration(milliseconds: 800),
this.height = 30,
this.width = 130.0,
this.borderRadius = 10.0,
});
#override
_NetflixCustomButtonState createState() =>
_NetflixCustomButtonState();
}
class _NetflixCustomButtonState
extends State<NetflixCustomButton> {
double _animatedWidth = 0.0;
#override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
height: widget.height,
width: widget.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
widget.borderRadius,
),
color: Colors.grey,
),
),
AnimatedContainer(
duration: widget.animationDuration,
height: widget.height,
width: _animatedWidth,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
widget.borderRadius,
),
color: Colors.white,
),
),
InkWell(
child: Container(
height: widget.height,
width: widget.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
widget.borderRadius,
),
color: Colors.transparent,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.play_arrow,
color: Colors.black87,
),
Text(
'Next Episode',
style: TextStyle(
color: Colors.black87,
),
),
],
),
),
onTap: () {
setState(() {
_animatedWidth = widget.width;
});
},
),
],
);
}
}

How Can I get the border Side color in flutter

I am designing a simple screen. I want to acheive this
How can I remove the topLeft border. I try to using border side to show only one side border but it showing error not able to change the another then uniform. Did i remove the app bar then stack the widget. Kindly tell me which thing I am doing wrong.
My Code is
import 'package:flutter/material.dart';
class SendScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey =
new GlobalKey<ScaffoldState>();
return Scaffold(
drawer: Drawer(),
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
leading: IconButton(
icon: Icon(Icons.access_alarm),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
actions: [
Container(
width: 50,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: const DecorationImage(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.black,
// width: 8,
),
borderRadius: BorderRadius.circular(12),
),
),
SizedBox(
width: 20,
)
],
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
// bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(80),
),
),
title: Text('Sliver AppBar'),
),
),
body: Column(
children: [
Container(
height: 80,
decoration: BoxDecoration(
border: Border.all(
color: Colors.red[500],
),
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(80),
),
),
child: Row(
children: [],
),
)
],
),
);
}
}
This is a known "issue". The problem is that once you assign a border radius, you don't know where the border starts and where it ends. Therefore flutter can't assign a border color to only certain border.
The only trick which works for you situation of everything I tried is to stack 2 container, the one on the back acting to do the border.
Here is the implementation:
import 'package:flutter/material.dart';
main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SendScreen(),
),
);
}
}
class SendScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
return Scaffold(
drawer: Drawer(),
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
leading: IconButton(
icon: Icon(Icons.access_alarm),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
actions: [
Container(
width: 50,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: const DecorationImage(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.black,
// width: 8,
),
borderRadius: BorderRadius.circular(12),
),
),
SizedBox(
width: 20,
)
],
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
// bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(80),
),
),
title: Text('Sliver AppBar'),
),
),
body: CustomRectangle(
borderSize: 20,
borderColor: Colors.grey[300],
height: 80.0,
borderRadius: 80.0,
child: Container(
color: Colors.red,
),
),
);
}
}
class CustomRectangle extends StatelessWidget {
final Widget child;
final double height;
final double borderSize;
final double borderRadius;
final Color borderColor;
const CustomRectangle({
Key key,
#required this.height,
#required this.borderSize,
#required this.borderRadius,
#required this.child,
#required this.borderColor,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Stack(
children: [
ClipRRect(
borderRadius: BorderRadius.only(bottomRight: Radius.circular(borderRadius)),
child: Container(
height: height + borderSize,
decoration: BoxDecoration(
color: borderColor,
),
),
),
ClipRRect(
borderRadius: BorderRadius.only(bottomRight: Radius.circular(borderRadius)),
child: Container(
height: height,
child: child,
),
),
],
);
}
}
Look at CustomRectangle it is my custom widget which solve your problem. You can expand from there.

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',
),
],
),
);
}

Make custom RisedButton expand

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(),
)