Manipulating child image from container within background image - flutter

I am trying to manipulate the size and position of an image which are over a background image. I have tried to set the height and width of a child image but it does not shrink.
What I want is to shrink the size of a child image("Neighbor") and give a position to 1/4 of the full screen from the top.
Any suggestions of what I can do?
Here is how my current app looks like:
Here is the code:

Are you looking for something like this?
class HomeView extends StatefulWidget {
const HomeView({Key? key}): super(key: key);
#override
State<StatefulWidget> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
constraints: const BoxConstraints.expand(),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/black-rocks.jpeg',
),
fit: BoxFit.fill,
),
),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Image.asset(
'assets/images/neighbor-name-and-logo.png',
width: MediaQuery.of(context).size.height / 4,
),
),
),
);
}
}

you can use Fractionaloffset or FractionallySizedBox
...
Container(
color: Colors.blue[200],
alignment: FractionalOffset(0.7, 0.6),
child: FractionallySizedBox(
widthFactor: 0.1,
heightFactor: 1/3,
child: Container(color: Colors.red[900])
),
),
...
Or
you can use https://velocityx.dev/docs/padding Plugin and give the top padding in percentage or
context.screenHeight*0.4

Related

How to slow down scroll velocity in flutter

I am trying to implement a slower scroll velocity in carousel_slider widget, or some better snapping. the problem is that if the user does a very fast swipe motion a couple of items will be scrolled instead of snapping tot he next one. i want to eliminate that behavior.
I'm not sure if it helps but I'm adding the code bellow:
// Automatic FlutterFlow imports
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
class ModelCarousel extends StatefulWidget {
// final List<String> iconUrlList;
final double modelSize;
final Function(int index)? onItemTapped;
final int selectedIndex;
const ModelCarousel(
{Key? key,
// required this.iconUrlList,
required this.modelSize,
required this.selectedIndex,
this.onItemTapped})
: super(key: key);
#override
_ModelCarouselState createState() => _ModelCarouselState();
}
class _ModelCarouselState extends State<ModelCarousel> {
ScrollPhysics _scrollPhysics = const PageScrollPhysics();
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.red),
height: 350,
child: CarouselSlider.builder(
itemCount: 5,
options: CarouselOptions(
height: 400.0,
pageSnapping: true,
scrollPhysics: _scrollPhysics,
),
itemBuilder: (BuildContext context, int index, int pageViewIndex) {
return Container(
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5.0),
decoration: BoxDecoration(
color: Colors.transparent,
image: DecorationImage(
image: AssetImage("assets/temp_images/model.png"),
fit: BoxFit.cover,
),
),
child: Text(
'text $index',
style: TextStyle(fontSize: 16.0),
));
})
);
}
Widget modelTile() {
return Padding(
padding: const EdgeInsets.fromLTRB(100, 0, 100, 0),
child: Container(
width: 200,
decoration: BoxDecoration(
color: Colors.transparent,
image: DecorationImage(
image: AssetImage("assets/temp_images/model.png"),
fit: BoxFit.cover,
),
),
),
);
}
}
i found a solution: I have changed the viewportFraction property to 0.98 for CarouselOptions class. this didn't change the speed but did have the desired effect, user now cant swipe more than one item.

Show half of rotating image in Flutter

I am creating an animation where a circle is rotating (left image).
My goals is only having half of the animation visible (blue rectangle in right image). In web development I would have created a div with a hidden overflow. I can't get this to work in Flutter. I've looked into ClipRect without luck.
This is the Flutter code I am using to rotate the image:
class ImageRotate extends StatefulWidget {
const ImageRotate({Key? key}) : super(key: key);
#override
_ImageRotateState createState() => _ImageRotateState();
}
class _ImageRotateState extends State<ImageRotate>
with SingleTickerProviderStateMixin {
late AnimationController animationController;
#override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: const Duration(seconds: 30),
);
animationController.repeat();
}
#override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
// color: Colors.white,
child: AnimatedBuilder(
animation: animationController,
child: SizedBox(
child: Image.asset('assets/svg/circle.png'),
),
builder: (BuildContext context, Widget? _widget) {
return Transform.rotate(
angle: animationController.value * 6.3,
child: _widget,
);
},
),
);
}
}
You could try using a Stack that clips its contents using its clipBehavior: Clip.hardEdge option, then you shift it half way its height. You wrap the Stack in a SizedBox to constrain its height to half the height of the circle, and apply a height to the ImageRotate also, as such:
Center(
child: SizedBox(
height: 200,
width: 400,
child: Stack(
clipBehavior: Clip.hardEdge,
children: [
Positioned(
top: 0,
child: ImageRotate()
)
]
)
),
)
To your ImageRotate:
SizedBox(
child: Image.asset('assets/svg/circle.png', width: 400, height: 400, fit: BoxFit.contain)
),
Check out this Gist and run it through DartPad.dev to check it out. Your output should look like this:
Here's an idea,
Put your circle and a rectangular opaque box(possibly container) in a Stack Widget in this order.
Give the container half of your rectangular box's size and color that can hide the circle.
Wrap the container in Positioned Widget and place it aligning to the bottom half of the blue rectangle.
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
height: 400,
width: 400,
alignment: Alignment.center,
child: Stack(children: [
AnimatedBuilder(
animation: animationController,
child: ClipOval(
child: Image.asset(
'assets/images/download.png',
width: 400,
height: 400,
fit: BoxFit.cover,
),
),
builder: (BuildContext context, Widget? _widget) {
return Transform.rotate(
angle: animationController.value * 6.3,
child: _widget,
);
},
),
Positioned(
bottom: 0,
right: 0,
child: Container(
width: 400,
height: 200,
color: Colors.white,
),
)
]),
),
),
);
}
In this code:
I used ClipOval for circular image cause my image was square.
Parent container has height and width, as well as child positioned container(half of the parent height).
And SafeArea for avoiding device margins.
Is this what you're looking for?

Custom widget, blur 2 widgets with text on front

I made my own widget with blur, bottom widget is looking correct, but top isn't. On top widget, text is behind blur, but why?
I need same result like second widget. (Text front of blur)
Second widget is looking correct.
Please look screenshot at first.
How to fix it? Thanks for any help.
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// incorrect
MyCard(
imageLink:
'https://catherineasquithgallery.com/uploads/posts/2021-02/1612198837_120-p-fioletovii-fon-mainkraft-160.png',
text: 'AR-scene',
),
SizedBox(
height: 70,
),
//correct
MyCard(
imageLink:
'https://www.digiseller.ru/preview/1019450/p1_3193057_f7ad4eea.jpg',
text: 'Photos',
),
],
),
);
}
}
// my custom widget
class MyCard extends StatelessWidget {
final imageLink;
final text;
const MyCard({Key? key, required this.imageLink, required this.text})
: super(key: key);
#override
Widget build(BuildContext context) {
return Container(
width: 270,
height: 320,
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 4, sigmaY: 3),
child: Center(
child: Text(
text,
style: TextStyle(fontSize: 25, color: Colors.white),
textAlign: TextAlign.center,
),
)),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
image: DecorationImage(
fit: BoxFit.cover, image: NetworkImage(imageLink))),
);
}
}
Wrap your BackdropFilter with ClipRect, else it covers the covering the full screen.
return Container(
key: ValueKey(text),
width: 270,
height: 320,
child: ClipRect( //<- here
child: BackdropFilter(
More on BackdropFilter-class
Using backdrop filter applies that particular filter to the whole screen. You can use ClipRRect to make it adopt the size of child widget (Container in this case).
// my custom widget
class MyCard extends StatelessWidget {
final imageLink;
final text;
const MyCard({Key? key, required this.imageLink, required this.text})
: super(key: key);
#override
Widget build(BuildContext context) {
return ClipRRect(
child: Container(
width: 270,
height: 320,
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 4, sigmaY: 3),
child: Center(
child: Text(
text,
style: TextStyle(fontSize: 25, color: Colors.white),
textAlign: TextAlign.center,
),
)),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
image: DecorationImage(
fit: BoxFit.cover, image: NetworkImage(imageLink))),
),
);
}
}
A better solution is to use ImageFiltered instead of BackdropFilter widget.
ImageFiltered blurs its child, for example, a single picture.
BackdropFilter blurs everything "behind" it, but does not blur its own child. It's useful in situations like a pop-up dialog, where you want to blur the whole screen, except the dialog itself.

Image not covering full device dimensions in flutter

I am trying to add an image covering the device width and height, also this image needs have text over it. At first when adding the image I used fit: BoxFit.fill on the Image.dart file so that the image covers the whole screen and it worked. Then for having the text over the image I added Stack widget wrapping the image and the text, as soon as I did this, the the text was over the image but now the image was not covering the full screen height. Why is this problem happening?
// main.dart
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: ImageContainer(),
);
}
}
// ImageContainer.dart
class ImageContainer extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(
children: [
MainImage("assets/images/startwars.jpg"),
Positioned(
bottom: 0,
left: 0,
child: Container(
child: Text(
"someText",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white),
)
),
)
],
)
],
),
);
}
}
// Image.dart
class MainImage extends StatelessWidget {
final String _assetPath;
MainImage(this._assetPath);
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.red,),
child: Expanded(
child: Image.asset(
_assetPath,
fit: BoxFit.fill,
),
),
);
}
}
If you have any questions please let me know in the comments;)
Set your Stack fit to expand:
Stack(
fit: StackFit.expand,
children: [],
);
I fixed this problem by adding constraints to the container in the Image.dart file, all I did was adding this line. After lots and lots of investigation I realized that adding MediaQuery to the height tells the app to cover the screen's height, this can be also done with width.
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.red,),
constraints: BoxConstraints.expand(height: MediaQuery.of(context).size.height), // add this line
child: Expanded(
child: Image.asset(
_assetPath,
fit: BoxFit.fill,
),
),
);
}
You can set Stack fit ..
Stack(
fit: StackFit.expand, ... )
An also you can add Container ( child:Image( ... ), width: double.infinity, heigh:...)

Flutter web ImageFilter.blur effect is not resizing when changing window size in browser

So I tried to add blur effect to my website, but when testing the blur doesn't resize. On large screen it works, but if starting with small screen and then going to bigger it doesn't. Am I doing something wrong here?
Image after resizing from small screen:
import 'dart:ui';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage(
'assets/background2.jpg',
),
fit: BoxFit.cover,
),
),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 2,
sigmaY: 2,
),
child: Container(
decoration: BoxDecoration(color: Colors.white.withOpacity(0.0)),
),
),
);
}
}
I've come across the same issue, which also occurs when transitioning to a new screen using the Navigator. While I don't know why this occurs, I have found a workaround. Initially I tried significantly increasing the size of the area being blurred in the hope that when you resize the window there's enough "blurred image" to expand into. However, while doing so I noticed that setting the size of the containing widget anything above 100% (e.g. 101%) simply solves this problem altogether.
Stack(
children: [
Positioned(
width: screenSize.width * 1.01,
height: screenSize.height * 1.01,
child: Container(
width: screenSize.width,
height: screenSize.height,decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("your image url"),
fit: BoxFit.cover,
),
),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 80.0, sigmaY: 80.0),
child: Container(
width: screenSize.width,
height: screenSize.height,decoration: BoxDecoration(color: Colors.white.withOpacity(0.0)),
),
),
),
),
Container(child: Text("Non blurry widgets go here")),
],
),