How can i fix my flutter app to be responsive in different screen sizes? - flutter

I'm new to flutter I made a task manager app but the applications screen is overflowing on different phones.
I'm not exactly sure where can I edit the code for a responsive pixel-perfect screen!
It'll be helpful if someone explains it clearly!
Here's my homepage code:
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/Background.jpg"),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.1),
BlendMode.darken,
),
),
),
width: double.infinity,
padding: EdgeInsets.symmetric(
horizontal: 24.0,
),
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(
top: 32.0,
bottom: 32.0,
),
Deleted some unnecesarry bottom code cause

You can do it in many way such as:
Use LayoutBuilder like this:
LayoutBuilder(
builder: (BuildContext ctx, BoxConstraints constraints) {
// If the screen width >= 700i.e Wide Screen
if (constraints.maxWidth >= 700) {
...
}
// If screen size is < 700
else {
...
}
},
),
Simply use MediaQuery.of(context).size.width; like this:
Scaffold(
body: Container(
child: MediaQuery.of(context).size.width > 700
? LargeWidget()
: SmallWidget()
),
)
Or many different ways

Related

Stack Widget Not Scrolling in SCSV, Flutter

I'm using a Stack to Positioned one widget on top of an Image.asset widget, but the problem I have is that my whole Stack widget is not scrollable to see the whole content. I wrapped the Stack widget with a SingleChildScrollView but the problem still persists. If I wrap the Stack widget with a Containter and give the height: MediaQuery.of(context).size.height, it's scrollable but I can't still see the whole content of the page. Where is my mistake, that's what I'm wondering? With what widget should I wrap Stack or is there a better way for my problem? In short, I'm stacking a Column on top of an Image and I need the whole Stack widget to be scrollable so I can see the whole content of the Stack widget. Here is the code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: SafeArea(
child: SingleChildScrollView(
child: Stack(
overflow: Overflow.visible,
children: [
Image.asset(
'lib/modules/common/images/logo.png',
width: double.infinity,
height: 196.0,
),
Positioned(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
top: 136.0,
child: Column(
children: [
Container(), // some content inside the container
Container(), // // some content inside the container
],
),
),
],
),
),
),
);
}
Thanks in advance for the help!
if you mean to have a image on back and scrollable content at fron check this answer, if not let me know and share a prototype/image that you want.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: SafeArea(
child: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 3,
child: Stack(
fit: StackFit.loose,
children: [
Align(
alignment: Alignment.topCenter,
child: Image.asset(
'assets/image.jpg',
width: double.infinity,
height: 196.0,
),
),
Positioned(
top: 136.0,
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisSize: MainAxisSize.min,
children: List.generate(
2,
(index) => Container(
height: 100,
width: double.infinity,
color: index % 2 == 0
? Colors.amberAccent
: Colors.cyanAccent,
), // some content inside the container
// some content inside the container
),
),
),
),
],
),
),
),
),
);
}

How to scroll stacked containers with sticky header in Flutter?

I am trying to achieve scroll in Flutter Web where I have few containers which are stacked and I use SingleChildScrollView to scroll the widget. However, when I scroll the first container everything working fine but the second one which is a child of the second container responds to the scroll without completing the initial one. And also is there a way to make a sticky header for the second container. How can I make the 3rd container(orange) to scroll after the 2nd(blue) one is finished scrolling? Here is what I am trying to achieve:
https://yobithemes.com/demo/html/freda/dark-video-index.html
And here what I got so far:
class MainScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
IntroScreen(),
SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).size.height - 100,
),
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.blue,
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height,
),
Container(
padding: EdgeInsets.only(top: 100),
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.orange,
),
],
),
),
),
],
),
),
),
],
),
);
}
}
You can achieve it by using sliver.
SliverToBoxAdapter fill the transparent area with screen height - app bar height.
SliverAppBar: make it sticky by setting floating and pin to true
class MainScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
IntroScreen(),
CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Container(
height: MediaQuery.of(context).size.height - 50,
),
),
SliverAppBar(
// toolbarHeight: 50,
floating: true,
pinned: true,
title: Container(
child: Center(child: Text('Header')),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => Container(
height: MediaQuery.of(context).size.height-50,
color: Colors.primaries[index % Colors.primaries.length],
),
),
),
],
),
],
),
);
}
}

