I have a view like that
And I would like the image carousel to be without a horizontal margin like:
My image carousel is inside 3rd party widget so I need something like a negative margin?
I've tried transform on Container, but it only translates one side.
Container(
height: 160,
transform: Matrix4.translationValues(-20, 0, 0),
child: ListView(
scrollDirection: Axis.horizontal,
children: [
...
],
),
),
Why dont you set the width to double.infinity and if you want to specify a margin work with the following?
margin: const EdgeInsets.only(top: 8.0),
Related
This is my Container:
Container(
height: height - height * 0.4, //this is my preferred height.
width: width - width * 0.7,
decoration: decoration,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Image.asset(
'assets/opensource.png',
scale: 1.35,
),
text('Open Source Software', 20, Color(0xFF02bbe5)),
text('Contributor since December, 2020', 16,
Color(0xFF02bbe5)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: text(
'test',
16,
Colors.blueGrey),
),
],
),
),
),
I need to make the app responsive, for that the Container needs to be elongated when the inner widgets don't fit inside the Container properly.
How do I give a preferred height to this Container but also allow it to resize freely in case of overflow?
Note: This Container is inside a Column itself with SingleChildScrollView as the parent. And I am building a web app.
Edit: I have used MediaQuery for getting width and height.
There are multiple ways to approach this problem. You can wrap you container inside an AspectRatio widget and an Expanded.
AspectRatio(
aspectRatio: someAspectRatioValue,
child: Expanded(
child Container(...)
)
)
This should expand the items container without loosing shape of the container.
You can also build your whole screen with a LayoutBuilder, though that would require a lot of reworking.
If you want to make your application responsive in Flutter then you should Sizer package( Here ). This package will make your app responsive for any screen size, it divides into percentages. For ex:
Container(
width: 20.w, //It will take a 20% of screen width
height:30.h //It will take a 30% of screen height
)
I have this code
body: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
HomeHeader(),
Container(
padding: EdgeInsets.only(top: 5, bottom: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
TitleCard(title: "Popular Coffee Bean"),
SizedBox(height: 5),
PopularCoffeeBeans(), //widget Overflowed
SizedBox(height: 10),
TitleCard(title: "Implementation"),
SizedBox(height: 5),
],
),
),
],
),
);
I want to try auto set height that the column shrinks to fit the children. I dont know whats wrong in my code.
I have try to wrap my first Column with Flexible or Expanded Widget, but thats still not working.
any solve for this?
this is my output
The error is not in your column but in your PopularCoffeBeans() Widget. In that container, wherever you have defined the height increase it by 26 pixels. And as for the problem of running it in smaller screen size, I guess you are trying to define height using the screen height which is usually not a good practice. It is better to define a fixed width to avoid the override solution.
I have solve my problem. the problem come, becaue i wraping my horizontal scroll direction using ListView.builder. ListView.builder is bad thing for now to create a horizontal scroll direction, of course if in 1 page there are many scroll pages. ListView.builder only support shrinkWrap by main Axis direction, thats mean shrinkWrap will be fine for vertical scroll Direction, but not good for horizontal scroll Direction.
I try to change that ListView.builder to SingleChildScrollView with simple For loop to get index Data. And with SingleChildScrollView, you dont need to set height of parent widget because the height will follow the size of the children who are inside.
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Container(
padding: EdgeInsets.all(5),
child: Row(
children: [
for(int index = 0; index < data_variable.length; index++)
Container(
padding: EdgeInsets.only(right: 5),
child: Container(
width: MediaQuery.of(context).size.width * 0.5,
child: Center(
child: Text(data_variable[index]),
),
),
),
],
),
),
)
try giving more height to PopularCoffeeBean card your container seem to be overflowing from card container
I wanna make a Listview where every items has the 'width' of the screen size, scrolling horizontally between them.
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Container(
child: Image.asset("assets/images/tests/asset0.PNG"),
width: MediaQuery.of(context).size.width,
),
Container(
child: Image.asset("assets/images/tests/asset1.PNG"),
width: MediaQuery.of(context).size.width,
),
],
),
But (like if you swipe between many pictures horizontally on instagram) i want the children to be full on page (not half half or something if you start scrolling between them).
Any ideas?
Use PageView.builder instead ListView
I have an horizontal SingleChildScrollView widget with variable amount of different buttons. I want to display button with right arrow only when there is too much buttons to show all on screen. I want to avoid situation when someone could dont know about more buttons hidden behind right edge of the screen.
How to achieve this?
Container(
height: iconsBarHeight,
color: Theme.of(context).scaffoldBackgroundColor,
padding: const EdgeInsets.fromLTRB(16, 16, 8, 0),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Button2(),
Button4(),
Button1(),
Button6(),
Button7(),
Button3(),
Button2(),
],
),
),
);
you can add ScrollControlller to SingleChildScrollView, and get the max extent of it. If max extent is greater than width of screen, then show the button.
scrollController.position.maxScrollExtent
I use Expanded() to tell the Widgets in my Row to use the available space. I want to have 4px space between elements in my Row. I don't want to have the same space towards elements outside of the Row. Given that Flutter doesn't support negative Padding I therefore can't add Padding in a way that gives the first item no left Padding and the last item no right Padding.
Is there pattern to easily define spacing with a specific width? I want a solution that works with an arbitrary number of Widgets. For 4 Widgets I want something that produces the equivalent of:
Row(children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 0, 0),
child: Expanded(Text("Alice")))
]);
cant understand what you want to achieve exactly but try
Use SizedBox widget very easy and simple check it here
https://api.flutter.dev/flutter/widgets/SizedBox-class.html
example
SizedBox(
width: 200.0,
height: 300.0,
)
I usually use this:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Widget(),
Spacer(),
Widget(),
Widget(),
],
),
Spacer, a widget that takes up space proportional to it's flex
value. Flutter Row Class
mainAxisAlignment property : How the children should be placed along
the main axis. Flutter MainAxisAlignment enum
If you want to determine the size, you can use SizedBox(width: double) instead of Spacer ().
SizedBox: A box with a specified size. Flutter SizedBox class