Notification badge on floating action button in flutter - flutter

I want to add the items to the cart. The cart will be Floating action button and item count I want to show on top of it. Please help me with this.
I want something like this

In case, you don't want to install any 3rd party package, you can always use the Stack widget to create your own badge.
Basically, while setting the floatingActionButton property of you Scaffold, you can use a Stack widget to build your Badge.
For example:
class BadgeExample extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
floatingActionButton: Container(
child: FittedBox(
child: Stack(
alignment: Alignment(1.4, -1.5),
children: [
FloatingActionButton( // Your actual Fab
onPressed: () {},
child: Icon(Icons.add),
backgroundColor: Colors.deepOrange,
),
Container( // This is your Badge
child: Center(
// Here you can put whatever content you want inside your Badge
child: Text('4', style: TextStyle(color: Colors.white)),
),
padding: EdgeInsets.all(8),
constraints: BoxConstraints(minHeight: 32, minWidth: 32),
decoration: BoxDecoration( // This controls the shadow
boxShadow: [
BoxShadow(
spreadRadius: 1,
blurRadius: 5,
color: Colors.black.withAlpha(50))
],
borderRadius: BorderRadius.circular(16),
color: Colors.blue, // This would be color of the Badge
),
),
],
),
),
),
);
}
}
Output

Well, the floatingActionButton receive Widget type, this mean that you can make Widget a FloactionActionButton, so, you can use badges package, like this:
Badge(
toAnimate: true,
shape: BadgeShape.circle,
badgeColor: Colors.red,
borderRadius: BorderRadius.circular(8),
badgeContent: Text('0', style: TextStyle(color: Colors.white)),
child: Icon(
Icons.shopping_cart,
),
)
Link to the package: https://pub.dev/packages/badges

Related

Is there a way to use Expanded widget inside Wrap widget

What I wanted to achieve is that the size of the elevated button should fill the entire space. in my case if you look at the picture attached i want "user experience" and "engineering" fill the empty space in blue and in the same time aligned from left and right
I am trying to make the buttons size dynamic inside the warp widget, her my code below
Wrap(
spacing: 10,
alignment: WrapAlignment.spaceBetween,
children: [
for (InterestsClass item in interests)
InterestsButton(
text: item.interestsName,
)
],
),
InterestsButton is just an elevated button that is reusable, but if I add expanded as show below its Incorrect use of ParentDataWidget, since there is no flex parent
Widget build(BuildContext context) {
return Expanded(
child: ElevatedButton(
onPressed: () {
setState(() {
_flag = !_flag;
});
},
child: Text(
widget.text,
style: TextStyle(color: _flag ? kWhite : k34, fontSize: 13),
),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
shape: StadiumBorder(),
primary: _flag ? kBlue : Color(0xffFAFAFA)),
),
);
}
I think u can use an alternative way Chip/ ChoiceChip. These widget is available in Flutter very useful for creating size dynamic button with change style when tap.
See more here
https://api.flutter.dev/flutter/material/Chip-class.html
https://medium.com/flutterdevs/chip-widgets-in-flutter-7a2d3d34597c
Result One
I have it working now. Here is the code:
FilterChip(
label: Text("text"),
backgroundColor: Colors.transparent,
shape: StadiumBorder(side: BorderSide()),
onSelected: (bool value) {print("selected");},
),
Result Two
You can achieve it by this way
Chip(
shape: RoundedRectangleBorder(
side: BorderSide(color: colorThird, width: 1),
borderRadius: BorderRadius.circular(10),
),
backgroundColor: colorSecondary,
deleteIcon: Icon(
Icons.close,
color: Colors.white,
size: 15,
),
label: Text(searchController.recentSearchesList[index], style:
TextStyle(color: Colors.white),),
deleteButtonTooltipMessage: 'erase',
onDeleted: () {
print(index);
},
);

Flutter transparent button elevation

I created a custom button that I want to give a color and an elevation. Works finde, except when I give the button the color Colors.transparent. Here is an example to showcase my problem:
Widget buildButton(void Function() onPressed, String label, double? elevation) {
return OutlinedButton(
onPressed: onPressed,
child: Text(label),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.white,
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: elevation ?? 0
),
);
}
class ButtonsDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
buildButton(() {}, "Button", null),
buildButton(() {}, "Button", 5),
],
),
),
);
}
}
The buttons look like this:
Here is how they should look like (I set the color of the buttons to the background color of the screen to showcase them, but I can't do that in my code):
I tried this answer but it only got me so far (I don't know if I did it completely wrong, but it looks so):
I also tried this answer and it got me there:
So, as you can see, both of them answers haven't helped me at all. Can anyone show me how I can add elevation to a transparent button?
The best solution to your problem is to use ElevatedButton instead of OutlinedButton and to give it a shadowColor. The code below is for the second button, please give it a try.
ElevatedButton(
onPressed: () {},
child: Text(
'Elevated',
style: TextStyle(color: Colors.black),
),
style: ElevatedButton.styleFrom(
elevation: 5,
primary: Colors.transparent,
shadowColor: Colors.transparent.withOpacity(0.1),
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),

how to change the shape of my elevatedbutton

import 'package:flutter/material.dart';
class SignInPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Time Tracker'),
elevation: 5.0,
),
body: _buildContent(),
backgroundColor: Colors.amber[50],
);
}
Widget _buildContent() {
return Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'Sing in',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.w600,
),
),
SizedBox(height: 8.0),
ElevatedButton(
child: Text('Sing in with google'),
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.purple[200],
onPrimary: Colors.black87,
elevation: 6.0,
shadowColor: Colors.yellow[200],
),
],
),
);
}
}
1_ i don't know how to change the shape of my button in this context
and i clearly have an error at the end of the last square ], line 41 .please help me to fix it
i appreciate your help in advance.
You can use a Container as a child of the Button or do this:
ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
child: Text(' Elevated Button'),
),
You can use the following way:
Inside elevated button,
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)
)
)
)
Here you can play with borderRadius property to get different shapes.
Make the child of the ElevatedButton a Container widget.
You can then use the shape attribute available for container widget.
The text widget can become the child for the container widget.

