7

enter image description here

I am trying to set an image in the center of Column, and the Text at the bottom of the image, by wrapping Image and Text in the Column widget and placing it in the Center widget.

Unfortunately, it centers the Column and makes Image to be above the center of the screen.

How can I solve it?

My current code:

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Theme.of(context).primaryColor),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.asset(ImagePaths.newLogoLogin),
            Text(Strings.beALocal)
          ],
        ),
      ),
    );
  }
4
  • 1
    Add padding or margin on top as high as the text below? Commented Jan 21, 2019 at 12:37
  • Thanks, but it sounds a bit like a workaround. I am pretty sure it should be more generic way, that can support multiple screens. Commented Jan 21, 2019 at 12:43
  • In which widget your currently putting your above-mentioned code? Scaffold or SafeArea or you are passing directly to runApp() ? Commented Jan 21, 2019 at 13:12
  • @dhuma1981 I return this widget as home in MaterialApp Commented Jan 21, 2019 at 13:23

3 Answers 3

7

This can be achieved using the Expanded widget:

@override
Widget build(BuildContext context) {
  return Container(
    decoration: BoxDecoration(color: Theme.of(context).primaryColor),
    child: Column(
      children: [
        Spacer(),
        Image.asset(ImagePaths.newLogoLogin),
        Expanded(
          Column(
             children: [ Text(Strings.beALocal) ],
             mainAxisAlignment: MainAxisAlignment.start
          )
        )
      ],
    ),
  );
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could use a Stack with a Positioned Text widget inside it.

Full example:

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Test")),
        body: Stack(children: [Placeholder(), Test()]),
      ),
    );
  }
}

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        alignment: AlignmentDirectional.bottomCenter,
        overflow: Overflow.visible,
        children: <Widget>[
          Container(
            width: 150,
            height: 150,
            color: Colors.blue,
          ),
          Positioned(child: Text("Some text"), bottom: -25),
        ],
      ),
    );
  }
}

Screenshot

1 Comment

Thank you, but unfortunately, if let's say the text size will be raised we need to raise the bottom parameter as well. I am looking for a more adaptive and not hardcoded way. Similar to ConstraintLayout in Android or AutoLayout in iOS.
0

you can use widget SafaArea or calc size of the appbar and the navigation bar, when you have this result use this for remove in height size of screen after this you can add column MainAxisAlignment.center, CrossAxisAlignment.center and add other widget in Column

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.