How to set logged in username inside a top navigationBar? - flutter

How can I get the username from Firebase in this code?
I have followed a guide on youtube for the design.
I have a top navigation bar that has a CustomText with the username.
But in this there is no class (stless/stfull). So how can I get my username and put in in my CustomText?
When I have a normal Statefull class I manage to do it, but when there is noe class I don't know how.
Here is the code:
AppBar(
leading: !ResponsiveWidget.isSmallScreen(context) ? Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Image.asset("assets/icons/logo.png", width: 28,),
),
],
) : IconButton(icon: Icon(Icons.menu), onPressed: (){
key.currentState.openDrawer();
}),
title: Container(
child: Row(
children: [
Visibility(
visible: !ResponsiveWidget.isSmallScreen(context),
child: CustomText(text: "Dash", color: lightGrey, size: 20, weight: FontWeight.bold,)),
Expanded(child: Container()),
IconButton(icon: Icon(Icons.settings, color: dark,), onPressed: (){}),
Stack(
children: [
IconButton(icon: Icon(Icons.notifications, color: dark.withOpacity(.7),), onPressed: (){}),
Positioned(
top: 7,
right: 7,
child: Container(
width: 12,
height: 12,
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: active,
borderRadius: BorderRadius.circular(30),
border: Border.all(color: light, width: 2)
),
),
)
],
),
Container(
width: 1,
height: 22,
color: lightGrey,
),
SizedBox(width: 24,),
CustomText(text: 'username', color: lightGrey,),
SizedBox(width: 16,),
Container(
decoration: BoxDecoration(
color: active.withOpacity(.5),
borderRadius: BorderRadius.circular(30)
),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(30)
),
padding: EdgeInsets.all(2),
margin: EdgeInsets.all(2),
child: CircleAvatar(
backgroundColor: light,
child: Icon(Icons.person_outline, color: dark,),
),
),
)
],
),
),
iconTheme: IconThemeData(color: dark),
elevation: 0,
backgroundColor: Colors.transparent,
);

Related

how can I achieve something like this with flutter sliver app bar?

I want to achieve the design provided below.
The design is scrollable with a tab bar. The scroll functionality has been achieved but the card between tab bar and sliver app bar is something I could not achieve.
The code I have tried
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
backgroundColor: Colors.deepPurple,
leading: IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(Icons.arrow_back_ios_new_sharp)),
actions: [
Container(
height: 46,
width: 46,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: .5, color: Colors.white),
color: Colors.transparent,
),
child: const Icon(
Icons.share_outlined,
color: Colors.white,
),
alignment: Alignment.center,
),
const SizedBox(
width: 10,
),
Container(
height: 46,
width: 46,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: .5, color: Colors.white),
color: Colors.transparent,
),
child: const Icon(
Icons.favorite_border,
color: Colors.white,
),
alignment: Alignment.center,
),
const SizedBox(
width: 5,
)
],
expandedHeight: MediaQuery.of(context).size.height * .35,
flexibleSpace: FlexibleSpaceBar(
title: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Good food'),
RatingBar.builder(
unratedColor: Colors.black,
initialRating: 5,
ignoreGestures: true,
itemSize: 18,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Colors.amber,
),
onRatingUpdate: (_) {},
)
],
),
centerTitle: true,
background: Image(
image: AssetImage(
'assets/images/food.jpg',
),
fit: BoxFit.cover,
),
),
),
SliverToBoxAdapter(
child: Center(
child: SizedBox(
height: 100,
width: MediaQuery.of(context).size.width * .9,
child: Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: Image(image: AssetImage('assets/images/book1.png')),
),
)),
),
],
),

Flutter how to create circular border around icon and align an image top of the circular border

