Flutter插件flutter_plugin_tk的使用详解
flutter_plugin #
这是一个新的Flutter插件项目。
开始使用 #
此项目是一个Flutter插件包的起点, 这是一种包含针对Android和/或iOS的平台特定实现代码的特殊包。
如需了解如何开始使用Flutter,请查看我们的 在线文档,其中包含教程、示例、移动开发指南和完整的API参考。
使用示例 #
下面是一个完整的示例,展示了如何使用flutter_plugin_tk
插件来获取联系人列表、从图库选择单个图像、从图库选择多个图像以及从相机拍摄图像。
example/lib/main.dart
import 'dart:developer';
import 'dart:io';
import ‘package:flutter/material.dart’;
import ‘package:flutter/services.dart’;
import ‘package:flutter_plugin/contact_model.dart’;
import ‘package:flutter_plugin/flutter_plugin_tk.dart’;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final List<Contact> _listContact = [];
final List<String> _listPath = [];
late String _imageUri = “”;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
useInheritedMediaQuery: true,
home: Scaffold(
appBar: AppBar(
title: const Text(‘插件示例应用’),
),
body: Column(
children: [
TextButton(
onPressed: () => _getAllContact(),
child: const Text(“获取所有联系人”),
),
TextButton(
onPressed: () => _getImageFromGallery(),
child: const Text(“从图库获取图像”),
),
TextButton(
onPressed: () => _getMultiImageFromGallery(),
child: const Text(“从图库获取多张图像”),
),
TextButton(
onPressed: () => _takeImageFromCamera(),
child: const Text(“从相机拍摄图像”),
),
SizedBox(
width: 100,
height: 100,
child: _imageUri == “”
? const SizedBox()
: Image.file(
File(_imageUri),
fit: BoxFit.cover,
),
),
_listPath.isNotEmpty
? Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return SizedBox(
width: 100,
height: 100,
child: Image.file(
File(_listPath[index]),
fit: BoxFit.cover,
),
);
},
itemCount: _listPath.length,
),
)
: const SizedBox(),
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
_listContact[index].name,
style: Theme.of(context)
.textTheme
.headline6!
.copyWith(color: Colors.black),
),
Text(
_listContact[index].number,
style: Theme.of(context)
.textTheme
.headline6!
.copyWith(color: Colors.red),
)
],
);
},
itemCount: _listContact.length,
),
)
],
),
),
);
}
void _getAllContact() async {
List<Contact> listContact;
try {
listContact = await FlutterPlugin.getAllContact;
} on PlatformException catch (platEx) {
log("_getAllContact/PlatformException $platEx");
listContact = [];
} catch (ex) {
log("_getAllContact/Exception $ex");
listContact = [];
}
if (!mounted) return;
setState(() {
_listContact.addAll(listContact);
});
}
void _getImageFromGallery() async {
String? imageUri;
try {
imageUri = await FlutterPlugin.getImageFromGallery;
} on PlatformException catch (platEx) {
log("_getImageFromGallery/PlatformException $platEx");
} catch (ex) {
log("_getImageFromGallery/Exception $ex");
}
setState(() {
_imageUri = imageUri!;
});
}
void _getMultiImageFromGallery() async {
List<String> listPath = [];
try {
listPath.addAll(await FlutterPlugin.getMultiImageFromGallery);
} on PlatformException catch (platEx) {
log("_getMultiImageFromGallery/PlatformException $platEx");
listPath = [];
} catch (ex) {
log("_getMultiImageFromGallery/Exception $ex");
listPath = [];
}
setState(() {
_listPath.addAll(listPath);
});
}
void _takeImageFromCamera() async {
String? imageUri;
try {
imageUri = await FlutterPlugin.getImageFromCamera;
} on PlatformException catch (platEx) {
log("_takeImageFromCamera/PlatformException $platEx");
} catch (ex) {
log("_takeImageFromCamera/Exception $ex");
}
setState(() {
_imageUri = imageUri!;
});
}
}
更多关于Flutter插件flutter_plugin_tk的使用详解的实战教程也可以访问 https://www.itying.com/category-92-b0.html