2022-12-25 12:30:20 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-12-29 08:45:28 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-12-25 12:30:20 +03:00
|
|
|
import 'package:gallery_picker/gallery_picker.dart';
|
2022-12-29 08:45:28 +03:00
|
|
|
import 'package:gallery_picker_example/examples/pick_medias_with_builder.dart';
|
|
|
|
import 'examples/gallery_picker_example.dart';
|
|
|
|
import 'examples/multiple_medias.dart';
|
|
|
|
import 'examples/whatsapp_pick_photo.dart';
|
2022-12-25 12:30:20 +03:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
const MyApp({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
title: 'Flutter Demo',
|
|
|
|
theme: ThemeData(
|
2022-12-29 08:45:28 +03:00
|
|
|
brightness: Brightness.light,
|
|
|
|
/* light theme settings */
|
2022-12-25 12:30:20 +03:00
|
|
|
),
|
2022-12-29 08:45:28 +03:00
|
|
|
darkTheme: ThemeData(
|
|
|
|
brightness: Brightness.dark,
|
|
|
|
/* dark theme settings */
|
|
|
|
),
|
|
|
|
themeMode: ThemeMode.dark,
|
|
|
|
home: const GalleryPickerExample(),
|
2022-12-25 12:30:20 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
2022-12-29 08:45:28 +03:00
|
|
|
final List<MediaFile>? medias;
|
|
|
|
const MyHomePage({super.key, required this.title, this.medias});
|
2022-12-25 12:30:20 +03:00
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
2022-12-29 08:45:28 +03:00
|
|
|
List<MediaFile> selectedMedias = [];
|
2022-12-25 12:30:20 +03:00
|
|
|
|
2022-12-29 08:45:28 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (widget.medias != null) {
|
|
|
|
selectedMedias = widget.medias!;
|
|
|
|
}
|
|
|
|
super.initState();
|
2022-12-25 12:30:20 +03:00
|
|
|
}
|
|
|
|
|
2022-12-29 08:45:28 +03:00
|
|
|
int pageIndex = 0;
|
|
|
|
var controller = PageController(initialPage: 0);
|
2022-12-25 12:30:20 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2022-12-29 08:45:28 +03:00
|
|
|
|
2022-12-25 12:30:20 +03:00
|
|
|
appBar: AppBar(
|
2022-12-29 08:45:28 +03:00
|
|
|
title: Text("Pick medias"),
|
2022-12-25 12:30:20 +03:00
|
|
|
),
|
2022-12-29 08:45:28 +03:00
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
const Spacer(),
|
|
|
|
const Text(
|
|
|
|
'These are your selected medias',
|
2022-12-25 12:30:20 +03:00
|
|
|
),
|
2022-12-29 08:45:28 +03:00
|
|
|
const Divider(),
|
|
|
|
Expanded(
|
|
|
|
flex: 5,
|
|
|
|
child: Stack(children: [
|
|
|
|
if (selectedMedias.isNotEmpty)
|
|
|
|
PageView(
|
|
|
|
controller: controller,
|
|
|
|
children: [
|
|
|
|
for (var media in selectedMedias)
|
|
|
|
Center(
|
|
|
|
child: MediaProvider(
|
|
|
|
media: media,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (selectedMedias.isNotEmpty)
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (pageIndex < selectedMedias.length - 1) {
|
|
|
|
pageIndex++;
|
|
|
|
controller.animateToPage(pageIndex,
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
curve: Curves.easeIn);
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: const Icon(
|
|
|
|
Icons.chevron_right,
|
|
|
|
size: 100,
|
|
|
|
color: Colors.red,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
if (selectedMedias.isNotEmpty)
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (pageIndex > 0) {
|
|
|
|
pageIndex--;
|
|
|
|
controller.animateToPage(pageIndex,
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
curve: Curves.easeIn);
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: const Icon(
|
|
|
|
Icons.chevron_left,
|
|
|
|
size: 100,
|
|
|
|
color: Colors.red,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 65,
|
|
|
|
child: ListView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
children: [
|
|
|
|
for (int i = 0; i < selectedMedias.length; i++)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 5),
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
pageIndex = i;
|
|
|
|
controller.animateToPage(pageIndex,
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
curve: Curves.easeIn);
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
width: 65,
|
|
|
|
height: 50,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border.all(
|
|
|
|
width: 2,
|
|
|
|
color: pageIndex == i
|
|
|
|
? Colors.red
|
|
|
|
: Colors.black)),
|
|
|
|
child: ThumbnailMedia(
|
|
|
|
media: selectedMedias[i],
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const Spacer(
|
|
|
|
flex: 2,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-12-25 12:30:20 +03:00
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
2022-12-29 08:45:28 +03:00
|
|
|
onPressed: pickMedias,
|
2022-12-25 12:30:20 +03:00
|
|
|
tooltip: 'Increment',
|
|
|
|
child: const Icon(Icons.add),
|
2022-12-29 08:45:28 +03:00
|
|
|
),
|
2022-12-25 12:30:20 +03:00
|
|
|
);
|
|
|
|
}
|
2022-12-29 08:45:28 +03:00
|
|
|
|
|
|
|
Future<void> pickMedias() async {
|
|
|
|
List<MediaFile>? medias = await GalleryPicker.pickMedias(
|
|
|
|
context: context,
|
|
|
|
initSelectedMedias: selectedMedias,
|
|
|
|
startWithRecent: true);
|
|
|
|
if (medias != null) {
|
|
|
|
setState(() {
|
|
|
|
this.selectedMedias += medias;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pickMediasWithBuilder() {
|
|
|
|
GalleryPicker.pickMediasWithBuilder(
|
|
|
|
multipleMediasBuilder: ((medias, context) {
|
|
|
|
return MultipleMediasView(medias);
|
|
|
|
}),
|
|
|
|
heroBuilder: (tag, media, context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Hero Page'),
|
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: Hero(
|
|
|
|
tag: tag,
|
|
|
|
child: MediaProvider(
|
|
|
|
media: media,
|
|
|
|
width: MediaQuery.of(context).size.width - 50,
|
|
|
|
height: 300,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
backgroundColor: Colors.blue,
|
|
|
|
onPressed: () {
|
|
|
|
GalleryPicker.dispose();
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => MyHomePage(
|
|
|
|
title: "Selected Medias",
|
|
|
|
medias: [media],
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Icon(
|
|
|
|
Icons.send,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
context: context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getGalleryMedia() async {
|
|
|
|
GalleryMedia? allmedia = await GalleryPicker.collectGallery;
|
|
|
|
}
|
2022-12-25 12:30:20 +03:00
|
|
|
}
|