手势按钮内的弹出菜单按钮在 Flutter 中不起作用

时间:2021-04-27 21:19:04

标签: flutter dart

我正在 Gesture Button 中创建一个 PopUp 菜单,但单击该按钮时菜单没有显示。单击按钮时什么也没有发生。我应该怎么做才能使其在单击按钮时可见? 我也使用了 Focused Menu Button 但它也发生了同样的情况。我无法弄清楚问题所在。

我正在制作这样的屏幕:

enter image description here

我想在点击按钮时显示这些菜单:

enter image description here

我的屏幕代码是这样的:

    import 'package:epicare/NavigBar.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:focused_menu/focused_menu.dart';
import 'package:focused_menu/modals.dart';
import 'AddMedicineScreen.dart';

class MedicinesRecord extends StatefulWidget {
  @override
  _MedicinesRecordState createState() => _MedicinesRecordState();
}

class _MedicinesRecordState extends State<MedicinesRecord>
    with TickerProviderStateMixin {
  TabController _tabController;
  var _selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this)
      ..addListener(() {
        setState(() {
          _selectedIndex = _tabController.index;
        });
      });
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: const Color(0xffE5E0A1),
        elevation: 0,
        centerTitle: true,
        title: Text(
          "Medicine",
          style: TextStyle(
            fontSize: 15.0,
            color: Colors.black,
            fontFamily: 'Montserrat',
            fontWeight: FontWeight.normal,
          ),
        ),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.black,
          ),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return Homepage();
                },
              ),
            );
          },
        ),
      ),
      body: Column(
        children: [
          // give the tab bar a height [can change height to preferred height]
          Container(
            margin: EdgeInsets.only(top: 32, right: 45, left: 45),
            height: 45,
            decoration: BoxDecoration(
              color: const Color(0xffE5E0A1),
              borderRadius: BorderRadius.circular(
                17.0,
              ),
              border: Border.all(width: 1.0, color: const Color(0xff2f363d)),
            ),
            child: TabBar(
              controller: _tabController,
              // give the indicator a decoration (color and border radius)
              indicator: BoxDecoration(
                borderRadius: BorderRadius.circular(
                  17.0,
                ),
                color: Colors.black,
              ),
              labelColor: const Color(0xffd4d411),
              unselectedLabelColor: Colors.black,
              tabs: [
                // first tab [you can add an icon using the icon property]
                Tab(
                  child: Text(
                    'Medicines',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 16,
                      //color: const Color(0xffd4d411),
                      letterSpacing: 0.48,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ),

                // second tab [you can add an icon using the icon property]
                Tab(
                  child: Text(
                    'History',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 16,
                      //color: const Color(0xffd4d411),
                      letterSpacing: 0.48,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ),
              ],
            ),
          ),
          // tab bar view her
          Expanded(
            child: TabBarView(
              controller: _tabController,
              children: [
                // Container(
                //padding: EdgeInsets.symmetric(vertical: 25, horizontal: 32),
                //height: 50,
                //child:
                Medicine(),
                //),

                // second tab bar view widget
                MedHistory(),
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: _selectedIndex > 0
          ? Container()
          : FloatingActionButton(
              child: Icon(
                Icons.add,
                size: 40,
              ),
              backgroundColor: const Color(0xffd4d411),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return AddMedicine();
                    },
                  ),
                );
              },
            ),
    );
  }

  Widget Medicine() {
    Size size = MediaQuery.of(context).size;
    return ListView(
      children: [
        Container(
          margin: EdgeInsets.only(top: 25, left: 32, right: 32),
          width: size.width * 0.80,
          height: 54,
          padding: EdgeInsets.symmetric(vertical: 11, horizontal: 20),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(9.0),
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: const Color(0x29000000),
                offset: Offset(0, 3),
                blurRadius: 6,
              ),
            ],
          ),
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Panadol',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                      color: const Color(0xff232425),
                    ),
                    textAlign: TextAlign.left,
                  ),
                  Text(
                    'Thrice a day',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 10,
                      color: const Color(0x80232425),
                      fontWeight: FontWeight.w500,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
              Container(
                width: size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      '50 mg',
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: const Color(0x80515559),
                      ),
                      textAlign: TextAlign.left,
                    ),
                    GestureDetector(
                      onTap: () {
                        myPopMenu();
                        //   FocusedMenuHolder(
                        //     menuBoxDecoration: BoxDecoration(
                        //       color: const Color(0xFFD9D9D9),
                        //     ),
                        //     animateMenuItems: false,
                        //     duration: Duration(milliseconds: 500),
                        //     blurBackgroundColor: Colors.white,
                        //     blurSize: 2,
                        //     menuWidth: size.width * 0.5,
                        //     onPressed: () {},
                        //     menuItems: <FocusedMenuItem>[
                        //       FocusedMenuItem(
                        //           title: Text(
                        //             "Edit",
                        //             style: TextStyle(
                        //               fontFamily: 'Montserrat',
                        //               fontWeight: FontWeight.w600,
                        //               fontSize: 14,
                        //               color: const Color(0xff000000),
                        //             ),
                        //             textAlign: TextAlign.center,
                        //           ),
                        //           onPressed: () {}),
                        //       FocusedMenuItem(
                        //           title: Text(
                        //             "Delete",
                        //             style: TextStyle(
                        //               fontFamily: 'Montserrat',
                        //               fontWeight: FontWeight.w600,
                        //               fontSize: 14,
                        //               color: const Color(0xff000000),
                        //             ),
                        //             textAlign: TextAlign.center,
                        //           ),
                        //           onPressed: () {})
                        //     ],
                        //   );
                      },
                      child: Icon(
                        Icons.more_horiz,
                        size: 20,
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget MedHistory() {
    Size size = MediaQuery.of(context).size;
    return ListView(
      children: [
        Container(
          margin: EdgeInsets.only(top: 25, left: 32, right: 32),
          width: size.width * 0.80,
          height: 50,
          padding: EdgeInsets.symmetric(vertical: 11, horizontal: 20),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(9.0),
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: const Color(0x29000000),
                offset: Offset(0, 3),
                blurRadius: 6,
              ),
            ],
          ),
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Panadol',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                      color: const Color(0xff232425),
                    ),
                    textAlign: TextAlign.left,
                  ),
                  Text(
                    'From: 1 Jan,2021',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 10,
                      color: const Color(0x80232425),
                      fontWeight: FontWeight.w500,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
              Container(
                width: size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      '50 mg',
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: const Color(0x80515559),
                      ),
                      textAlign: TextAlign.left,
                    ),
                    Text(
                      'Ongoing',
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: const Color(0x80232425),
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 25, left: 32, right: 32),
          width: size.width * 0.80,
          height: 50,
          padding: EdgeInsets.symmetric(vertical: 11, horizontal: 20),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(9.0),
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: const Color(0x29000000),
                offset: Offset(0, 3),
                blurRadius: 6,
              ),
            ],
          ),
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Calpol',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                      color: const Color(0xff232425),
                    ),
                    textAlign: TextAlign.left,
                  ),
                  Text(
                    'As needed',
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 10,
                      color: const Color(0x80232425),
                      fontWeight: FontWeight.w500,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
              Container(
                width: size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      '250 mg',
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: const Color(0x80515559),
                      ),
                      textAlign: TextAlign.left,
                    ),
                    Text(
                      'Ended: 20 Jan, 2021',
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: const Color(0x80232425),
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget myPopMenu() {
    return PopupMenuButton(
      color: Colors.white,
        onSelected: (value) {
          Fluttertoast.showToast(
              msg: "You have selected " + value.toString(),
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
              timeInSecForIosWeb: 1,
              backgroundColor: Colors.black,
              textColor: Colors.white,
              fontSize: 16.0);
        },
        itemBuilder: (context) => [
              PopupMenuItem(
                  value: 1,
                  child: Row(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.fromLTRB(2, 2, 8, 2),
                        child: Icon(Icons.print),
                      ),
                      Text('Print')
                    ],
                  )),
              PopupMenuItem(
                value: 2,
                child: Row(
                  children: <Widget>[
                    Text(
                      "Edit",
                      style: TextStyle(
                        fontFamily: 'Montserrat',
                        fontWeight: FontWeight.w600,
                        fontSize: 14,
                        color: const Color(0xff000000),
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ],
                ),
              ),
              PopupMenuItem(
                  value: 3,
                  child: Row(
                    children: <Widget>[
                      Text(
                        "Delete",
                        style: TextStyle(
                          fontFamily: 'Montserrat',
                          fontWeight: FontWeight.w600,
                          fontSize: 14,
                          color: const Color(0xff000000),
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  )),
            ]);
  }
}

PS:我是 Flutter 的新手。请帮帮我

0 个答案:

没有答案