mirror of
https://github.com/flutter/samples.git
synced 2025-11-11 07:18:15 +00:00
Desktop photo search (#174)
This commit is contained in:
122
experimental/desktop_photo_search/lib/main.dart
Normal file
122
experimental/desktop_photo_search/lib/main.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_chooser/file_chooser.dart' as file_choser;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:menubar/menubar.dart' as menubar;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'src/model/photo_search_model.dart';
|
||||
import 'src/unsplash/unsplash.dart';
|
||||
import 'src/widgets/data_tree.dart';
|
||||
import 'src/widgets/photo_details.dart';
|
||||
import 'src/widgets/photo_search_dialog.dart';
|
||||
import 'src/widgets/split.dart';
|
||||
import 'unsplash_access_key.dart';
|
||||
|
||||
void main() {
|
||||
Logger.root.level = Level.ALL;
|
||||
Logger.root.onRecord.listen((rec) {
|
||||
// ignore: avoid_print
|
||||
print('${rec.loggerName} ${rec.level.name}: ${rec.time}: ${rec.message}');
|
||||
});
|
||||
|
||||
if (unsplashAccessKey.isEmpty) {
|
||||
Logger('main').severe('Unsplash Access Key is required. '
|
||||
'Please add to `lib/unsplash_access_key.dart`.');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
runApp(
|
||||
ChangeNotifierProvider<PhotoSearchModel>(
|
||||
create: (context) => PhotoSearchModel(
|
||||
Unsplash(accessKey: unsplashAccessKey),
|
||||
),
|
||||
child: UnsplashSearchApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class UnsplashSearchApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Photo Search',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.orange,
|
||||
),
|
||||
home: const UnsplashHomePage(title: 'Photo Search'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnsplashHomePage extends StatelessWidget {
|
||||
const UnsplashHomePage({@required this.title});
|
||||
final String title;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final photoSearchModel = Provider.of<PhotoSearchModel>(context);
|
||||
menubar.setApplicationMenu([
|
||||
menubar.Submenu(label: 'Search', children: [
|
||||
menubar.MenuItem(
|
||||
label: 'Search ...',
|
||||
onClicked: () {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
PhotoSearchDialog(photoSearchModel.addSearch),
|
||||
);
|
||||
},
|
||||
),
|
||||
])
|
||||
]);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title),
|
||||
),
|
||||
body: photoSearchModel.entries.isNotEmpty
|
||||
? Split(
|
||||
axis: Axis.horizontal,
|
||||
initialFirstFraction: 0.4,
|
||||
firstChild: DataTree(photoSearchModel.entries),
|
||||
secondChild: Center(
|
||||
child: photoSearchModel.selectedPhoto != null
|
||||
? PhotoDetails(
|
||||
photo: photoSearchModel.selectedPhoto,
|
||||
onPhotoSave: (photo) async {
|
||||
final result = await file_choser.showSavePanel(
|
||||
suggestedFileName: '${photo.id}.jpg',
|
||||
allowedFileTypes: ['jpg'],
|
||||
);
|
||||
if (!result.canceled) {
|
||||
final bytes =
|
||||
await photoSearchModel.download(photo: photo);
|
||||
await File(result.paths[0]).writeAsBytes(bytes);
|
||||
}
|
||||
},
|
||||
)
|
||||
: Container(),
|
||||
),
|
||||
)
|
||||
: const Center(
|
||||
child: Text('Search for Photos using the Fab button'),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => PhotoSearchDialog(photoSearchModel.addSearch),
|
||||
),
|
||||
tooltip: 'Search for a photo',
|
||||
child: Icon(Icons.search),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user