Search code examples
flutterlistviewtextalignmentflutter-layout

How to align the text within ListTile?


Is there any other way for me to align the text within the leading and title of a ListTile without using the Row widget? The data was fetched from Firebase and then being viewed using Listview. Builder.

FutureBuilder(
  future: fetchData(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      List<taskData> result = snapshot.data as List<taskData>;

      return ListView.builder(
          scrollDirection: Axis.vertical,
          itemCount: result.length,
          itemBuilder: (BuildContext context, int i) {
            DateTime updateDate = DateTime.fromMillisecondsSinceEpoch(result[i].date);
            String formDate = DateFormat('yMMMd').format(updateDate);

            return GestureDetector(
              onTap: () {
                NavigationController(context, 'Edit', 'Title',
                    result[i].task, result[i].id, updateDate);
              },
              child: Card(
                child: ListTile(
                  leading: Text(formDate, style: TextStyle(fontFamily: 'ProximaNova', fontSize: 16),),
                  title: Text('${result[i].task}',style: TextStyle(fontFamily: 'ProximaNova',fontSize: 18),),
                  trailing: IconButton(
                    icon: Icon(Icons.delete),
                    onPressed: () {
                      id = result[i].id;
                      showDialog<void>(
                          context: context, builder: (context) => dialog);
                    },
                  ),
                ),
              ),
            );
          });
    } else if (snapshot.hasError) {
      print('${snapshot.error}');
      return Text(
        "",
        style: TextStyle(fontSize: 20),
      );
    }

    // By default, show a loading spinner.
    return Center(
        child: Container(
      margin: EdgeInsets.symmetric(vertical: 100.0),
      child: CircularProgressIndicator(),
    ));
  },
),

Here is the picture for reference ('title' is not aligned with 'leading' and 'trailing'): 'title' is not aligned with 'leading' and 'trailing'


Solution

  • It is working for you

    ListTile(
           leading: Text('2021-04-27'),
           title: Text('Lion', textAlign: TextAlign.center,),
           trailing: IconButton(
                    icon: Icon(Icons.delete),
                    onPressed: () {},                  
          ),
        ),