Flutter-从另一个小部件调用函数

时间:2020-04-21 17:30:21

标签: google-maps flutter dart

我正在开发带有底部导航栏的应用程序,可以在其中导航到2个屏幕。在底部导航栏中,我有一个来自此浮动动作按钮的浮动动作按钮,我将在另一个小部件(findQuakes())中调用一个函数,但是我无法从我的浮动动作按钮中调用函数findQuakes()。 这是底部导航栏小部件,在这里我将调用findQuakes()函数:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:erdbeben/models/quake.dart';
import 'package:erdbeben/network/network.dart';
import 'package:erdbeben/ui/earthquakeMapPage.dart';
import 'package:erdbeben/ui/earthquakeListPage.dart';

class BottomNavigationBarController extends StatefulWidget {
  @override
  _BottomNavigationBarControllerState createState() => _BottomNavigationBarControllerState();
}

class _BottomNavigationBarControllerState extends State<BottomNavigationBarController> {
  int _currentPageNumber;
  final List<Widget> pages = [
    EarthquakeMap(),
    EarthquakeList(),
  ];
  Widget _currentPage;
  final PageStorageBucket _bucket = PageStorageBucket();

  @override
  void initState() {
    _currentPageNumber = 0;
    _currentPage = EarthquakeMap();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageStorage(bucket: _bucket, child: _currentPage),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              EarthquakeMapState().findQuakes();
            });
          },
          child: Text("2.5+")),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.public),
            title: Text('Karte'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.dehaze),
            title: Text('Liste'),
          ),
        ],
        onTap: (int index) => _showPage(index),
      ),
    );
  }

  void _showPage(int index) {
    setState(() {
      switch (index) {
        case 0:
          _currentPage = EarthquakeMap();
          _currentPageNumber = 0;
          break;
        case 1:
          _currentPage = EarthquakeList();
          _currentPageNumber = 1;
          break;
      }
    });
  }
}

这是带有功能findQuakes()的StatefulWidget:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:erdbeben/models/quake.dart';
import 'package:erdbeben/network/network.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:erdbeben/ui/bottomNavigationBarController.dart';

class EarthquakeMap extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new EarthquakeMapState();
}

class EarthquakeMapState extends State<EarthquakeMap> {
  Future<Quake> quakesData;
  List<Marker> _earthquakeList = <Marker>[];
  Completer<GoogleMapController> _controller = Completer();

  @override
  void initState() {
    quakesData = Network().getAllQuakes();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Stack(
        children: <Widget>[_buildEarthquakeMap(context)],
      ),
    );
  }

  _buildEarthquakeMap(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: GoogleMap(
        mapType: MapType.hybrid,
        zoomControlsEnabled: false,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
        initialCameraPosition: CameraPosition(target: LatLng(52.519735, 13.4046413)),
        markers: Set<Marker>.of(_earthquakeList),
      ),
    );
  }

  findQuakes() {
    setState(() {
      _earthquakeList.clear();
      _handleResponse();
    });
  }

  void _handleResponse() {
    setState(() {
      quakesData.then((quakes) => [
            quakes.features.forEach((quake) => [
                  _earthquakeList.add(Marker(
                      markerId: MarkerId(quake.id),
                      infoWindow: InfoWindow(title: quake.properties.mag.toString(), snippet: quake.properties.place),
                      icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueMagenta),
                      position: LatLng(quake.geometry.coordinates[1], quake.geometry.coordinates[0]),
                      onTap: () {}))
                ])
          ]);
    });
  }
}

如何从BottomNavigationBarController调用函数findQuakes()?

有人可以帮我吗,我找不到解决办法。

0 个答案:

没有答案