Flutter: Background becomes half after putting logo image - flutter

So I am trying to put my image logo on the background, but my background suddenly became cropped with half of a black screen appearing. As per picture:
My main.dart code:
import 'package:flutter/material.dart';
import 'login_page.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new LoginPage(),
theme: new ThemeData(
primarySwatch: Colors.green
)
);
}
}
my login_page.dart code:
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget{
#override
State createState() => new LoginPageState();
}
class LoginPageState extends State<LoginPage>{
#override
Widget build(BuildContext context){
return new Scaffold(
backgroundColor: Colors.transparent,
body: new Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/arrowPNG.png'),
]
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/background.png"),
fit: BoxFit.cover,
)
)
)
);
}
}
It happened after putting the image logo which is 'Image.asset('assets/arrowPNG.png'),'. How do I resolve this?

Try this below code
class LoginPageState extends State<LoginPage>{
#override
Widget build(BuildContext context){
return new Scaffold(
backgroundColor: Colors.transparent,
body: new Container(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('images/a_dot_ham.png'),
]
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.jpg"),
fit: BoxFit.cover,
)
)
)
);
}
When you add the below line to your Container, the issue will get resolved
width: MediaQuery.of(context).size.width
Here is my result

Try to add, width: MediaQuery.of(context).size.width height: MediaQuery.of(context).size.height to Container like this,
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/logo.png'),
]
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/splash_bg.png"),
fit: BoxFit.cover,
)
)
)
Output

Related

How to remove white background in picture?

Design I want:
My current design:
How to make the letter back to normal color?
My code:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Stack(
children: [
Image.asset(
'assets/images/front_Page_Image.png',
height: 746,
width: constraints.maxWidth * 0.64,
fit: BoxFit.cover,
),
Image.asset(
'assets/images/half_circle.png',
color: Color.fromARGB(255, 255, 255, 255),
//colorBlendMode: BlendMode.srcOver,
)
],
),
],
),
You can use Opacity with color as below for transparent white background
color : Colors.white.withOpacity(0.5)
For image Opacity :
Container(
child: new Text(
'Hello world',
style: Theme.of(context).textTheme.display4
),
decoration: new BoxDecoration(
color: const Color(0xff7c94b6),
image: new DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.2), BlendMode.dstATop),
image: new NetworkImage(
'http://www.allwhitebackground.com/images/2/2582-190x190.jpg',
),
),
),
),
It is required that the image you want to "remove the background" for, is transparent except for the text. Meaning what you'd like to remove the white for, is actually the Containers background. Meaning that you can use something like Colors.white.withOpacity(0.3) to achieve the transparency.
A complete example that will produce this image:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Stack(
children: [
Card(
child: Image.asset('assets/this_is_fine.jpg'),
),
Container(
width: MediaQuery.of(context).size.width * 0.5,
color: Colors.white.withOpacity(0.3),
child: Image.asset('assets/fine_text.png'),
)
],
)
],
),
);
}
}
I think you shouldn't wrap them in a Card. The first Container also serves no purpose. So like
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Stack(
children: [
Image.asset(
'assets/images/front_Page_Image.png',
height: 746,
width: constraints.maxWidth * 0.64,
fit: BoxFit.cover,
),
Container(
alignment: Alignment.topLeft,
child: Image.asset(
'assets/images/half_circle.png'),
)
],
),
],
)

how to make a icon get out from container on flutter