I am trying to use the below code to create a circular border around an image and align an icon on top of the circular border. What I am looking at is as the image below:
And my code is as below but it didn't work out perfectly:
Stack(
children: [
CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: CircleAvatar(
radius: 70,
backgroundImage: AssetImage('assets/person_icon.png'),
backgroundColor: Colors.white,
//
)),
),
Positioned(
bottom: 100,
right: 50,
child: InkWell(
onTap: () {},
child: Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_a_photo, color: colorBlue),
),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.white,
),
borderRadius: BorderRadius.all(
Radius.circular(
50,
),
),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(2, 4),
color: Colors.black.withOpacity(
0.3,
),
blurRadius: 3,
),
]),
),
)),
],
),
As you can above I am trying to use stack to lay each widget on top of each other but couldn't achieve that. I don't if anyone can help out where I missed it or give me a good idea of how to come about this.
Thanks in advance.
Default clipBehavior on Stack is hardEdge.
Use clipBehavior: Clip.none on Stack.
And to have circle shape
use customBorder: CircleBorder(), on InkWell.
use shape: BoxShape.circle instead of circular radius on container.
For better alignment use
Positioned(
top: -12,//half of icon size
left: 0,
right: 0,
Also better providing size on Stack like here.
/// fixing top widget size
SizedBox.square(
dimension: squareSize,
child: Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
clipBehavior: Clip.hardEdge,
decoration: const ShapeDecoration(
shape: CircleBorder(),
),
child: Image.asset(
'assets/images/image01.png',
fit: BoxFit.cover,
),
),
),
),
///background circle, you also do it on image widget
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: 5, color: Colors.green),
),
),
Positioned(
top: -12, // half of icon size
left: 0,
right: 0,
child: InkWell(
onTap: () {},
customBorder: const CircleBorder(),
child: Container(
width: 24 + 12, //icon size+padding
height: 24 + 12,
alignment: Alignment.center,
decoration: const ShapeDecoration(
shape: CircleBorder(),
color: Colors.green,
),
child: Icon(
Icons.add_a_photo,
color: Colors.white,
),
),
),
),
],
),
)
Play with sizes and decoration
Just add to the Stack Widget the Property clipBehavior: Clip.none,.
The clipBehavior will befine how to handle the Clip of the Stack Widget. Standard is hardEdge and cuts your icon off.
So your finished working Code is
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
clipBehavior: Clip.none,
children: [
CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: CircleAvatar(
radius: 70,
backgroundImage: AssetImage('assets/person_icon.png'),
backgroundColor: Colors.white,
//
),
),
),
Positioned(
bottom: 100,
right: 50,
child: InkWell(
onTap: () {},
child: Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_a_photo, color: Colors.blue),
),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.white,
),
borderRadius: BorderRadius.all(
Radius.circular(
50,
),
),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(2, 4),
color: Colors.black.withOpacity(
0.3,
),
blurRadius: 3,
),
],
),
),
),
),
],
),
),
);
}
}
void main() {
runApp(
const MaterialApp(
home: MyApp(),
),
);
}
Try below code
Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 25),
child: Container(
padding: EdgeInsets.all(8),
height: 270,
width: 270,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 250,
width: 250,
child: Image.network(
'https://miro.medium.com/max/1400/1*-6WdIcd88w3pfphHOYln3Q.png',
fit: BoxFit.cover,
),
),
),
),
),
),
Positioned(
top: 0,
left: .0,
right: .0,
child: Center(
child: CircleAvatar(
backgroundColor: Colors.green,
radius: 30.0,
child: Icon(
Icons.check,
color: Colors.white,
size: 40,
),
),
),
)
],
),
Result Screen->
user this below code to achieve
Center(
child: Stack(
children: [
Container(
height: 200,
child: CircleAvatar(
radius: 75,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 70,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: const CircleAvatar(
radius: 60,
backgroundImage: NetworkImage("https://helostatus.com/wp-content/uploads/2021/09/pic-for-WhatsApp-HD.jpg"),
backgroundColor: Colors.white,
//
)),
),
),
),
Positioned(
left: 16,
right: 16,
top: 8,
child: InkWell(
onTap: () {},
child: const CircleAvatar(
backgroundColor: Colors.green,
radius: 16,
child: Icon(
Icons.check,
color: Colors.white,
),
)),
),
],
),
)[enter image description here][1]
Thanks all for the contribution but here is what works perfectly for what I'm looking at. I added clipBehavior and set Clip to noneclipBehavior: Clip.none, added ClipOval in a Container and set it margin and padding respectively to give the space between the green circular border and the profile image. I also, added another Container inside my ClipOval so as to add my image as a child to it. Find below my final code:
Center(
child: Stack(
clipBehavior: Clip.none,
children: [
CircleAvatar(
radius: 60,
backgroundColor: colorGreen,
child:Container(
padding: EdgeInsets.all(4),
margin: EdgeInsets.all(3),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 250,
width: 250,
child: Image.network(
'my image link',
fit: BoxFit.cover,
),
),
)
)),
Positioned(
bottom: 100,
right: 45,
child: InkWell(
onTap: () {},
child: Center(
child: CircleAvatar(
backgroundColor: colorGreen,
radius: 15.0,
child: Icon(
Icons.check,
color: Colors.white,
size: 25,
),
),
),
),
),
],
),
),

