如何添加底表?

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

enter image description here

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart' as http;
import 'package:geocoder/geocoder.dart';
import 'package:mapsearch/screen/add_place_screen.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  LatLng _lastPosition;
  TextEditingController locationController = TextEditingController();
  GoogleMapController mapController;

  void onMapCreated(controller) {
    setState(() {
      mapController = controller;
    });
  }

  searchNavigate() {
    Geolocator().placemarkFromAddress(locationController.text).then((result) {
      //move the map camera to search location
      mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
          target:
              LatLng(result[0].position.latitude, result[0].position.longitude),
          zoom: 30.0)));
    });
  }

  Future<void> getCurrentLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
    setState(() {
      //move the camera to user current location
      mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: LatLng(position.latitude, position.longitude),
        zoom: 20.0,
      )));
    });
  }

  _onCameraMove(CameraPosition position) {
    setState(() {
      _lastPosition = position.target;
      final coordinates =
          Coordinates(_lastPosition.latitude, _lastPosition.longitude);
      Geocoder.local
          .findAddressesFromCoordinates(coordinates)
          .then((addresses) {
        setState(() {
          locationController.text = addresses.first.addressLine;
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          GoogleMap(
            onMapCreated: onMapCreated,
            initialCameraPosition: (CameraPosition(
              target: LatLng(3.1390, 101.6869),
              zoom: 15.0,
            )),
            onCameraMove: _onCameraMove,
          ),
          Positioned(
            top: 30.0,
            right: 15.0,
            left: 15.0,
            child: Container(
              height: 50.0,
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.white,
              ),
              child: TextField(
                onChanged: (value) async {
                  Prediction p = await PlacesAutocomplete.show(
                      context: context,
                      apiKey: 'AIzaSyDoA0-q0UxChFxGpVCIPdAiyshlRjxcT-MQQ',
                      mode: Mode.overlay, // Mode.fullscreen
                      language: "en",
                      components: [new Component(Component.country, "mys")]);
                  setState(() {
                    locationController.text = p.description;
                    print(locationController.text);
                  });
                },
                decoration: InputDecoration(
                  hintText: 'Search Location',
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(left: 15.0, top: 15.0),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.search),
                    iconSize: 30.0,
                    onPressed: searchNavigate,
                  ),
                ),
                controller: locationController,
              ),
            ),
          ),
          Positioned(
            bottom: 50.0,
            right: 15.0,
            child: FloatingActionButton(
              heroTag: 'btn2',
              backgroundColor: Colors.lightBlueAccent,
              child: Icon(
                Icons.location_searching,
                color: Colors.white,
              ),
              onPressed: getCurrentLocation,
            ),
          ),
          Positioned(
            bottom: 120.0,
            right: 15.0,
            child: FloatingActionButton(
              heroTag: 'btn1',
              backgroundColor: Colors.lightBlueAccent,
              child: Icon(
                Icons.add,
                color: Colors.white,
              ),
              onPressed: () {
//                Navigator.of(context).push(
//                  MaterialPageRoute(
//                    builder: (ctx) => AddPlaceScreen(
//                      placeName: locationController.text,
//                      position: _lastPosition,
//                    ),
//                  ),
//                );
              showBottomSheet(context: context, builder: (context) => Container(
                color: Colors.red,
              ));
              },
            ),
          ),
        ],
      ),
    );
  }
}
flutter flutter-layout
1个回答
0
投票

移动您的Scaffold主体以分离无状态小部件或使用Builder

[观看该视频以了解发生的原因

https://www.youtube.com/watch?v=xXNOkIuSYuA&vl=en

© www.soinside.com 2019 - 2024. All rights reserved.