contents of SingleChildScrollView isn't becoming full height

I have a SingleChildScrollView > Column > [A bunch of widgets, Text (really long text)]
I'm trying to make the Text go to the bottom of the screen so I tried 2 things and they both gave me errors
I tried adding a Spacer before the Text, but I got a RenderFlex error. I then tried wrapping all the widgets in a second column and leaving the Text widget in the 1st/original column. I also got an error with that.
How can I make the Text Widget go all the way to the bottom of the screen?
In SingleChildScrollView you can't use Spacer or Expanded widget. You should give height to your widget. I offer you try Stack widget like code below:
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: [
SingleChildScrollView(
child: Column(
children: [
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: Text('test'),
)
],
),
));
}

Container inside SingleChildScrollView doesnt show up without defining width and height

I have a body of scaffold that structured like this :
body : Column(
children : [
Expanded(
child : SingleChildScrollView(
Container (
Column(
//content
)
)
)
),
BottomMenu()
]
)
And the problem is the content would not show up if i dont define the width and height for the Container inside SingleChildScrollView, but if i define the height then the height of my childscrollview wont work dynamically with the content, i want the height to fit the content inside of my container so i can scroll until the end of content. I am still new to this so any advice and suggestion will be appreciated. Thankyou
It is better to use Stack instead of the column as top Widget like this:
Stack(
children: <Widget>[
SingleChildScrollView(
Container (
Column(
//content
)
)
),
Align(
alignment: Alignment.bottomCenter,
child: BottomMenu(),
),
],
)
Does this help you?
Your layout isn't best practice, try:
return Scaffold(
body: LayoutBuilder(builder: (context, constraints) {
return SingleChildScrollView(
child: Container(
width: constraints.maxWidth,
height: constraints.maxHeight,
child: Column(
children: [
//your content
],
),
),
);
}));
This makes a container which is the size of the screen, and it contains a column which will scroll with all the content.
I could not understand what exactly the issue is. But I could give some idea.
Try something like this,
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
color: Colors.blue,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.9,
child: SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: ListView(
children: [
Row(
children: [
Container(
margin: const EdgeInsets.only(bottom: 10.0),
height: 100.0,
width: 100.0,
color: Colors.red,
),
],
),
....
],
),
),
),
),
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.1,
child: BottomMenu(),
),
],
),
);
}
Hope that solves your issue.

Scrollable page in flutter

I am new to flutter dart language and was wondering how to make the page scroll.
This is one of the pages of the code which I want to scroll:
class Second extends StatelessWidget {
#override
Widget build(BuildContext context) {
return DraggableScrollableSheet(
builder: (context, scrollController) {
return SingleChildScrollView(
controller: scrollController,
child: Scaffold(
backgroundColor: const Color(0xffffffff),
body: Stack(
children: <Widget>[
Transform.translate(
offset: Offset(30.0, 296.0),
child:
Container(
width: 315.0,
height: 287.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(37.0),
image: DecorationImage(
image: const AssetImage('assets/images/right.png'),
fit: BoxFit.fill,
)
),
),
),
],
),
),
);
}
);
}
}
Note that this is not the full code as the code is very long.
I tried using the Draggable Scrollable Sheet and when I ran the code I could only see a black screen.
Can someone help me?
Thanks in advance :)
Easiest way to make something scroll in Flutter is to use ListView widget.
Try something like this:
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Column(
children: [
Container(
height: 2000,
child: Text('This container exceeds most screens'),
),
Text('End of container.'),
],
),
],
),
);
You can use SingleChildScrollview or Listview for scrolling,let me know if it works for you
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffffffff),
appBar: AppBar(
title: Text(""),
),
body: SingleChildScrollView(
child: Column(
children: [
Stack(
children: <Widget>[
Transform.translate(
offset: Offset(30.0, 296.0),
child: Container(
width: 315.0,
height: 287.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(37.0),
image: DecorationImage(
image: const
AssetImage('assets/images/right.png'),
fit: BoxFit.fill,
)),
)),
],
),
],
),
));
}