Align center of an image to the bottom of another image in flutter
Currently I'm facing a problem for creating responsive design in flutter so it could work fine with the same look and feel to all screen sizes
Currently I need to create like the image below, I need the center of an image (the red one in the image below) to be aligned to the bottom of another one (the blue big image) , sometimes the red one is centered but in different screen sizes its either raised a little bit to the top or to the bottom.
This is my attempt:
class ImageAssetUtils
static Image drawImage(String imagePath, double requiredWidth, double requiredHeight)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.8 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.21 : screenFactor;
requiredWidth = requiredWidth * screenFactor;
requiredHeight = requiredHeight * screenFactor;
return new Image.asset(imagePath, width: requiredWidth, height: requiredHeight);
class StyleUtils
static EdgeInsets givePadding(EdgeInsets absoluteEdges)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.75 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.14 : screenFactor;
double left = absoluteEdges.left * screenFactor;
double right = absoluteEdges.right * screenFactor;
double top = absoluteEdges.top * screenFactor;
double bottom = absoluteEdges.bottom * screenFactor;
return EdgeInsets.only(left: left, right: right, top: top, bottom: bottom);
class Test extends StatefulWidget
@override
_TestState createState() => _TestState();
class _TestState extends State<Test>
@override
Widget build(BuildContext context)
return new Scaffold(
backgroundColor: Color.fromRGBO(235, 235, 235, 1.0),
body: new Stack(children: <Widget>[
new Image.asset('some Image.png',
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.33
),
ListView(children: <Widget>[
new Padding(
padding: StyleUtils.givePadding(EdgeInsets.only(top: 16.0)),
child: new Center(
child: new Container(
child: ImageAssetUtils.drawImage("my image.png", 100.0, 100.0),
),
)
)
]),
])
);
If any help is it will be much appreciated, thanks in advance.
responsive-design flutter
add a comment |
Currently I'm facing a problem for creating responsive design in flutter so it could work fine with the same look and feel to all screen sizes
Currently I need to create like the image below, I need the center of an image (the red one in the image below) to be aligned to the bottom of another one (the blue big image) , sometimes the red one is centered but in different screen sizes its either raised a little bit to the top or to the bottom.
This is my attempt:
class ImageAssetUtils
static Image drawImage(String imagePath, double requiredWidth, double requiredHeight)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.8 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.21 : screenFactor;
requiredWidth = requiredWidth * screenFactor;
requiredHeight = requiredHeight * screenFactor;
return new Image.asset(imagePath, width: requiredWidth, height: requiredHeight);
class StyleUtils
static EdgeInsets givePadding(EdgeInsets absoluteEdges)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.75 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.14 : screenFactor;
double left = absoluteEdges.left * screenFactor;
double right = absoluteEdges.right * screenFactor;
double top = absoluteEdges.top * screenFactor;
double bottom = absoluteEdges.bottom * screenFactor;
return EdgeInsets.only(left: left, right: right, top: top, bottom: bottom);
class Test extends StatefulWidget
@override
_TestState createState() => _TestState();
class _TestState extends State<Test>
@override
Widget build(BuildContext context)
return new Scaffold(
backgroundColor: Color.fromRGBO(235, 235, 235, 1.0),
body: new Stack(children: <Widget>[
new Image.asset('some Image.png',
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.33
),
ListView(children: <Widget>[
new Padding(
padding: StyleUtils.givePadding(EdgeInsets.only(top: 16.0)),
child: new Center(
child: new Container(
child: ImageAssetUtils.drawImage("my image.png", 100.0, 100.0),
),
)
)
]),
])
);
If any help is it will be much appreciated, thanks in advance.
responsive-design flutter
add a comment |
Currently I'm facing a problem for creating responsive design in flutter so it could work fine with the same look and feel to all screen sizes
Currently I need to create like the image below, I need the center of an image (the red one in the image below) to be aligned to the bottom of another one (the blue big image) , sometimes the red one is centered but in different screen sizes its either raised a little bit to the top or to the bottom.
This is my attempt:
class ImageAssetUtils
static Image drawImage(String imagePath, double requiredWidth, double requiredHeight)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.8 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.21 : screenFactor;
requiredWidth = requiredWidth * screenFactor;
requiredHeight = requiredHeight * screenFactor;
return new Image.asset(imagePath, width: requiredWidth, height: requiredHeight);
class StyleUtils
static EdgeInsets givePadding(EdgeInsets absoluteEdges)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.75 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.14 : screenFactor;
double left = absoluteEdges.left * screenFactor;
double right = absoluteEdges.right * screenFactor;
double top = absoluteEdges.top * screenFactor;
double bottom = absoluteEdges.bottom * screenFactor;
return EdgeInsets.only(left: left, right: right, top: top, bottom: bottom);
class Test extends StatefulWidget
@override
_TestState createState() => _TestState();
class _TestState extends State<Test>
@override
Widget build(BuildContext context)
return new Scaffold(
backgroundColor: Color.fromRGBO(235, 235, 235, 1.0),
body: new Stack(children: <Widget>[
new Image.asset('some Image.png',
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.33
),
ListView(children: <Widget>[
new Padding(
padding: StyleUtils.givePadding(EdgeInsets.only(top: 16.0)),
child: new Center(
child: new Container(
child: ImageAssetUtils.drawImage("my image.png", 100.0, 100.0),
),
)
)
]),
])
);
If any help is it will be much appreciated, thanks in advance.
responsive-design flutter
Currently I'm facing a problem for creating responsive design in flutter so it could work fine with the same look and feel to all screen sizes
Currently I need to create like the image below, I need the center of an image (the red one in the image below) to be aligned to the bottom of another one (the blue big image) , sometimes the red one is centered but in different screen sizes its either raised a little bit to the top or to the bottom.
This is my attempt:
class ImageAssetUtils
static Image drawImage(String imagePath, double requiredWidth, double requiredHeight)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.8 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.21 : screenFactor;
requiredWidth = requiredWidth * screenFactor;
requiredHeight = requiredHeight * screenFactor;
return new Image.asset(imagePath, width: requiredWidth, height: requiredHeight);
class StyleUtils
static EdgeInsets givePadding(EdgeInsets absoluteEdges)
double screenFactor = 1.0;
screenFactor = ScreenSize.isSmallScreenSize(myApp.navigatorState.currentContext) ? 0.75 : screenFactor;
screenFactor = ScreenSize.isLargeScreenSize(myApp.navigatorState.currentContext) ? 1.14 : screenFactor;
double left = absoluteEdges.left * screenFactor;
double right = absoluteEdges.right * screenFactor;
double top = absoluteEdges.top * screenFactor;
double bottom = absoluteEdges.bottom * screenFactor;
return EdgeInsets.only(left: left, right: right, top: top, bottom: bottom);
class Test extends StatefulWidget
@override
_TestState createState() => _TestState();
class _TestState extends State<Test>
@override
Widget build(BuildContext context)
return new Scaffold(
backgroundColor: Color.fromRGBO(235, 235, 235, 1.0),
body: new Stack(children: <Widget>[
new Image.asset('some Image.png',
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.33
),
ListView(children: <Widget>[
new Padding(
padding: StyleUtils.givePadding(EdgeInsets.only(top: 16.0)),
child: new Center(
child: new Container(
child: ImageAssetUtils.drawImage("my image.png", 100.0, 100.0),
),
)
)
]),
])
);
If any help is it will be much appreciated, thanks in advance.
responsive-design flutter
responsive-design flutter
edited Nov 14 '18 at 10:15
derHugo
6,56431231
6,56431231
asked Oct 24 '18 at 11:03
Hesham Ali KamalHesham Ali Kamal
677
677
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The FractionalTranslation widget is used to manipulate the position of a child widget. You'll also have to pass an Offset to it, which will define the position manipulation. The child widget would be the red rectangle and the Offset
would have the values x: 0.0
and y: 0.5
. This would place the red rectangle lower, by the half of its height.
Now you can lay the red rectangle above the blue one. To do this, you can use the Stack widget. You'll have to set alignment: Alignment.bottomCenter
, such that the red rectangle is placed at the lower edge in the center.
You can find a code example below. The blue rectangle has a third of the size of the screen. The red rectangle is half as large as the blue one.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(theme: ThemeData(), home: Home());
class Home extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: BlueRedRects(
big: MediaQuery.of(context).size.width / 3.0,
small: MediaQuery.of(context).size.width / 6.0,
),
),
);
class BlueRedRects extends StatelessWidget
final double big;
final double small;
BlueRedRects(this.big, this.small);
@override
Widget build(BuildContext context)
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(color: Colors.blue, width: big, height: big),
FractionalTranslation(
translation: Offset(0.0, 0.5),
child: Container(
color: Colors.red,
width: small,
height: small,
),
)
],
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52967341%2falign-center-of-an-image-to-the-bottom-of-another-image-in-flutter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The FractionalTranslation widget is used to manipulate the position of a child widget. You'll also have to pass an Offset to it, which will define the position manipulation. The child widget would be the red rectangle and the Offset
would have the values x: 0.0
and y: 0.5
. This would place the red rectangle lower, by the half of its height.
Now you can lay the red rectangle above the blue one. To do this, you can use the Stack widget. You'll have to set alignment: Alignment.bottomCenter
, such that the red rectangle is placed at the lower edge in the center.
You can find a code example below. The blue rectangle has a third of the size of the screen. The red rectangle is half as large as the blue one.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(theme: ThemeData(), home: Home());
class Home extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: BlueRedRects(
big: MediaQuery.of(context).size.width / 3.0,
small: MediaQuery.of(context).size.width / 6.0,
),
),
);
class BlueRedRects extends StatelessWidget
final double big;
final double small;
BlueRedRects(this.big, this.small);
@override
Widget build(BuildContext context)
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(color: Colors.blue, width: big, height: big),
FractionalTranslation(
translation: Offset(0.0, 0.5),
child: Container(
color: Colors.red,
width: small,
height: small,
),
)
],
);
add a comment |
The FractionalTranslation widget is used to manipulate the position of a child widget. You'll also have to pass an Offset to it, which will define the position manipulation. The child widget would be the red rectangle and the Offset
would have the values x: 0.0
and y: 0.5
. This would place the red rectangle lower, by the half of its height.
Now you can lay the red rectangle above the blue one. To do this, you can use the Stack widget. You'll have to set alignment: Alignment.bottomCenter
, such that the red rectangle is placed at the lower edge in the center.
You can find a code example below. The blue rectangle has a third of the size of the screen. The red rectangle is half as large as the blue one.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(theme: ThemeData(), home: Home());
class Home extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: BlueRedRects(
big: MediaQuery.of(context).size.width / 3.0,
small: MediaQuery.of(context).size.width / 6.0,
),
),
);
class BlueRedRects extends StatelessWidget
final double big;
final double small;
BlueRedRects(this.big, this.small);
@override
Widget build(BuildContext context)
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(color: Colors.blue, width: big, height: big),
FractionalTranslation(
translation: Offset(0.0, 0.5),
child: Container(
color: Colors.red,
width: small,
height: small,
),
)
],
);
add a comment |
The FractionalTranslation widget is used to manipulate the position of a child widget. You'll also have to pass an Offset to it, which will define the position manipulation. The child widget would be the red rectangle and the Offset
would have the values x: 0.0
and y: 0.5
. This would place the red rectangle lower, by the half of its height.
Now you can lay the red rectangle above the blue one. To do this, you can use the Stack widget. You'll have to set alignment: Alignment.bottomCenter
, such that the red rectangle is placed at the lower edge in the center.
You can find a code example below. The blue rectangle has a third of the size of the screen. The red rectangle is half as large as the blue one.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(theme: ThemeData(), home: Home());
class Home extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: BlueRedRects(
big: MediaQuery.of(context).size.width / 3.0,
small: MediaQuery.of(context).size.width / 6.0,
),
),
);
class BlueRedRects extends StatelessWidget
final double big;
final double small;
BlueRedRects(this.big, this.small);
@override
Widget build(BuildContext context)
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(color: Colors.blue, width: big, height: big),
FractionalTranslation(
translation: Offset(0.0, 0.5),
child: Container(
color: Colors.red,
width: small,
height: small,
),
)
],
);
The FractionalTranslation widget is used to manipulate the position of a child widget. You'll also have to pass an Offset to it, which will define the position manipulation. The child widget would be the red rectangle and the Offset
would have the values x: 0.0
and y: 0.5
. This would place the red rectangle lower, by the half of its height.
Now you can lay the red rectangle above the blue one. To do this, you can use the Stack widget. You'll have to set alignment: Alignment.bottomCenter
, such that the red rectangle is placed at the lower edge in the center.
You can find a code example below. The blue rectangle has a third of the size of the screen. The red rectangle is half as large as the blue one.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(theme: ThemeData(), home: Home());
class Home extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: BlueRedRects(
big: MediaQuery.of(context).size.width / 3.0,
small: MediaQuery.of(context).size.width / 6.0,
),
),
);
class BlueRedRects extends StatelessWidget
final double big;
final double small;
BlueRedRects(this.big, this.small);
@override
Widget build(BuildContext context)
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(color: Colors.blue, width: big, height: big),
FractionalTranslation(
translation: Offset(0.0, 0.5),
child: Container(
color: Colors.red,
width: small,
height: small,
),
)
],
);
answered Nov 13 '18 at 21:26
NiklasNiklas
49428
49428
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52967341%2falign-center-of-an-image-to-the-bottom-of-another-image-in-flutter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown