Center text in a row (Flutter)

0

I'm trying to center my text in a row but I want to keep the image on the left side as well. I would like to put the title and the description in the same line at the top centre of the right hand side. I've already tried several solutions but either it doesn't matter or the images move. Everything is implemented in a ListView.

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';


class ListViewExample extends StatefulWidget {
  @override 
  State<StatefulWidget> createState() {
    return new ListViewExampleState(
    );
  }
}

class ListViewExampleState extends State<ListViewExample>{
  List<Container> _buildListItemsFromItems(){
    int index = 0;
    return item.map((item){

      var container = Container(
        decoration: index % 2 == 0?
        new BoxDecoration(color: const Color(0xFFFFFFFF)):
          new BoxDecoration(
            color: const Color(0xFFFAFAF5)
          ),
        child: new Row(
          children: <Widget>[
            new Container(
              margin: new EdgeInsets.all(5.0),
              child: new CachedNetworkImage(
                imageUrl: item.imageURL,
                width: 200.0,
                height: 100.0,
                fit: BoxFit.cover,
              ),
            ),

            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
  
                new Text(
                    item.title,
                    style: new TextStyle(),
                  ),
                new Text(
                    item.description,
                  ),

                ]
            )
            

          ],
        )
      );

      index = index + 1;
      return container;
    }).toList();
  }
  @override
  Widget build(BuildContext context) {
    return new ListView(
      children: _buildListItemsFromItems(),
    );
  }
}

Thank you in advance for your help!

Update : I have added the Expanded widget.

flutter
dart
row
center
asked on Stack Overflow Oct 10, 2020 by Rianou • edited Nov 25, 2020 by Rianou

1 Answer

0

You can Wrap your second Row with Expanded widget like code below:

Expanded(
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        item.title,
                        style: TextStyle(),
                      ),
                      Text(
                        item.description,
                        style: TextStyle(),
                      ),
                    ]),
              )
answered on Stack Overflow Oct 10, 2020 by Ali Mohammadzadeh

User contributions licensed under CC BY-SA 3.0