Can not render image in flutter Splash screen/First Screen(iOS only) - flutter

I am adding an background image to the Splash screen, but is not rendering the image. Sometime it load sometime not.If I load in the Second screen it loads properly. The issue is in iOS not in Android.
Here is a sample code:
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
//runApp()
// SplashWidget
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: UIData.buildTheme(),
home: SplashS(),
));
}
class SplashS extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(body:Container(
color: Colors.yellow,
child: Center(
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child:Image.asset(
"images/image_name.png",
fit: BoxFit.fill,
)),
),
));
}
}

Have you included the image path in your pubspec.yaml?
assets:
- images/image_name.png

Related

flutter, How to write precise responsive code from design

Is there any proper method to write responsive app without any additional package? I usually use MediaQuery but I feel it's not proper enough because it's hard to measure the size from figma since figma use number so i have to calculate it but im afraid everytime i calculate it's not precise and for the font size what is used to make it responsive?
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
By using Sizer plugin we can make flutter app responsive.
https://pub.dev/packages/sizer
See the example below:
add into pubspec.yaml
dependencies:
flutter:
sdk: flutter
sizer: ^2.0.15
add import statement to code
import 'package:sizer/sizer.dart';
Wrap MaterialApp with ResponsiveSizer widget
main.dart
import 'package:flutter/material.dart';
import 'package:mediaquerydemo/sizer_demo.dart';
import 'package:sizer/sizer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Sizer(
builder: (context, orientation, deviceType) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SizerDemo(),
);
}
);
}
}
Your actual code where you can show UI
SizerDemo.dart
import 'package:flutter/material.dart';
import 'package:sizer/sizer.dart';
class SizerDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
final deviceInfo = MediaQuery.of(context);
late var h = deviceInfo.size.height;
late var w = deviceInfo.size.width;
return Scaffold(
appBar: AppBar(
title: Text("Responsive App"),
),
body: deviceInfo.orientation == Orientation.portrait
? SizedBox(
width:30.w,
height: 20.h,
child: Container(
child: Center(child: Text("HELLO", style: TextStyle(fontSize: 20.sp),)),
color: Colors.green,
),
)
: SizedBox(
width:30.w,
height: 20.h,
child: Container(
child: Center(child: Text("World!", style: TextStyle(fontSize: 20.sp),)),
color: Colors.red,
),
)
);
}
}

Dart Flutter display Icons

Im writing my first code and I want to display a Icon, and I try to use the command Icon (Icons.star, color: Colors.red, size: 100, ),. But it doesn't work. Can somebody help me. Thx
Welcome to Stackoverflow.
Try the following code. Your above code is also correct.
Try to check below line in your pubspec.yaml file
flutter:
uses-material-design: true
Refer Flutter Icons
Full Example:
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Icon(
Icons.star,
color: Colors.red,
size: 100,
),
);
}
}
Result screen->
You can also test the code on Dartpad.

Is there a way to have width of container bigger than the screen in Flutter?

I have a following container which is actually bigger than the screen. But it seems like flutter doesn't allow the container width to be bigger than the screen.
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Test(),
);
}
}
class Test extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
print("Tapped");
},
child: Transform.rotate(
angle: math.pi / 4,
child: Container(
height: 200,
width: 700,
color: Colors.blue,
),
),
),
);
}
}
I tried to use Transform.scale instead but the GestureDetector doesn't work on the parts of container that is bigger than the screen.
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Test(),
);
}
}
class Test extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
print("Tapped");
},
child: Transform.rotate(
angle: math.pi / 4,
child: Transform(
transform: Matrix4.diagonal3Values(4, 1, 1),
child: Container(
height: 200,
width: 300,
color: Colors.blue,
),
),
),
),
);
}
}
The container will be resized using a resize handle. I'm okay with container not having width greater than screen whilst it's not rotated but when it's rotated I want the container to have width based on resize handle which can be greater than screen. Is there any workaround for this? Any help would be greatly appreciated.
I found a way and that is to use Stack & Positioned:
Stack(
children: [
Positioned(top: 0,
left: 0,
height: 200,
width: 1000,
child: Container(height: 200, width: 1000, color: Colors.blue),
),
],
),
Adjust the height and width of Positioned to limit how much width and height container can have.
From what I know it's not possible to have a container bigger than the screen. You have to take into account that the idea is for the app to run on different sized screens, so I personally don't see the point in giving static values when you can use dynamic ones so it fits different devices' sizes.

How to expand this lottie animation to fill the whole screen - Flutter

I am currently working on having a gradient background animation in my app... I am doing so with the help of a lottie animation! I have tried to enclose it in a container and have succeeded in doing so. However there is one issue, I am not able to make the container bigger than a certain amount despite me changing the height to something even bigger than 2000... I really dont know what to do to make sure that there are no whitespaces in the screen and that this gradient fills the screen in all devices. Here is the code. I have also added in a screenshot of how it looks so that you get an idea of whats happening.
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
class WelcomeScreen extends StatefulWidget {
#override
_WelcomeScreenState createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.all(0),
child: Container(
height: 1000,
width: 1000,
child: Lottie.asset('assets/gradient-background.json'),
),
),
));
}
}
I am new to flutter development so please forgive me if this is a very silly mistake! Thanks a lot and i really appreciate your help!
First of all, i would like to thank you all for your help. Special thanks to Nehal because he made me aware about the fit property which turns out to be a feature of a lottie asset animation! Thanks so much and this is the correct code:
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
class WelcomeScreen extends StatefulWidget {
#override
_WelcomeScreenState createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: double.infinity,
width: double.infinity,
child:
Lottie.asset('assets/gradient-background.json', fit: BoxFit.cover),
),
));
}
}
I was able to fix this by using OverflowBox.
SizedBox(
height: 120,
child: OverflowBox(
minHeight: 170,
maxHeight: 170,
child: Lottie.asset(
'assets/file.json',
repeat: false,
),
),
)
Use an expanded widget and dont use any padding. You dont need to mention the height or width of the container and use the background property in decoration of container
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
class WelcomeScreen extends StatefulWidget {
#override
_WelcomeScreenState createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/gradient-background.json'),
fit: BoxFit.cover,
),
),
),
),
),
);
}
}
Also you can make a custom gradient in flutter rather than using a background photo.

Normal Appbar with image background in Flutter

I am new in a flutter. Kindly don't mind if this question is weird. I need a background image in AppBar. I have found on the internet and got the solution with a widget SliverAppBar. I need the image background image in normal appear. I found there is no property of image or background image in AppBar. Can anyone help me with this?
Thanks! Darshan.
welcome to stackoverflow.
I have made this demo earlier for understanding. Kindly follow the below example code.
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: AppBarWithImage(),
);
}
}
class AppBarWithImage extends StatefulWidget {
#override
_AppBarWithImageState createState() => _AppBarWithImageState();
}
class _AppBarWithImageState extends State<AppBarWithImage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('App Bar with image background'),
flexibleSpace: Image(
image: NetworkImage(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
),
fit: BoxFit.cover,
),
backgroundColor: Colors.transparent,
),
body: Center(
child: Text("Sample Text"),
),
);
}
}
Output:
Conclusion
the property called "flexibleSpace" is what you are looking for the background image.