在ListTile中放置两个尾随图标

时间:2019-02-06 07:51:25

标签: flutter flutter-layout

我想在ListTile的“尾随”侧并排放置两个图标。我尝试添加带有两个图标的Row小部件,但是它完全弄乱了整个ListTile的布局,使其无法使用。有什么方法可以扩展为尾部分配的空间?

代码如下:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: ListView(
      children: <Widget>[
        ListTile(
          leading: Icon(Icons.play_arrow,),
          title: Text("This is a title"),
          subtitle: Text("This is subtitle"),
          trailing: Row(          
            children: <Widget>[
            Icon(Icons.flight),
            Icon(Icons.flight_land)
          ]),
        )
      ]
    ),
      ),
    );
  }
}

它是这样的:

4 个答案:

答案 0 :(得分:34)

您只需在Wrap中使用trailing

ListTile(
  title: Text("This is my ListTile"),
  trailing: Wrap(
    spacing: 12, // space between two icons
    children: <Widget>[
      Icon(Icons.call), // icon-1
      Icon(Icons.message), // icon-2
    ],
  ),
)

enter image description here

答案 1 :(得分:7)

尝试此代码。我认为这是正确的方法。

trailing: FittedBox(
          fit: BoxFit.fill,
          child: Row(
          children: <Widget>[
            Icon(Icons.flight),
            Icon(Icons.flight_land),
          ],
          ),
        ),

答案 2 :(得分:5)

mainAxisSize: MainAxisSize.min添加到Row()实例可解决此问题。

答案 3 :(得分:0)

我利用了上面留下的 FittedBox 解决方案,并通过在屏幕处于横向和纵向模式时显示一个 TextButton 和一个 IconButton 来解决我的问题

trailing: MediaQuery.of(context).size.width > 480
                      ? FittedBox(
                          fit: BoxFit.fill,
                          child: Row(
                            children: <Widget>[
                              TextButton(
                                style: TextButton.styleFrom(
                                  // padding: const EdgeInsets.all(16.0),
                                  primary: Theme.of(context).errorColor,
                                  textStyle: const TextStyle(
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold),
                                ),
                                onPressed: () => onRemove(tr.id),
                                child: const Text('Excluir'),
                              ),
                              IconButton(
                                icon: Icon(Icons.delete),
                                color: Theme.of(context).errorColor,
                                onPressed: () => onRemove(tr.id),
                              ),
                            ],
                          ),
                        )
                      : IconButton(
                          icon: Icon(Icons.delete),
                          color: Theme.of(context).errorColor,
                          onPressed: () => onRemove(tr.id),
                        ),
相关问题