How to make Transparent Circle inside colored rectangle - flutter

I am new in flutter. I want to make the transparent Circle in side the semi transparent rectangle. Look like below,
I have not idea about that. So please help me to make this type of widget in flutter.

Using ColorFiltered and Stack you can achieve this.
(I took the answer mentioned in the comment and edited to your needs)
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://lh3.googleusercontent.com/proxy/_wrP_GRGOkGw0FLMiR3qeMzlyC-qN8Dd4sND89_xxLZMDIZh204g8PCccS-o9WaL1RKyiVOLGVS9QpodSkMMhOh8kbNh1CY492197-im-4vlFRRdsVqT2g4QbRlNgljDIg',
fit: BoxFit.cover,
), //Give your Image here
ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.5),
BlendMode.srcOut,
), // This one will create the magic
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
backgroundBlendMode: BlendMode.dstOut,
), // This one will handle background + difference out
),
Align(
alignment: Alignment.center,
child: Container(
height: MediaQuery.of(context).size.width,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(
MediaQuery.of(context).size.width / 2,
),
),
),
),
],
),
),
],
),
);
}

Related

insert 2 images together

I am trying to make this design from figma ui design and need to make the 2 pic inside each other but when i do it there is a space between them also the shaded part in the right pic how can i make it
but it did not work so It comes in this shape
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Row(
children: [
Expanded(
child: Container(
width: 235,
height: 235,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/back1.png',
),
fit: BoxFit.cover,
),
),
),
),
Expanded(
child: Container(
width: 279,
height: 279,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/back1.png',
),
fit: BoxFit.cover,
),
),
),
),
],
),
],
),
);
}
}
So what widget or things to add to make the 2 pic mixed
Use Stack and Positioned ,try this:
Stack(
children: [
Positioned(
top: 0,
right: 0,
child: Image.asset(
'assets/images/back1.png',
fit: BoxFit.cover,
width: 235,
height: 235,
),
),
Positioned(
top: 0,
left: 0,
child: Image.asset(
'assets/images/back1.png',
fit: BoxFit.cover,
width: 279,
height: 279,
),
),
],
),
use Row Widget and size the first image to have half screen using MediaQuery
then wrap the Row it's self to stack and add other one over the stack and position it wherever you want !
I think the best way is to export the two images together in Figma, forming a single image. You can do this by selecting both images > Group > Export. So you don't need almost any code to put them together. I know that's not the point, but it's a creative and valid solution for your case. I hope it helps.

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

Flutter Stack / Align

Align inside a stack gives me a result that I do not understand.
Here is the code:
Container(
height: totalHeight,
child: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Container(
width: avatarSize,
height: avatarSize,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
)
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 35,
height: 20,
color: Colors.yellow,
),
)
],
),
);
The yellow container is always appearing at the bottom left, no matter what I do.
I want to have it in the bottom center.
avatarSize is greater than 35 of course. I put an outer container to let the stack expand to that outer parent. Seems all natural, but not working. Weird.
What do I miss here?
The best widget to align something inside a Stack is the Positioned widget. I think it can solve your problem
Positioned Widget

Create widget with transparent hole inside

How can I create a semi-transparent background with the transparent hole inside? I tried to use decoration and foreground decorations with different blend modes, stack, ClipRect, colorfilters, but nothing works. I will appreciate any ideas. Thanks!
The "easiest" way I've found to do it is using a ColorFiltered Widget with a Stack.
The following code will create exactly what you need:
#override
Widget build(BuildContext context) {
return Material(
child: Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://wallpaperplay.com/walls/full/e/5/3/13586.jpg',
fit: BoxFit.cover,
),
ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.8), BlendMode.srcOut), // This one will create the magic
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
backgroundBlendMode: BlendMode.dstOut), // This one will handle background + difference out
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: const EdgeInsets.only(top: 80),
height: 200,
width: 200,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(100),
),
),
),
Center(
child: Text(
'Hello World',
style: TextStyle(fontSize: 70, fontWeight: FontWeight.w600),
),
)
],
),
),
],
),
);
}
This one you not only create "holes" over views, it works with anything! including texts, etc.
Final result:

How do I Set Background image in Flutter?