How to make a Floating Action Button with a popup menu in flutter?

I need to make a popup menu kind of button.
Is there any way to make a pop up menu floating action button,this is my desired view
You can use flutter speed dial package.
Visit - https://pub.dev/packages/flutter_speed_dial .
And here is a youtube video - https://www.youtube.com/watch?v=1FmATI4rOBc
Your answer is PopupMenuItem class, which will help you get the desirable result.
PLEASE NOTE: I have just demonstrated how to use, and with what code, you achieve the result. You can anyways, play with it, and get your desirable result.
CODE SNIPPET FOR CREATING A POPUP MENU ITEM
PopupMenuButton<Choice>(
itemBuilder: (context) => [
PopupMenuItem()
],
icon: Icon(),
offset: Offset()
)
CODE FOR REFERENCE
class _MyHomePageState extends State<MyHomePage> {
Widget _offsetPopup() => PopupMenuButton<int>(
itemBuilder: (context) => [
PopupMenuItem(
value: 1,
child: Text(
"Flutter Open",
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700),
),
),
PopupMenuItem(
value: 2,
child: Text(
"Flutter Tutorial",
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700),
),
),
],
icon: Container(
height: double.infinity,
width: double.infinity,
decoration: ShapeDecoration(
color: Colors.blue,
shape: StadiumBorder(
side: BorderSide(color: Colors.white, width: 2),
)
),
//child: Icon(Icons.menu, color: Colors.white), <-- You can give your icon here
)
);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.only(right: 10.0, bottom: 10.0),
child: Align(
alignment: Alignment.bottomRight,
child: Container(
height: 80.0,
width: 80.0,
child: _offsetPopup()
)
)
)
);
}
}
The above will give you this result:
PRO-TIP
You can play around with Offset() to decide the position of your PopupMenuItems

Want to Show an AlertDialog when clicking on a Button, on Pressed Method shows an error. ( Undefined name 'context' .)

Hey im New in learning Flutter and Dart. Please help me. :)
I really donĀ“t know what to do I`m a totaly beginner. :/
Now I have pastet my complete code I hope you can see where I defined my Container.
This all is a TabView because I want to train an play with some examples so I tryed out the TabView. In The TabView I packed then all in Containers. If there is a better option, you can tell me of course also. :)
This is my Code:
Future<void> _showAlert(BuildContext context) async {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Accept?'),
content: Text("Do you accept?"),
actions: <Widget>[
FlatButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('No')
),
FlatButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('Yes')
),
],
backgroundColor: Colors.deepOrange,
shape: CircleBorder(),
);
}
);
}
class MyApp extends StatelessWidget {
List<Widget> containers = [
Container(
color: Colors.orange,
padding: EdgeInsets.all(20.0),
alignment: Alignment.center,
child: Container(
height: 80,
width: 80,
child: FloatingActionButton(
child: Icon(Icons.check),
tooltip: ('"Hello World"'),
onPressed: () {
print('Hello World');
},
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
),
),
Container(
color: Colors.teal,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
print("Raised Button clicked!");
},
child: Text(
"Please click on me!",
style: TextStyle(fontSize: 18),
),
),
),
Container(
color: Colors.deepPurple,
alignment: Alignment.center,
child: RaisedButton(onPressed: () {_showAlert(context);},
color: Colors.deepOrange,
child: Icon(Icons.warning)
),
),
];
The Error says: Undefined name 'context'. (In the onPresssed section at my Button.)
Error shown
Code Snippet 1
Code Snippet 2
What you are missing in your Stateless widget is the build method(Which contains the context), but the issue there is that you can't return a List with the build method because it only returns a Widget. In order to fix this, you should first create a function for your list of widgets, then inside the Stateless widget return the function inside of a widget with a children property, like a Column
Your Widget list function
widgetList(BuildContext context) {
return [
Container(
color: Colors.orange,
padding: EdgeInsets.all(20.0),
alignment: Alignment.center,
child: Container(
height: 80,
width: 80,
child: FloatingActionButton(
child: Icon(Icons.check),
tooltip: ('"Hello World"'),
onPressed: () {
print('Hello World');
},
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
),
),
Container(
color: Colors.teal,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
print("Raised Button clicked!");
},
child: Text(
"Please click on me!",
style: TextStyle(fontSize: 18),
),
),
),
Container(
color: Colors.deepPurple,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
_showAlert(context);
},
color: Colors.deepOrange,
child: Icon(Icons.warning)),
),
];
}
Your Stateless Widget
class MyApp extends StatelessWidget {
#override Widget build(BuildContext context) {
return Column(
children:
widgetList(context)
);
}
}