or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
使用 flutter 建構 Favorite Places APP
原始碼連結:https://github.com/WangShuan/flutter-06-favorite-places
學習如何使用設備本身的功能(相機、相簿、定位當前位置)、如何使用 Google Map 在應用程式中顯示地圖與標記位置、如何將數據保存在設備中。
會用到的 pub 介紹
flutter_riverpod
: 用於狀態管理uuid
: 用來產生隨機id
image_picker
: 拍攝照片或從相簿選取圖片並取得檔案來使用location
: 獲取當前位置經、緯度來使用flutter_config
: 用來在專案中存取.env
檔案http
: 與 Google API 進行交互,以獲取經、緯度所對應的地址資訊google_maps_flutter
: 在應用程式中顯示 Google Mappath_provider
: 用於查找文件系統上的常用位置以保存數據path
: 用來簡化操作路徑的方法sqflite
: 使用SQL
命令在設備上存儲數據建立雛型
先建立
models/place.dart
設置地點的藍圖:畫面分析
ListView.builder
建立Stack
建立滿版背景圖再搭配Positioned
放置文字資訊在 bottom 的位置CircleAvatar
製作(可設置radius
調整圓的大小)GestureDetector
包裹住,以綁定點擊事件開啟地圖畫面Form
小部件處理標題欄位,也可直接用TextField
小部件ImagePicker().pickImage(source: ImageSource.camera)
ImagePicker().pickImage(source: ImageSource.gallery)
location.getLocation()
https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&key=$apiKey
https://maps.googleapis.com/maps/api/staticmap?center=$lat,$long&scale=2&zoom=15&size=400x300&key=$apiKey&markers=color:red%7Clabel:%7C$lat%2C$long
GoogleMap
小部件可顯示 Google 地圖initialCameraPosition
即可設置以某經緯度為中心的地圖畫面markers
即可在畫面中顯示 Marker 在想要的經緯度位置上onTap
事件即可在點擊地圖時保存選中的座標位置主要程式碼片段
地點詳細介紹畫面
檔案路徑:
lib/screens/place_screen.dart
圖片選取及預覽區塊
這邊將新增地點中的『圖片選取及預覽』區塊,獨立拉出來做一個客製化小部件
ImgaeInput
:定位或選取地點及預覽區塊
首先將 Google API Key 設為
.env
:flutter_config
:main.dart
中初始化:.env
:接著將獲取經、緯度以及利用經、緯度取得地址資訊的函數抽出來,做成
location_provider.dart
中的方法以方便取用:最後把新增地點畫面中的『定位/選取地點及預覽』區塊,獨立拉出來做一個客製化小部件
LocationInput
:新增地點畫面
這邊主要分享
Form
小部件內容:地圖畫面
將數據保存到設備中
image_input.dart
中選取圖片的函數,將選中的圖片檔案拷貝到設備中,最終傳給父層 Place 對象的 File 為拷貝到設備中檔案,可確保檔案不會因記憶體爆掉被刪除。places_provider.dart
中的方法聲明一個方法獨立於
PlacesNotifier
外面,用來獲取 db :於
addPlace
中將資料insert
到資料庫中:添加
getPlaces
方法,從資料庫獲取數據:最後在
places_screen.dart
中,將ListView.builder
用FutureBuilder
包起來,於初次執行getData
方法初始化資料即可:補充於
AppDelegate.swift
中取用 env 的方法: