So I'm building a flutter app, a screen contains a column with four containers. I haven't specified the containers heights as I want them to take up the whole screen. Is there a way to convert the mainAxisSize to a float so that I can set the height of the containers to a quarter of the axis size. Thanks
For the containers you can use the attribute heigth. Then, you can use
screenHeigth=MediaQuery.of(context).size.heigth to obtain the heigth size of the phone you are using. Last of all, you can set the Containes' heigth attribute to: screenHeigth*0.25
So, each of them are 1/4 of the screen size high!
Here's the quick solution I think you are looking for. If not, let me know what to achieve other than this:
Demo:
**Code: **
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
// Getting Quarter Height of the screen
double qtrScreen = MediaQuery.of(context).size.height * 0.25;
return Scaffold(
body: Column(
children: [
Container(
color: Colors.red,
height:
qtrScreen, // Providing the Quarter height to all Containers
),
Container(
color: Colors.green,
height: qtrScreen,
),
Container(
color: Colors.blue,
height: qtrScreen,
),
Container(
color: Colors.orange,
height: qtrScreen,
),
],
),
);
}
}
You can use expanded and define flex property as you need. You don't need to create values using media query as it makes the code more complex for beginners.
Related
I have a Text widget each time I change the size of the Text widget it keeps increasing in width and height, but i want to only Increase the Height of the Text widget.
I think you can make it by using the Transfrom widget.
Example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(home: MyHomePage());
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Transform(
transform: Matrix4.identity()..scale(1.0, 1.5),
child: const Text(
'Transformed Text',
),
),
const SizedBox(height: 16.0),
const Text('Default Text'),
],
),
),
);
}
}
You can adjust in xml file to set height and width differently
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.
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.
My Image.asset file doesn't display my image but other widgets display text, card, etc
assets:
- images/internet_image.jpg
class SplashScreen extends StatefulWidget{
#override
_SplashScreenState createState() =>
_SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen>{
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Stack(
children: <Widget>[
Column(
children: <Widget>[
Image.asset(
'images/internet_image.jpg',
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 1/4,
fit: BoxFit.fitWidth,
),
],
),
],
),
);
}
}
assets: - images/downloaded_image.jpg
and the referenced asset should be the same:
Image.asset('images/downloaded_image.jpg'),
In your code you refer to an asset named images/internet_image.jpg
An extra layer of widget might look like the code below so that the Scaffold doesn't have to field the context call.
class SplashScreen extends StatefulWidget{
#override
_SplashScreenState createState() =>
_SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen>{
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SplashScreenStack(),
);
}
}
class SplashScreenStack extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Column(
children: <Widget>[
Image.asset(
'images/internet_image.jpg',
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 1/4,
fit: BoxFit.fitWidth,
),
],
),
],
);
}
}
Thanks a lot every one for answering. All solutions provided would definitely fix the issue, I decided to upload my splash screen the normal way via pubspec.yaml rather than trying to create a splash screen page.
I'd like to fix an Image in the footer of the screen, so in different phones it stays always in the bottom .
What I tried
I tried to use Column, and then in the mainAxisAlignment I would use MainAxisAlignment.end
Additional Question : Is there a way to create a canvas and place it on the image, so I can use relative coordinate on the image if I want to draw something instead of the full screen
You can use the bottomSheet in the scaffold. This automatically allows you to have the widget fixed at the bottom of the display
return Scaffold(
bottomSheet: yourWidget(),
body: Container(color: Colors.red,)
);
Please write a question for each question you have in the future - that makes them more searchable and useful in the future for other people.
However, I think I can answer both pretty simply so I will.
Firstly, if you want an image to always be at the foot of the screen, you can simply use a the bottomNavigationBar property of the Scaffold. It looks like it would have to be a BottomNavigationBar but you can actually pass any widget you'd like.
Here's the code (you can paste it into a file and run that file as it's self-enclosed):
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
#override
State<StatefulWidget> createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
bottomNavigationBar: Stack(
children: [
new Container(
height: 100.0,
color: Colors.green,
),
Positioned(
left: 0.0,
right: 0.0,
top: 0.0,
bottom: 0.0,
child: new CustomPaint(
painter: Painter(),
size: Size.infinite,
),
),
],
),
),
);
}
}
class Painter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
canvas.drawLine(Offset.zero, size.bottomRight(Offset.zero), Paint());
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
// bad, but okay for example
return true;
}
}
I've just used a simple painter to illustrate the 'canvas' and how to position it the same as the picture, and a Container with a colour instead of a picture as I don't feel like adding assets to my project. The Positioned widget makes it so that the canvas can be sized to the image and not the other way around, as would happen if you just put them right into the Stack.
Note that if you don't want to use scaffold, you could probably also do this using a Column, with whatever you want for the body wrapped in an Expanded widget (as that will make it expand to fit as much space as possible, which should be everything except the space the image takes up).
you can use persistentFooterButtons widget inside Scaffold
#override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Demo App'),
),
body: _questionIndex < _questions.length
? Quiz(
list: _questions,
selectHandler: _printAns,
questionIndex: _questionIndex)
: Result(_totalScore, _resetQuiz),
persistentFooterButtons: <Widget>[
Row(
children: <Widget>[
RaisedButton(
onPressed: null,
child: Text('Prev'),
),
RaisedButton(
onPressed: null,
child: Text('Next'),
),
],
)
],
),
);