Horizontally center a image slider in flutter - flutter

I have a image slider in flutter where I want to vertically center the carousel. I'm using carousel_slider. And this here is my code:
Widget _buildItems(BuildContext context) {
return Column(
children: <Widget>[
appBar(context),
Center(
child: CarouselSlider(
options: CarouselOptions(
enlargeCenterPage: true,
enableInfiniteScroll: false,
autoPlay: false,
),
items: widget.recipeStepsList!
.map(
(e) => ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
child: Text(
e.name!,
style: TextStyle(color: Colors.red),
),
),
Image.network(
e.stepThumbnailLink!,
width: 1000,
height: 200,
fit: BoxFit.cover,
),
],
),
),
)
.toList(),
),
),
],
);
}
As you can see, I've already wrapped everything inside a Center but this is the outcome of my code:
The only reason the back button is in place because it has a padding of 10. But since I'm building this application for all types of screens I need to keep the image slider exactly in the center.

The carousel is not coming in the center, since your Center widget is not actually taking up the full space and this is because it is inside a Column.
For widget like Row and Column, you can use Expanded widget to make any child take up the remaining space.
So, for your case, just wrap your Center inside an Expanded.
Here is the linkk to full working code.
Result,
Ignore the overflow warning, since that is happening because the carousel package is squishing view while scrolling.

Related

how to use TabBar view inside scrollable widget in flutter

I have this design, The first section in the red box is fixed height size, and The second section is the dynamic height (ListviewBuilder) which changed the content based on the tabBar.
My question is How can I use the TabBar view inside the scrollable widget (custom scroll view/listview etc..)
the solution That I currently found is to use a customScrollView and use SliverFillRemaining like that
SliverFillRemaining(
child: TabBarView(
children: [],
),
),
but that adds extra white space at the bottom of the list and I can't remove it by
making hasScrollBody property false
You could probably achieve what you want with this kind of template :
Scaffold(
body: Column(
children: [
Container(
height: MediaQuery.of(context).size.height * .33,
child: Container(/* Content of the first section */),
),
Expanded(
child: DefaultTabController(
length: 2,
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Container(
height: 50,
child: TabBar(
tabs: [Text("Guest"), Text("Service")],
),
),
),
SliverFillRemaining(
child: TabBarView(
children: [
// Your ListView Builder here
],
),
),
],
),
),
),
],
),
);
Seeing the bit of code you posted, it is probably close to what you already have but after test it doesn't adds extra white space at the bottom.
If it continue to display the same behavior, adding a little more context could help us provide a more accurate answer.

Flutter align two widgets in same position on stack

I have 2 widget in stack. One of them is aligned in center. And the widget which is on the top is other stack. And I want show second widget with first widget's top and left position. Here is the explain:
The yellow area is my stack. And first widget is setted like Center(child: myFirstWidget). My second widget is referenced from here it's a resizable widget so it's an another stack and it's childs are "positioned". So I need to set top and left value for initialize. But my main stack filled page So when I set my second widget's top and left to 0. It's shown as below.
But I want to show align it to centered child's top like:
My code snip:
child: Container(
color: Colors.red,
child: Stack(
children: [
Center(
child: Image.file(
File("myfilepath"),
),
),
ResizableWidget(
child: Container(
color: Colors.blue,
),
),
],
),
),
You can adjust the position of the widgets with the Position widget like this:
Positioned(
child: ResizableWidget(
child: Container(
color: Colors.blue,
),
),
top: 80,
right: -5,
),
The value of top and right are just random values, you should check what works for you!
You should make Center the parent of the Stack.
The Stack will be positioned at the center of its parent because of Center, it will get the dimensions of its biggest childs (height and width) and will position other childs according to the alignment value.
Edit : I modified the code to include a placeHolder image. Here I gave it a 6000x2000 size but you can change it to whatever value you want. Code is working as expected, see capture below.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Stack(
alignment: AlignmentDirectional.topStart,
children: [
Image.network('https://via.placeholder.com/6000x2000'),
SizedBox(
height: 50,
width: 50,
child: Container(
color: Colors.green,
))
],
),
),
);
}

how to make a widget to be on the bottom of the screen if using SliverStack?

as you can see at the image above, I have a red container stick on the top (right below the app bar). here is the code I use
CustomScrollView(
slivers: [
SliverAppBar(),
_buildStack(),
]
),
Widget _buildStack() {
return SliverStack(
children: [
// I have other widgets here ..
SliverPositioned(
bottom: 0, // <-- I have set the bottom to be zero
child: SliverToBoxAdapter(
child: _buildRedBox(),
),
],
)
}
Widget _buildRedBox() {
return Container(
width: double.infinity,
height: 50,
color: Colors.red,
);
}
I am using sliver tool package so I can access SliverStack and SliverPositioned.
as you can see, I have set the bottom to be zero. but the red box is still on the top. I need to make the red box at the bottom of the screen inside my SliverStack. how to do that ?
Use SliverFillRemaining widget instead of SliverPositioned widget.
SliverFillRemaining will automatically size itself to fill the space between the bottom of the last list item and the bottom of the viewport
SliverFillRemaining(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
_buildRedBox(),
],
),
),

Is it possible to make watermark with stack when the image is over widget and still can interact with the widgets behind?

So, I'm building apps with flutter and have some cases when watermark is needed for some page. I have solution for this problem using Stack Widget, but more problem comes. The problem are:
I need the watermark can cover all the screen.
I've try using Positioned with Image behind widgets, but the widgets that have background cover my watermark and cannot be seen well.
//Code to make watermark behind widgets
Stack (
children : [
Positioned(
child: Center(
child: Opacity(
opacity: 0.1,
child: Image.asset("path/to/images.png", fit: BoxFit.contain)
),
),
),
AllMyScreenWidgets(),
]
)
When Positioned widget with Image is over the widgets, you can see all watermark and that's good but you can't interact with the widgets behind.
//Code to make watermark over widgets
Stack (
children : [
AllMyScreenWidgets(),
Positioned(
child: Center(
child: Opacity(
opacity: 0.1,
child: Image.asset("path/to/images.png", fit: BoxFit.contain)
),
),
),
]
)
Is it possible to keep use Stack Widget? or there any advice, solution, trick or tips about this problem?
Thanks
try IgnorePointer:
//Code to make watermark over widgets
Stack (
children : [
AllMyScreenWidgets(),
Positioned(
child: IgnorePointer(ignoring: true,
child: Center(
child: Opacity(
opacity: 0.1,
child: Image.asset("path/to/images.png", fit: BoxFit.contain)
),
),
),
),
]
)

How to rotate a larger-than-screen image without the overflow being clipped off in Flutter?

I have this image that I would like to display full screen and rotate in the background:
Here it is filling the screen correctly:
The problem is, when it rotates, the sides have been clipped off:
I've tried every type of box fit. I've tried sizing the container width to double.infinity. I've tried wrapping the image in a SingleChildScrollView. I've tried putting overflow: Overflow.visible on the stack. All day trying things, but nothing seems to be working.
The image needs to continuously fill the screen while rotating. How can I code it so that the edges aren't clipped off?
Here's my code:
class FirstScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
children: <Widget>[
SpinPerfect(
infinite: true,
duration: Duration(seconds: 10),
child: Image.asset(
'assets/images/star-burst.png',
fit: BoxFit.none,
),
),
Container(
child: Center(
child: Text('This is Screen 1'),
),
),
],
),
),
);
}
}
Note: I am currently rotating it using SpinPerfect from the animate_do package, but the same clipping problem happens when using Transform.rotate.
Thanks in advance for any direction!
Here is a solution that works very well:
Use SpinPerfect from the animate_do package (https://pub.dev/packages/animate_do) in combination with the photo_view package (https://pub.dev/packages/photo_view).
SpinPerfect(
infinite: true,
spins: 1,
duration: Duration(seconds: 60),
child: PhotoView(
disableGestures: true,
backgroundDecoration: BoxDecoration(color: Colors.transparent),
initialScale: PhotoViewComputedScale.covered * 2.7,
imageProvider: AssetImage('assets/images/background.jpg'),
),
)
Thanks to the creator of the animate_do package for the idea. (Very cool dude!)