Placing button at the bottom of container in flutter

How can I create button like below,
Try below code hope its helpful to you. used Stack widget here for that
Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: circleRadius / 2.0),
child: Container(
height: 200,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
color: Colors.white,
margin: EdgeInsets.all(
16,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 2,
height: 2,
decoration: ShapeDecoration(
shape: CircleBorder(),
color: Colors.transparent,
),
),
],
),
),
),
),
Container(
width: 100,
height: 40,
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(),
color: Colors.transparent,
),
child: Padding(
padding: EdgeInsets.all(1),
child: DecoratedBox(
child: Center(
child: Text(
'Profile',
style:TextStyle(color: Colors.white,),
textAlign: TextAlign.center,
),
),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.blue,
),
),
),
)
],
),
Your Result screen->
Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(20))),
child: TextButton(
child: Text(
"Profile",
style: TextStyle(color: Colors.white),
),
onPressed: () {},
),
),
you can use following code sample...change height according to your need...or use mediaquery for better result:
Container(
height: 275,
child: Stack(
children: [
Container(//container with background color
height: 250,
child: //other widgets
),
Positioned(
bottom: 0,
child: //button here
),
],
),
),

How can I create a popup with arrow mark in AppBar in flutter without any plugin?

In AppBar I have kept an icon. When I click on that icon it should open a popup with pointing arrow. That popup should display below the icon only. Should not overlap on that image. And that dropdown should able to customise according to any UI. Please find the attached image.
I don't want to use any plugin.
You can use the Stack Widget to overlap this bubble, and change dynamically the visibility.
//if the bubble is visible or not
bool isVisible = false;
#override
Widget build(BuildContext context) {
return Material(
child: Stack(
children: [
Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey[50],
actions: [
Padding(
padding: const EdgeInsets.only(right: 15),
child: IconButton(
icon: Icon(
Icons.add_alert_rounded,
color: Colors.black,
),
onPressed: () {
setState(() {
isVisible = !isVisible;
});
},
),
),
],
),
body: Container(),
),
Positioned(
top: 72, //considering the app bar and system status bar height
right: 10,
child: Visibility(
visible: isVisible,
child: Stack(
children: [
Positioned( //the diamond behind the content
top: 0,
right: 20,
child: Transform.rotate(
angle: math.pi / 4, //rotating the container to turn it into a diamond
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey[400],
blurRadius: 5.0,
)
],
),
),
),
),
Container(
height: 40.0,
width: 200,
margin: const EdgeInsets.only(top: 5.0),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey[400],
blurRadius: 3.0,
offset: Offset(0, 4.0),
),
],
borderRadius: BorderRadius.circular(5.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
//just to represent the content of the bubble
Container(
padding: const EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
'number',
style: TextStyle(color: Colors.white),
),
),
Container(
padding: const EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
'5000',
style: TextStyle(color: Colors.white),
),
),
],
),
),
],
),
),
),
],
),
);
}

