Skip to content

Flutter can't close Cupertino Context Menu inside Cupertino App with Cupertino tabs #49991

@nilpntr

Description

@nilpntr
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:parkeagle_cupertino/assets.dart';
import 'package:http/http.dart' as http;
import 'package:parkeagle_cupertino/tabs/sensor/config.dart';
import 'dart:convert';
import 'package:platform_maps_flutter/platform_maps_flutter.dart';

class SensorTab extends StatefulWidget {
  @override
  _SensorTabState createState() => _SensorTabState();
}

class _SensorTabState extends State<SensorTab> with SingleTickerProviderStateMixin {

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Padding(
        padding: scrollHorizontalPadding,
        child: SafeArea(
            top: true,
            left: true,
            right: true,
            bottom: true,
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text("Sensor", style: headerFontStyle),
                  Padding(
                    padding: settingsItemPadding,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("SENSORS", style: settingsLabelFontStyle,),
                        FutureBuilder(
                          future: _getSensors(),
                          builder: (BuildContext context, AsyncSnapshot snapshot) {
                            if (snapshot.data == null) {
                              return CupertinoActivityIndicator(animating: true,);
                            } else {
                              return ListView.builder(
                                scrollDirection: Axis.vertical,
                                shrinkWrap: true,
                                itemCount: snapshot.data.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return CupertinoContextMenu(
                                    child: Padding(
                                      padding: settingsItemPadding,
                                      child: Container(
                                        decoration: BoxDecoration(
                                            color: cardBackgroundColor,
                                            borderRadius: cardRadius
                                        ),
                                        child: Padding(
                                          padding: cardItemPadding,
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            children: <Widget>[
                                              Expanded(
                                                flex: 9,
                                                child: Text(snapshot.data[index].name, style: settingsGroupItemFontStyle,)
                                              ),
                                              Expanded(
                                                flex: 3,
                                                child: CupertinoSwitch(
                                                  value: snapshot.data[index].active,
                                                  onChanged: (bool value) {
                                                    snapshot.data[index].active = value;
                                                  },
                                                  activeColor: activeCheckboxColor,
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                      ),
                                    ),
                                    actions: <Widget>[
                                      CupertinoContextMenuAction(
                                        child: const Text('Settings'),
                                        onPressed: () {
                                          Navigator.push(context, CupertinoPageRoute(builder: (context) => SensorConfig()));
                                        },
                                        isDestructiveAction: false,
                                      ),
                                      CupertinoContextMenuAction(
                                        child: const Text('Close'),
                                        onPressed: () {
                                          Navigator.pop(context);
                                        },
                                        isDestructiveAction: true,
                                      ),
                                    ],
                                    previewBuilder: (BuildContext context, Animation<double> animation, Widget child) {
                                      return Container(
                                        decoration: BoxDecoration(
                                          color: cardBackgroundColor,
                                          borderRadius: BorderRadius.circular(10)
                                        ),
                                        child: Center(
                                          child: PlatformMap(
                                            initialCameraPosition: CameraPosition(
                                                target: LatLng(snapshot.data[index].location[0], snapshot.data[index].location[1]),
                                                zoom: 19
                                            ),
                                            markers: Set<Marker>.of(
                                              [
                                                Marker(
                                                  markerId: MarkerId('marker_1'),
                                                  position: LatLng(snapshot.data[index].location[0], snapshot.data[index].location[1]),
                                                  consumeTapEvents: true,
                                                  infoWindow: InfoWindow(
                                                    title: snapshot.data[index].name,
                                                    snippet: allWordsCapitilize(snapshot.data[index].type),
                                                  ),
                                                  onTap: () {
                                                    print("Marker tapped");
                                                  },
                                                ),
                                              ]
                                            ),
                                          )
                                        )
                                      );
                                    },
                                  );
                                },
                              );
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            )),
      ),
    );
  }
  String allWordsCapitilize (String str) {
    return str.toLowerCase().split(' ').map((word) {
      String leftText = (word.length > 1) ? word.substring(1, word.length) : '';
      return word[0].toUpperCase() + leftText;
    }).join(' ');
  }
}



class Sensor {
  final String id;
  final bool active;
  final String type;
  final String name;
  final List location;
  Sensor(this.id, this.active, this.type, this.name, this.location);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: cupertinoflutter/packages/flutter/cupertino repositoryframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions