2025-12-29 First Commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import './person_model.dart';
|
||||
import '../services/generic_api_service.dart'; // 假設您有這個共用服務
|
||||
|
||||
class PersonApiService {
|
||||
final GenericApiService _apiService = GenericApiService();
|
||||
|
||||
// 獲取所有人員列表,增加可選的 searchName 參數
|
||||
Future<List<Person>> fetchPeople({String? searchName}) async {
|
||||
// 預設參數:每頁 100 筆,按中文姓名排序
|
||||
String filterPart = "";
|
||||
|
||||
// 如果提供了 searchName,則增加模糊查詢過濾條件
|
||||
if (searchName != null && searchName.isNotEmpty) {
|
||||
filterPart = "personcname^%$searchName%";
|
||||
}
|
||||
|
||||
String v_queryFilter = "1^100^personid^*^^^$filterPart";
|
||||
|
||||
return await _apiService.fetchList<Person>(
|
||||
tableName: "basperson", // 對應到 basperson 表格
|
||||
pk: "personid", // 主鍵為 personid
|
||||
queryFilter: v_queryFilter,
|
||||
fromJson: (json) => Person.fromJson(json),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import './person_model.dart';
|
||||
import 'package:url_launcher/url_launcher.dart'; // 用於撥打電話和發送郵件
|
||||
|
||||
class PersonDetail extends StatelessWidget {
|
||||
final Person person;
|
||||
|
||||
const PersonDetail({required this.person, super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(person.personCName ?? '人員詳細資訊'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
// 姓名與職稱
|
||||
CircleAvatar(
|
||||
radius: 50,
|
||||
backgroundColor: person.sexColor.withOpacity(0.2),
|
||||
child: Icon(person.sexIcon, size: 60, color: person.sexColor),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
person.personCName ?? 'N/A',
|
||||
style: const TextStyle(fontSize: 28.0, fontWeight: FontWeight.bold, color: Colors.black87),
|
||||
),
|
||||
Text(
|
||||
person.jobName ?? 'N/A',
|
||||
style: const TextStyle(fontSize: 18.0, color: Colors.black54),
|
||||
),
|
||||
const Divider(height: 30.0, thickness: 1),
|
||||
|
||||
// 聯絡資訊列表
|
||||
_buildInfoTile(
|
||||
Icons.badge,
|
||||
'工號 / ID',
|
||||
person.personId,
|
||||
),
|
||||
_buildInfoTile(
|
||||
Icons.business,
|
||||
'部門代號',
|
||||
person.departmentId ?? 'N/A',
|
||||
),
|
||||
_buildActionTile(
|
||||
context,
|
||||
Icons.phone,
|
||||
'公司電話',
|
||||
person.tel ?? 'N/A',
|
||||
person.tel,
|
||||
'tel:',
|
||||
),
|
||||
_buildActionTile(
|
||||
context,
|
||||
Icons.smartphone,
|
||||
'手機號碼',
|
||||
person.cellphone ?? 'N/A',
|
||||
person.cellphone,
|
||||
'tel:',
|
||||
),
|
||||
_buildActionTile(
|
||||
context,
|
||||
Icons.email,
|
||||
'電子郵件',
|
||||
person.email ?? 'N/A',
|
||||
person.email,
|
||||
'mailto:',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 靜態資訊欄位
|
||||
Widget _buildInfoTile(IconData icon, String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, color: Colors.grey[700], size: 24),
|
||||
const SizedBox(width: 15),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 可操作(點擊撥號/發送郵件)的欄位
|
||||
Widget _buildActionTile(
|
||||
BuildContext context, IconData icon, String label, String displayValue,
|
||||
String? actionValue, String protocol) {
|
||||
final canLaunch = actionValue != null && actionValue.isNotEmpty;
|
||||
|
||||
Future<void> _launchUrl() async {
|
||||
if (canLaunch) {
|
||||
final uri = Uri.parse('$protocol$actionValue');
|
||||
if (!await launchUrl(uri)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('無法打開 $displayValue')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: InkWell(
|
||||
onTap: canLaunch ? _launchUrl : null,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, color: canLaunch ? Colors.deepPurple : Colors.grey[700], size: 24),
|
||||
const SizedBox(width: 15),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
Text(
|
||||
displayValue,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: canLaunch ? Colors.deepPurple : Colors.black,
|
||||
decoration: canLaunch ? TextDecoration.underline : TextDecoration.none,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import './person_api.dart';
|
||||
import './person_model.dart';
|
||||
import './person_detail.dart'; // 稍後創建
|
||||
|
||||
class PersonManager extends StatefulWidget {
|
||||
const PersonManager({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return _PersonManagerState();
|
||||
}
|
||||
}
|
||||
|
||||
class _PersonManagerState extends State<PersonManager> {
|
||||
late PersonApiService _apiService;
|
||||
late Future<List<Person>> _peopleFuture;
|
||||
|
||||
// 新增:搜尋文字控制器
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
|
||||
// 新增:用於記錄當前的搜尋關鍵字
|
||||
String _currentSearchTerm = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_apiService = PersonApiService();
|
||||
// 頁面加載時自動開始獲取資料
|
||||
_peopleFuture = _apiService.fetchPeople();
|
||||
|
||||
// 新增:監聽搜尋框文字變更
|
||||
_searchController.addListener(_onSearchChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// 釋放資源
|
||||
_searchController.removeListener(_onSearchChanged);
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// 搜尋文字變更時觸發的邏輯
|
||||
void _onSearchChanged() {
|
||||
final newTerm = _searchController.text;
|
||||
// 只有當關鍵字真正改變時才刷新
|
||||
if (newTerm != _currentSearchTerm) {
|
||||
_currentSearchTerm = newTerm;
|
||||
_refreshPeople();
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新資料的函數
|
||||
void _refreshPeople() {
|
||||
setState(() {
|
||||
// 調用 API 時傳入目前的搜尋關鍵字
|
||||
_peopleFuture = _apiService.fetchPeople(searchName: _currentSearchTerm);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('公司通訊錄'),
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: '刷新列表',
|
||||
onPressed: _refreshPeople,
|
||||
),
|
||||
],
|
||||
// 新增:底部放置搜尋框
|
||||
bottom: PreferredSize(
|
||||
preferredSize: const Size.fromHeight(60.0),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: TextField(
|
||||
controller: _searchController,
|
||||
decoration: InputDecoration(
|
||||
hintText: '輸入姓名進行查詢...',
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
suffixIcon: _currentSearchTerm.isNotEmpty
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
_searchController.clear(); // 清空文字會觸發 _onSearchChanged
|
||||
},
|
||||
)
|
||||
: null,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
filled: true,
|
||||
fillColor: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
body: FutureBuilder<List<Person>>(
|
||||
future: _peopleFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (snapshot.hasError) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('載入失敗: ${snapshot.error}', textAlign: TextAlign.center),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(onPressed: _refreshPeople, child: const Text('重試')),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else if (snapshot.hasData && snapshot.data!.isNotEmpty) {
|
||||
return PersonList(people: snapshot.data!);
|
||||
} else {
|
||||
return const Center(child: Text('查無人員資料。'));
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// 列表顯示小部件 (PersonList)
|
||||
// -----------------------------------------------------------
|
||||
|
||||
class PersonList extends StatelessWidget {
|
||||
final List<Person> people;
|
||||
|
||||
const PersonList({required this.people, super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: people.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
final person = people[index];
|
||||
|
||||
return Card(
|
||||
elevation: 3,
|
||||
margin: const EdgeInsets.symmetric(vertical: 6.0, horizontal: 16.0),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
// 點擊項目:導航到詳細頁面
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PersonDetail(person: person),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: ListTile(
|
||||
// 左側性別指示器
|
||||
leading: Icon(
|
||||
person.sexIcon,
|
||||
color: person.sexColor,
|
||||
size: 32,
|
||||
),
|
||||
title: Text(
|
||||
person.personCName ?? '姓名未知',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(
|
||||
'${person.departmentId ?? '無部門'} | ${person.jobName ?? '無職稱'}',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
trailing: const Icon(Icons.arrow_forward_ios, size: 16, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Person {
|
||||
final String personId; // personid
|
||||
final String? personCName; // personcname (中文姓名)
|
||||
final String? jobName; // jobname (職稱)
|
||||
final String? departmentId; // departmentid (部門代號)
|
||||
final String? tel; // tel (公司電話)
|
||||
final String? cellphone; // cellphone (手機)
|
||||
final String? email; // email (電子郵件)
|
||||
final String? sex; // sex (性別)
|
||||
|
||||
Person({
|
||||
required this.personId,
|
||||
this.personCName,
|
||||
this.jobName,
|
||||
this.departmentId,
|
||||
this.tel,
|
||||
this.cellphone,
|
||||
this.email,
|
||||
this.sex,
|
||||
});
|
||||
|
||||
// Factory 構造函數:從 API 返回的 JSON (Map) 創建 Person 物件
|
||||
factory Person.fromJson(Map<String, dynamic> json) {
|
||||
return Person(
|
||||
personId: json['personid'] as String? ?? 'N/A', //
|
||||
personCName: json['personcname'] as String?, //
|
||||
jobName: json['jobname'] as String?, //
|
||||
departmentId: json['departmentid'] as String?, //
|
||||
tel: json['tel'] as String?, //
|
||||
cellphone: json['cellphone'] as String?, //
|
||||
email: json['email'] as String?, //
|
||||
sex: json['sex'] as String?, //
|
||||
);
|
||||
}
|
||||
|
||||
// 輔助屬性:獲取性別圖示
|
||||
IconData get sexIcon {
|
||||
switch (sex?.toUpperCase()) {
|
||||
case 'M':
|
||||
return Icons.male;
|
||||
case 'F':
|
||||
return Icons.female;
|
||||
default:
|
||||
return Icons.person;
|
||||
}
|
||||
}
|
||||
|
||||
// 輔助屬性:獲取性別顏色
|
||||
Color get sexColor {
|
||||
switch (sex?.toUpperCase()) {
|
||||
case 'M':
|
||||
return Colors.blue;
|
||||
case 'F':
|
||||
return Colors.pink;
|
||||
default:
|
||||
return Colors.grey;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user