【问题标题】:How to add an Icon at the beginning of PopupMenuItem in flutter app如何在 Flutter 应用中的 PopupMenuItem 开头添加一个图标
【发布时间】:2020-01-05 10:06:28
【问题描述】:

我有一个PopupMenuButton 小部件,我想在每个PopupMenuItem 的开头添加一个图标。我一直在尝试找到一种方法来做到这一点,但我没有找到任何方法。

Below is the **main.dart** file.
import 'package:flutter/material.dart';
import 'package:practical_0/homepage.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue
      ),
      home: Homepage(),
    );
  }
}

下面是 home.dart 文件。这是我实现 PopupMenuButton 的地方。我希望图标出现在PopupMenuItem 的开头text 之前。

import 'package:flutter/material.dart';

enum WhyFarther { harder, smarter, selfStarter, tradingCharter }

class Homepage extends StatelessWidget {

  final double heightFactor = 600/896;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        return new Card(
          new Row(
            children: <Widget>[
              PopupMenuButton<WhyFarther>(
               onSelected: (WhyFarther result) { setState(() { _selection = result; }); },
               itemBuilder: (BuildContext context) => <PopupMenuEntry<WhyFarther>>[
                const PopupMenuItem<WhyFarther>(
                  value: WhyFarther.harder,
                  child: Text('Working a lot harder'),
                ),
                const PopupMenuItem<WhyFarther>(
                  value: WhyFarther.smarter,
                  child: Text('Being a lot smarter'),
                ),
                const PopupMenuItem<WhyFarther>(
                  value: WhyFarther.selfStarter,
                  child: Text('Being a self-starter'),
                 ),
                 const PopupMenuItem<WhyFarther>(
                   value: WhyFarther.tradingCharter,
                   child: Text('Placed in charge of trading charter'),
                 ),
                ],
              ),
            ],
          )
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    我实际上建议使用这样的 ListTile:

                  PopupMenuItem<WhyFarther>(
                    value: WhyFarther.harder,
                    child: ListTile(
                      leading: Icon(Icons.work),
                      title: Text('Working a lot harder'),
                    ),
                  )
    

    查看 Flutter Gallery 以获取实时示例:https://gallery.flutter.dev/#/demo/menu

    【讨论】:

      【解决方案2】:

      你可以用一行来做,像这样:

      PopupMenuItem<WhyFarther>(
                          value: WhyFarther.harder,
                          child: Row(
                            children: <Widget>[
                              Icon(Icons.work),
                              Text('Working a lot harder'),
                            ],
                          ),
                        ),
      

      【讨论】:

      • 这个解决方案,一开始似乎不起作用,但是当将图标颜色设置为非白色时 - 做到了Icon(Icons.home, size: 24, color: Colors.orange,),
      • @mike123 这是因为您的主题设置可能设置为白色作为原色和不同的背景。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2018-02-05
      相关资源
      最近更新 更多