如何在flutter应用程序的PopupMenuItem的开头添加图标

问题描述 投票:0回答:1

我有一个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个回答
0
投票

您可以像这样用Row来完成它:

PopupMenuItem<WhyFarther>(
                    value: WhyFarther.harder,
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.work),
                        Text('Working a lot harder'),
                      ],
                    ),
                  ),
© www.soinside.com 2019 - 2024. All rights reserved.