Card size not being adjusted when wrapped in a container

In my flutter app I have a GridView in which I list cards. In the cards I want to have an image and some text. I am trying to do this as follows:
import 'package:flutter/material.dart';
class Home extends StatefulWidget{
#override
State<StatefulWidget> createState() {
return HomeState();
}
}
class HomeState extends State<Home>{
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.green,
Colors.teal,
]
)
),
),
Padding(
padding: const EdgeInsets.only(top: 65),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.white60, width: 2.0)
),
padding: EdgeInsets.all(8),
child: CircleAvatar(
backgroundColor: Colors.white,
child: Icon(Icons.restaurant, size: 120,),
),
),
]
),
SizedBox(
height: 8,
),
Text(
"Genesis Technologies",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
color: Colors.white
),
),
SizedBox(
height: 6,
),
Text(
"Please take the following precautions",
style: TextStyle(
fontSize: 16,
color: Colors.white70
),
),
],
),
),
Container(
margin: EdgeInsets.only(top: 350),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
)
),
),
Container(
margin: EdgeInsets.only(top: 250),
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30)
)
),
child: Padding(
padding: EdgeInsets.all(0),
child: GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
children: <Widget>[
Container(
height: 200,
width: 100,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.restaurant, size: 120,),
Text(
"Cover your mouth and nose when yous sneeze or cough",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
],
),
),
),
),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.restaurant, size: 120,),
Text(
"Avoid touching your face with unwashed hands",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
],
),
),
),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.restaurant, size: 120,),
Text(
"Stay home if you don't need to get out for critical matters",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
],
),
),
),
],
)
),
),
],
),
),
),
);
}
}
And this renders the list of cards fine. My problem is that the cards bottom is being overflowed. I wrap the card in a container to fix this which looks like the follows:
Container(
height: 200,
width: 100,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.restaurant, size: 120,),
Text(
"Some text here",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
],
),
),
),
),
I don't understand why this is happening. Any help would be appreciated. Thanks.
ok... I usual don't do that but I had to rework the whole structure because most of the things didn't make any sense. Now I don't really understand.. Why do you want cards to increase size to not overflow.. Did you mean decrease size? The cards in your grid view will be always the same because you use grid view. You have set crossAxisCount to 2 so 2 items in x axis and square.. If you add more, then on smaller screen they will be accessible by scrolling and will not overflow. I have set position of the container where you have all your cards from the top to 1/3 of the screen height which perhaps is better then hardcoding it to fixed size as you had. Try it and let me know if you need to adjust it in any other way.
UPDATE
Scaffold(
body: Column(children: [
Flexible(
flex: 2,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.green,
Colors.teal,
])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
flex: 1,
child: FractionallySizedBox(
heightFactor: 0.5,
child: Container(
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
border:
Border.all(color: Colors.white60, width: 2.0)),
padding: EdgeInsets.all(8),
child: CircleAvatar(
backgroundColor: Colors.white,
child: Icon(
Icons.restaurant,
),
),
),
),
),
SizedBox(
height: 8,
),
Text(
"Genesis Technologies",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
color: Colors.white),
),
SizedBox(
height: 6,
),
Text(
"Please take the following precautions",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16, color: Colors.white70),
),
],
),
),
),
Flexible(
flex: 3,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.green,
Colors.teal,
])),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30))),
child: Padding(
padding: EdgeInsets.all(0),
child: GridView(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
children: <Widget>[
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(
Icons.restaurant,
size: 120,
),
Text(
"Cover your mouth and nose when yous sneeze or cough",
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
),
),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(
Icons.restaurant,
size: 120,
),
Text(
"Avoid touching your face with unwashed hands",
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
),
),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
elevation: 10,
child: Container(
margin: EdgeInsets.all(4),
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(
Icons.restaurant,
size: 120,
),
Text(
"Stay home if you don't need to get out for critical matters",
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
),
),
],
)),
),
),
),
]),
);