Hi guys i'm try to make this on flutter: img
i'm try with that code, but work
Container(
child: Positioned(top:0, left:0)
)```
as an option you can use Stack as container for card widget + image
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Stack(
children: <Widget>[
Card(
color: Colors.blueAccent,
child: Container(
width: 300,
height: 80,
),
),
FractionalTranslation(
translation: Offset(0, -0.5),
child: Icon(Icons.android, size: 48),
),
],
),
),
),
);
}
}

Flutter set background image for a page

I want to set the image as the background color for the page but the code below seems not to work well. I'm not sure if there is something missing in my code or mistake inside. And there is no error message shown.
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.tealAccent,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background.png"),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 50),
_signInButton(),
],
),
),
),
);
}
you have to just use stack widget just like this
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/login.png"), fit: BoxFit.fill)),
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: <YOUR CHILD COMPONENTS WILL BE PLACED HERE>
),
],
),
Hope it will help!

Flutter row not showing children in a non-material app

I have the following very simple Flutter code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
)
);
}
}
I expect to see an app with a green background, and 2 square boxes in a line.
However, I only seem to see the green background and no boxes.
I have also experimented with mainAxisAlignment and crossAxisAlignment.
What do I seem to be doing wrong?
Docs:
https://api.flutter.dev/flutter/widgets/Row-class.html
You need to wrap your container in a WidgetsApp or the more commonly used MaterialApp or CupertinoApp that builds upon WidgetsApp. These widgets provide the default configuration required for a flutter application. Like navigation behaviour and such, see this link for more info: https://api.flutter.dev/flutter/widgets/WidgetsApp/WidgetsApp.html
An working examples:
import 'package:flutter/material.dart';
//import 'package:flutter/cupertino.dart'; // If using CupertinoApp
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return WidgetsApp( // Alternatively replace with MaterialApp or CupertinoApp
color: Colors.white,
builder: (context, widget) {
return Container(
color: Colors.green,
child: Row(
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
)
);
},
);
}
}
Hope this helps :-)
I ran your code and it raised the exception
Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined.
I added textDirection to Row and it ran as expected
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
width: double.infinity,
height: double.infinity,
color: Colors.green,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
),
),
),
),
);
}
}

How to fit image as the background of status bar in flutter?

I am trying to apply image in the background of the status bar in an flutter project. So which widget can help me to apply the following expected result.
detailpage.dart
import 'package:flutter/material.dart';
class MyDetailPage extends StatefulWidget {
#override
_MyDetailPageState createState() => _MyDetailPageState();
}
class _MyDetailPageState extends State<MyDetailPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
height: 300,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/image2.png"),
fit: BoxFit.fitHeight,
),
),
)
],
),
);
}
}
Use Scaffold property
extendBodyBehindAppBar: true, and
set statusBarColor: Colors.transparent
This code is for status bar colour.
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent
));
Here is an example of code in my current project.
#override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent
));
return Scaffold(
extendBodyBehindAppBar: true,
body: Column(
children: [
Container(
height: 250,
child: Container(
child: Stack(
alignment: Alignment.topLeft,
children: [
Container(
width: screenWidthPercentage(context,percentage: 1),
child: Image.network(
contentImage,
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.only(top: 48.0,left: 12),
child: Icon(Icons.arrow_back,color: Colors.white,),
),
],
),
),
),
],
),
);
}
}
you can do it by not keeping SafeArea widget and set statusbar color as transparent.
I have created an example as below.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class FullScreen extends StatefulWidget {
const FullScreen({Key? key}) : super(key: key);
#override
_FullScreenState createState() => _FullScreenState();
}
class _FullScreenState extends State<FullScreen> {
#override
void initState() {
super.initState();
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light
//color set to transperent or set your own color
)
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: buildBody(),
);
}
Widget buildBody() {
return SizedBox(
height: 400,
width: double.infinity,
child: Card(
clipBehavior: Clip.antiAlias,
margin: EdgeInsets.all(0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40)),
),
child: Image.network(
'https://source.unsplash.com/random',
fit: BoxFit.cover,
),
),
);
}
}
I have added card as it gives nice elevation effect to the image 😎 .
FYI keep margin as 0 in card if you are using card.
You can achieve it using FlexibleSpaceBar, here is the example code.
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
expandedHeight: 300,
flexibleSpace: FlexibleSpaceBar(
background: Image.asset("assets/images/chocolate.jpg", fit: BoxFit.cover),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(title: Text("Item ${index}")),
childCount: 100,
),
),
],
),
);
}
When we using a ListView on a top tree,
The ListView makes a default padding.top even without a SafeArea.
So we need a
padding: EdgeInsets.zero.