You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
samples/experimental/desktop_photo_search/lib/main.dart

123 lines
3.8 KiB

// 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),
),
);
}
}