How to resize the width of the image in flutter? - flutter

Hello! The width of the image is same as the width of the phone screen. What's the problem and how can I reduce the size of the width? Thank you!
class _SignupProfileImageState extends State<SignupProfileImage> {
bool isUploadImage = false;
var selectedImage;
#override
Widget build(BuildContext context) {
return Positioned(
top: 140,
right: 0,
left: 0,
child: SizedBox(
height: 100,
width: 100,
child: Stack(
clipBehavior: Clip.none,
fit: StackFit.expand,
children: [
Container(
width: 50,
child: ClipOval(
child: Image.asset(
'assets/face.jpg',
height: 50.0,
width: 50.0,
fit: BoxFit.fill
],
),
)
);
}
}

You can use CircleAvatar instead of ClipOval like below and it will be a better choice.
CircleAvatar class you can check
Container(
width: 50,
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: 90.0,
child: Image.asset(
"assets/face.jpg",
height: 50.0,
width: 50.0,
fit: BoxFit.fill
),
)
)

You can use the code below:
class SignupProfileImage extends StatefulWidget {
const SignupProfileImage({Key? key}) : super(key: key);
#override
State<SignupProfileImage> createState() => _SignupProfileImageState();
}
class _SignupProfileImageState extends State<SignupProfileImage> {
bool isUploadImage = false;
#override
Widget build(BuildContext context) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 100,
width: 100,
child: ClipOval(
child: Image.asset(
'assets/face.png',
fit: BoxFit.fill,
),
),
),
],
);
}
You can also access the complete source code through GitHub.
If you have further questions, please don't hesitate to write in the comments.

Wrap your SizeBox with Center. It will show your Image in Center
return Positioned(
top: 140,
right: 0,
left: 0,
child: Center( //TODO: Wrap SizedBox with Center
child: SizedBox(
height: 100,
width: 100,
child: Stack(
clipBehavior: Clip.none,
fit: StackFit.expand,
children: [
Container(
width: 50,
child: ClipOval(
child: Image.asset(
'assets/face.jpg',
height: 50.0,
width: 50.0,
fit: BoxFit.fill
],
),
),
)
);

Related

How to display cards on a board (for a card game)?

I'm implementing a phone app for playing UNO using flutter (and dart), and I'm stuck with how I should display the cards on the board.
This is what I would like my code to look like
and this is what my code result looks like.
import 'package:flutter/material.dart';
import 'card.dart';
class Partida extends StatefulWidget {
const Partida({ Key? key }) : super(key: key);
#override
State<Partida> createState() => _PartidaState();
}
class _PartidaState extends State<Partida> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 19, 107, 22),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
MyCard(),
MyCard(),
],
),
// MyCard(),
// Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// // MyCard(),
// MyCard(),
// ]
// ),
],
),
),
),
);
}
}
Thanks in advance.
You can use the Stack() widget to achieve this. Start by giving clipBehaviour:Clip.none which will help overflow the cards when u stack multiple of them.
Then use the Positioned() widget to make them shift to left using property called left: 50 (or whatever your preferred number). This will shift the cards to left.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
alignment: AlignmentDirectional.bottomCenter,
children: const [
ColoredBox(
color: Colors.red,
child: SizedBox(
height: 100,
width: 80,
),
),
Positioned(
left: 50,
bottom: 0,
child: ColoredBox(
color: Colors.green,
child: SizedBox(
height: 100,
width: 80,
),
),
),
Positioned(
left: 100,
bottom: 0,
child: ColoredBox(
color: Colors.blue,
child: SizedBox(
height: 100,
width: 80,
),
),
),
Positioned(
left: 150,
bottom: 0,
child: ColoredBox(
color: Colors.yellow,
child: SizedBox(
height: 100,
width: 80,
),
),
),
],
);
}
}
Output

circle in container Flutter

I have a flutter app,
and I need to make a circle in a container in flutter.
I try to create a container and put an image inside it but the circle is to small,
here is my code:
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("ipage.jpeg),
fit: BoxFit.fitWidth,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: CircleAvatar(backgroundColor: Colors.transparent,
child: Image.network('https://image.flaticon.com/icons/png/512/147/147144.png' , height:150,),
),
),
),
Please help me how can I do that in flutter
according to your demand you need to use Stack here.
This can archive this way
import 'package:flutter/material.dart';
class FFTBuild extends StatelessWidget {
const FFTBuild({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final mainBoxheight = MediaQuery.of(context).size.height / 1.9;
return Container(
height: mainBoxheight,
color: Colors.cyanAccent,
child: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Image.asset(
"assets/image.jpg",
height: mainBoxheight * .85,
width: double.infinity,
fit: BoxFit.cover,
),
),
Align(
alignment: Alignment.bottomCenter,
child: Image.network(
'https://image.flaticon.com/icons/png/512/147/147144.png',
fit: BoxFit.cover,
width: 150,
height: 150,
),
),
],
),
);
}
}
But I prefer LayoutBuilder instead of using MediaQuery
class FFTBuild extends StatelessWidget {
const FFTBuild({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
// final mainBoxheight = MediaQuery.of(context).size.height / 1.9;
return LayoutBuilder(
builder: (context, constraints) => Container(
height: constraints.maxHeight * .4,
///I added `Container Color`to clarify the thing,
// color: Colors.cyanAccent,
child: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Image.asset(
"assets/image.jpg",
height: constraints.maxHeight * .3,
width: constraints.maxWidth,
fit: BoxFit.cover,
),
),
Align(
alignment: Alignment.bottomCenter,
child: Image.network(
'https://image.flaticon.com/icons/png/512/147/147144.png',
fit: BoxFit.cover,
width: 150,
height: 150,
),
),
],
),
),
);
}
}
you may need to set tweak the value according to your desire. let me know if you need more on it.
Simply add radius to the circle avatar and modify circle avatar a bit
Container(
height: MediaQuery.of(context).size.height / 1.9,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("${ImagePath.profileCover}"),
fit: BoxFit.fitWidth,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: CircleAvatar(
radius: 100,//increase or decrease this for the size of your image
backgroundImage: NetworkImage('https://image.flaticon.com/icons/png/512/147/147144.png'),
),
),
),

How can I create this ui in Flutter...?

I want to code this player card in flutter.
need to design this ,I can create this as a card but the curves are difficult.
sharing code below,
I can create rows and columns but the curves are difficult.please help
class PLayerCard extends StatefulWidget {
PLayerCard(this.image);
String image;
#override
_PLayerCardState createState() => _PLayerCardState();
}
class _PLayerCardState extends State<PLayerCard> {
#override
Widget build(BuildContext context) {
return Container(
height: SizeConfig.screenHeight * .4,
width: SizeConfig.screenWidth * .4,
child: Column(
children: [
Row(
children: [
Container(
height: SizeConfig.screenHeight * .2,
color: Colors.green,
child: Column(
children: [Text("Points"), Text("305")],
)),
Container(
child: widget.image != null
? Image(
height: SizeConfig.screenHeight * .2,
width: SizeConfig.screenWidth * .29,
image: NetworkImage(
widget.image,
),
fit: BoxFit.cover)
: Image(image: AssetImage('assets/user-avatar.png')),
),
],
),
Container(
height: 20,
color: Colors.green[800],
),
Container(
height: 50,
color: Colors.green,
)
],
),
);
}
}

Swipe circle to open new Activity(page) in flutter

I want to open new page when I swipe the circle in the middle of the screen. that is when I swipe blue circle left a new page should open and when I swipe right another new page should open.
Similarly when I swipe pink circle bottom another new page should open
Here is my code:
import 'package:flutter/material.dart';
class FirstRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 300.0,
width: 150.0,
child: Stack(
children: <Widget>[
Positioned(
top: 25,
child: Container(
height: 150.0,
width: 150.0,
decoration: BoxDecoration(
shape:BoxShape.circle,
color:Colors.lightBlue,
),
),
),
Positioned(
bottom: 25,
child: Opacity(
opacity: 0.45,
child: Container(
height: 150.0,
width: 150.0,
decoration: BoxDecoration(
shape:BoxShape.circle,
color:Colors.pinkAccent,
),
),
),
),
],
),
),
),
);
}
}
wrap Positioned() with SwipeDetector() Widget
Have a look at this example
SwipeDetector(
onSwipeRight: () {
//push new page
Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new YourNewPage()));
},
child: //Your circle here
Positioned(
top: 25,
child: Container(
height: 150.0,
width: 150.0,
decoration: BoxDecoration(
shape:BoxShape.circle,
color:Colors.lightBlue,
),
),
),
)

Flutter SingledChildScrollView > Stack > Positioned is glitches on Scroll

I am using a SingleChildScrollView with a Stack as its first element. The Stack contains two containers. The second one is positioned at bottom: 0.0 using a Positioned.
I highly simplified my view for this post to focus only on this issue. When I scroll slowly, you can see that the white container is "glitching" and you see a line that is the bottom of the first child of the stack.
Here is the view:
Not that's what happen when I scroll down, I have no clue why it's glitching like that:
View:
return Container(color: Colors.white, child: SingleChildScrollView(child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
HeaderComponent(),
Container(height: 600, width: MediaQuery.of(context).size.width)
]
)));
Header Component
class _HeaderComponentState extends State<HeaderComponent> {
#override
Widget build(BuildContext context) {
return Stack(
children: [
Container(height: 245.0, width: MediaQuery.of(context).size.width, color: Colors.red),
Positioned(bottom: 0.0, child: Container(height: 40.0, width:
MediaQuery.of(context).size.width, color: Colors.white)),
],
);
}
}
If that, just add bottom padding 0.2 at first child.
class HeaderComponent extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
height: 245.0,
padding: EdgeInsets.only(bottom: 0.2),
width: MediaQuery.of(context).size.width,
color: Colors.red),
Positioned(
bottom: 0.0,
child: Container(
height: 40.0,
width: MediaQuery.of(context).size.width,
color: Colors.white)),
],
);
}
}
I don't know root cause. But Positoned's 'bottom: 0.0' occurs upper container's background color a little during scrolling.
So when I set 'bottom: -0.2', this problem is not occured.
But it does not looks cool...
class HeaderComponent extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
height: 245.0,
width: MediaQuery.of(context).size.width,
color: Colors.red),
Positioned(
bottom: -0.2,
child: Container(
height: 40.0,
width: MediaQuery.of(context).size.width,
color: Colors.white)),
],
);
}
}
I suggest another solution like below.
class HeaderComponent extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 245.0,
child: Column(
children: <Widget>[
Expanded(
child: Container(
width: MediaQuery.of(context).size.width, color: Colors.red),
),
Container(
height: 40.0,
width: MediaQuery.of(context).size.width,
color: Colors.white)
],
),
);
}
}