123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:oktoast/oktoast.dart';
- import 'package:photo/photo.dart';
- import 'package:photo_manager/photo_manager.dart';
-
- import './preview.dart';
- import 'icon_text_button.dart';
- import 'picked_example.dart';
-
- void main() => runApp(MyApp());
-
- class MyApp extends StatelessWidget {
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return OKToast(
- child: MaterialApp(
- title: 'Pick Image Demo',
- theme: ThemeData(
- primaryColor: Color(0xFF01AAFF),
- // colorScheme: ColorScheme.light().copyWith(surface: Colors.white, onPrimary: Color(0xFFFFFFFF)),
- // appBarTheme: AppBarTheme(
- // color: Color(0xFFFFFFFF),
- // elevation: 0,
- // iconTheme: IconThemeData(
- // color: Color(0xFF595959),
- // ),
- // actionsIconTheme: IconThemeData(
- // color: Color(0xFF01AAFF),
- // ),
- // textTheme: TextTheme(
- // headline6: TextStyle(
- // fontSize: 18,
- // fontWeight: FontWeight.w500,
- // color: Color(0xFF595959),
- // ),
- // ),
- // ),
- // dividerColor: Color(0xFFF5F5F5),
- ),
- home: MyHomePage(title: 'Pick Image Demo'),
- ),
- );
- }
- }
-
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
-
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
-
- class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
- String currentSelected = "";
-
- @override
- Widget buildBigImageLoading(
- BuildContext context, AssetEntity entity, Color themeColor) {
- return Center(
- child: Container(
- width: 50.0,
- height: 50.0,
- child: CupertinoActivityIndicator(
- radius: 25.0,
- ),
- ),
- );
- }
-
- @override
- Widget buildPreviewLoading(
- BuildContext context, AssetEntity entity, Color themeColor) {
- return Center(
- child: Container(
- width: 50.0,
- height: 50.0,
- child: CupertinoActivityIndicator(
- radius: 25.0,
- ),
- ),
- );
- }
-
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- actions: <Widget>[
- FlatButton(
- child: Icon(Icons.image),
- onPressed: _testPhotoListParams,
- ),
- ],
- ),
- body: Container(
- child: SingleChildScrollView(
- child: Column(
- children: <Widget>[
- IconTextButton(
- icon: Icons.photo,
- text: "photo",
- onTap: () => _pickAsset(PickType.onlyImage),
- ),
- IconTextButton(
- icon: Icons.videocam,
- text: "video",
- onTap: () => _pickAsset(PickType.onlyVideo),
- ),
- IconTextButton(
- icon: Icons.album,
- text: "all",
- onTap: () => _pickAsset(PickType.all),
- ),
- IconTextButton(
- icon: CupertinoIcons.reply_all,
- text: "Picked asset example.",
- onTap: () => routePage(PickedExample()),
- ),
- Container(
- height: 210,
- child: PhotoPicker.buildGallery(
- context: context,
- padding: 4.0,
- itemHeight: 210,
- itemWidth: 180,
- provider: I18nProvider.chinese,
- sortDelegate: SortDelegate.common,
- loadingDelegate: this,
- pickType: PickType.onlyImage,
- photoPathList: null,
- onSelected: (List<AssetEntity> value) {
- print(value);
- }
- ),
- )
- ],
- ),
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () => _pickAsset(PickType.all),
- tooltip: 'pickImage',
- child: Icon(Icons.add),
- ),
- );
- }
-
- void _testPhotoListParams() async {
- var assetPathList =
- await PhotoManager.getAssetPathList(type: RequestType.image);
- _pickAsset(PickType.all, pathList: assetPathList);
- }
-
- void _pickAsset(PickType type, {List<AssetPathEntity> pathList}) async {
- /// context is required, other params is optional.
- /// context is required, other params is optional.
- /// context is required, other params is optional.
- List<AssetPathEntity> pathList2 = await PhotoManager.getAssetPathList(
- hasAll: true, type: RequestType.image,);
- List<AssetEntity> imgList = await PhotoPicker.pickAsset(
- context: context,
- padding: 4.0,
- itemRadio: 1,
- maxSelected: 8,
- provider: I18nProvider.chinese,
- rowCount: 4,
- thumbSize: 150,
- sortDelegate: SortDelegate.common,
- pickType: PickType.onlyVideo,
- photoPathList: pathList2,
- );
-
- if (imgList == null || imgList.isEmpty) {
- showToast("No pick item.");
- return;
- } else {
- List<String> r = [];
- for (var e in imgList) {
- var file = await e.file;
- r.add(file.absolute.path);
- }
- currentSelected = r.join("\n\n");
-
- List<AssetEntity> preview = [];
- preview.addAll(imgList);
- Navigator.push(context,
- MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
- }
- setState(() {});
- }
-
- void routePage(Widget widget) {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (BuildContext context) {
- return widget;
- },
- ),
- );
- }
- }
|