I am trying to set a background image for the home page. I am getting the image place from start of the screen and filling the width but not the height.
Am I missing something in my code? Are there image standards for flutter? Do images scale based on each phone's screen resolution?
class BaseLayout extends StatelessWidget{
#override
Widget build(BuildContext context){
return Scaffold(
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Image.asset("assets/images/bulb.jpg")
]
)
)
);
}
}
I'm not sure I understand your question, but if you want the image to fill the entire screen you can use a DecorationImage with a fit of BoxFit.cover.
class BaseLayout extends StatelessWidget{
#override
Widget build(BuildContext context){
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/bulb.jpg"),
fit: BoxFit.cover,
),
),
child: null /* add child content here */,
),
);
}
}
For your second question, here is a link to the documentation on how to embed resolution-dependent asset images into your app.
If you use a Container as the body of the Scaffold, its size will be accordingly the size of its child, and usually that is not what you want when you try to add a background image to your app.
Looking at this other question, #collin-jackson was also suggesting to use Stack instead of Container as the body of the Scaffold and it definitely does what you want to achieve.
This is how my code looks like
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(
children: <Widget>[
new Container(
decoration: new BoxDecoration(
image: new DecorationImage(image: new AssetImage("images/background.jpg"), fit: BoxFit.cover,),
),
),
new Center(
child: new Text("Hello background"),
)
],
)
);
}
Screenshot:
Code:
#override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage("your_asset"), fit: BoxFit.cover),
),
child: Center(child: FlutterLogo(size: 300)),
);
}
You can use Stack to make the image stretch to the full screen.
Stack(
children: <Widget>
[
Positioned.fill( //
child: Image(
image: AssetImage('assets/placeholder.png'),
fit : BoxFit.fill,
),
),
...... // other children widgets of Stack
..........
.............
]
);
Note: Optionally if are using a Scaffold, you can put the Stack inside the Scaffold with or without AppBar according to your needs.
I was able to apply a background below the Scaffold (and even it's AppBar) by putting the Scaffold under a Stack and setting a Container in the first "layer" with the background image set and fit: BoxFit.cover property.
Both the Scaffold and AppBar has to have the backgroundColor set as Color.transparent and the elevation of AppBar has to be 0 (zero).
Voilà! Now you have a nice background below the whole Scaffold and AppBar! :)
import 'package:flutter/material.dart';
import 'package:mynamespace/ui/shared/colors.dart';
import 'package:mynamespace/ui/shared/textstyle.dart';
import 'package:mynamespace/ui/shared/ui_helpers.dart';
import 'package:mynamespace/ui/widgets/custom_text_form_field_widget.dart';
class SignUpView extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack( // <-- STACK AS THE SCAFFOLD PARENT
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/bg.png"), // <-- BACKGROUND IMAGE
fit: BoxFit.cover,
),
),
),
Scaffold(
backgroundColor: Colors.transparent, // <-- SCAFFOLD WITH TRANSPARENT BG
appBar: AppBar(
title: Text('NEW USER'),
backgroundColor: Colors.transparent, // <-- APPBAR WITH TRANSPARENT BG
elevation: 0, // <-- ELEVATION ZEROED
),
body: Padding(
padding: EdgeInsets.all(spaceXS),
child: Column(
children: [
CustomTextFormFieldWidget(labelText: 'Email', hintText: 'Type your Email'),
UIHelper.verticalSpaceSM,
SizedBox(
width: double.maxFinite,
child: RaisedButton(
color: regularCyan,
child: Text('Finish Registration', style: TextStyle(color: white)),
onPressed: () => {},
),
),
],
),
),
),
],
);
}
}
We can use Container and mark its height as infinity
body: Container(
height: double.infinity,
width: double.infinity,
child: FittedBox(
fit: BoxFit.cover,
child: Image.network(
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
),
),
));
Output:
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/background.png'),fit:BoxFit.cover
)
),
);
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage("images/background.png"),
fit: BoxFit.cover
),
),
this also works inside a container.
Other answers are great. This is another way it can be done.
Here I use SizedBox.expand() to fill available space and for passing tight constraints for its children (Container).
BoxFit.cover enum to Zoom the image and cover whole screen
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand( // -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
fit: BoxFit.cover, // -> 02
),
),
),
),
);
}
To set a background image without shrinking after adding the child, use this code.
body: Container(
constraints: BoxConstraints.expand(),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/aaa.jpg"),
fit: BoxFit.cover,
)
),
//You can use any widget
child: Column(
children: <Widget>[],
),
),
You can use the following code to set a background image to your app:
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.jpg"),
fit: BoxFit.cover,
),
),
// use any child here
child: null
),
);
}
If your Container's child is a Column widget, you can use the crossAxisAlignment: CrossAxisAlignment.stretch to make your background image fill the screen.
I know there is a lot of answers to this question already, but this solution comes with a color gradient around the background image, I think you would like it
import 'package:flutter/material.dart';
class BackgroundImageExample extends StatelessWidget {
const BackgroundImageExample({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Stack(
children: [
backgroudImage(),
Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Column(
children: [
// your body content here
],
),
),
),
],
);
}
Widget backgroudImage() {
return ShaderMask(
shaderCallback: (bounds) => LinearGradient(
colors: [Colors.black, Colors.black12],
begin: Alignment.bottomCenter,
end: Alignment.center,
).createShader(bounds),
blendMode: BlendMode.darken,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('your image here'), /// change this to your image directory
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(Colors.black45, BlendMode.darken),
),
),
),
);
}
}
Stack(
children: [
SizedBox.expand(
child: FittedBox(
fit: BoxFit.cover,
child: Image.asset(
Images.splashBackground,
),
)
),
your widgets
])
This Helped me
import 'package:flutter/material.dart';
void main() => runApp(DestiniApp());
class DestiniApp extends StatefulWidget {
#override
_DestiniAppState createState() => _DestiniAppState();
}
class _DestiniAppState extends State<DestiniApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(245, 0, 87, 1),
title: Text(
"Landing Page Bankground Image",
),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage("images/appBack.jpg"),
fit: BoxFit.cover
),
),
),
),
),
);
}
}
Output:
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/bgmain.jpg'),
//fit: BoxFit.cover
fit: BoxFit.fill),
),
child: Column(
children:
[
//
],
),
)));
}
Image.asset(
"assets/images/background.png",
fit: BoxFit.cover,
height: double.infinity,
width: double.infinity,
alignment: Alignment.center,
),
if still there is a problèm, it' seem like your image is not perfection in the heigth and width
Here is how you can achieve this. First example is with assets image and the second one is with network image.
Local image:
Container(
height: 200,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/cat2.jpg"),
fit: BoxFit.cover),
),
child:
)
Network image:
Container(
height: 200,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://picsum.photos/id/237/200/300"),
fit: BoxFit.cover),
),
child:
)
You can use FractionallySizedBox
Sometimes decoratedBox doesn't cover the full-screen size.
We can fix it by wrapping it with FractionallySizedBox Widget.
In this widget we give widthfactor and heightfactor.
The widthfactor shows the [FractionallySizedBox]widget should take _____ percentage of the app's width.
The heightfactor shows the [FractionallySizedBox]widget should take _____ percentage of the app's height.
Example : heightfactor = 0.3 means 30% of app's height. widthfactor = 0.4 means 40% of app's width.
Hence, for full screen set heightfactor = 1.0 and widthfactor = 1.0
Tip: FractionallySizedBox goes well with the stack widget. So that you can easily add buttons, avatars, texts over your background image in the stack widget whereas in rows and columns you cannot do that.
For more info check out this project's repository github repository link for this project
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
Container(
child: FractionallySizedBox(
heightFactor: 1.0,
widthFactor: 1.0,
//for full screen set heightFactor: 1.0,widthFactor: 1.0,
child: DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/1.jpg"),
fit: BoxFit.fill,
),
),
),
),
),
],
),
),
),
);
}
}
OutPut:
It can be done either of the following ways, based on your requirement:
Background image spanning accross the app Bar
Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
elevation: 0,
title: const Text(
"Home Page",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.transparent,
),
body: Container(
height: double.infinity,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'https://wallpaperaccess.com/full/2440003.jpg'))
child: < Your Widgets go here >
),
));
Background image not spanning accross the app Bar
Scaffold(
appBar: AppBar(
elevation: 0,
title: const Text(
"Home Page",
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.transparent,
),
body: Container(
height: double.infinity,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'https://wallpaperaccess.com/full/2440003.jpg'))
child: < Your Widgets go here >
),
));
Extra :
To add background image only to the appBar refer this answer