ente/lib/ui/face_search_results_page.dart

103 lines
3 KiB
Dart
Raw Normal View History

2020-04-05 14:00:44 +00:00
import 'package:flutter/material.dart';
import 'package:myapp/db/db_helper.dart';
import 'package:myapp/face_search_manager.dart';
import 'package:myapp/models/face.dart';
2020-04-05 14:45:04 +00:00
import 'package:myapp/models/photo.dart';
2020-04-05 14:00:44 +00:00
import 'package:myapp/models/search_result.dart';
import 'package:myapp/ui/circular_network_image_widget.dart';
import 'package:myapp/core/constants.dart' as Constants;
2020-04-05 14:45:04 +00:00
import 'package:myapp/ui/image_widget.dart';
2020-04-05 14:00:44 +00:00
import 'package:myapp/ui/network_image_detail_page.dart';
import 'detail_page.dart';
class FaceSearchResultsPage extends StatelessWidget {
final FaceSearchManager _faceSearchManager = FaceSearchManager.instance;
final Face _face;
FaceSearchResultsPage({Key key, Face face})
: this._face = face,
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Search results"),
actions: <Widget>[
Hero(
tag: "face_" + _face.faceID.toString(),
child: CircularNetworkImageWidget(
2020-04-09 19:03:11 +00:00
Constants.ENDPOINT + _face.thumbnailPath, 20),
2020-04-05 14:00:44 +00:00
)
],
),
body: Container(
child: _getBody(),
),
);
}
FutureBuilder<List<SearchResult>> _getBody() {
return FutureBuilder<List<SearchResult>>(
future: _faceSearchManager.getFaceSearchResults(_face),
builder: (context, snapshot) {
if (snapshot.hasData) {
return GridView.builder(
2020-04-05 14:45:04 +00:00
itemBuilder: (_, index) =>
2020-04-09 19:03:11 +00:00
_buildItem(context, snapshot.data[index].path),
2020-04-05 14:00:44 +00:00
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
));
} else {
return Text("Loading...");
}
},
);
}
2020-04-09 19:03:11 +00:00
Widget _buildItem(BuildContext context, String path) {
2020-04-05 14:00:44 +00:00
return GestureDetector(
2020-04-05 14:45:04 +00:00
onTap: () async {
2020-04-09 19:03:11 +00:00
_routeToDetailPage(path, context);
2020-04-05 14:00:44 +00:00
},
2020-04-09 19:03:11 +00:00
child: _getImage(path),
2020-04-05 14:45:04 +00:00
);
}
2020-04-09 19:03:11 +00:00
Widget _getImage(String path) {
2020-04-05 14:45:04 +00:00
return FutureBuilder<Photo>(
2020-04-09 19:03:11 +00:00
future: DatabaseHelper.instance.getPhotoByPath(path),
2020-04-05 14:45:04 +00:00
builder: (_, snapshot) {
if (snapshot.hasData) {
return ImageWidget(path: snapshot.data.thumbnailPath);
} else if (snapshot.hasError) {
return Container(
margin: EdgeInsets.all(2),
2020-04-09 19:03:11 +00:00
child: Image.network(Constants.ENDPOINT + "/" + path,
2020-04-05 14:45:04 +00:00
height: 124, width: 124, fit: BoxFit.cover),
);
} else {
return Text("Loading...");
}
},
2020-04-05 14:00:44 +00:00
);
}
2020-04-09 19:03:11 +00:00
void _routeToDetailPage(String path, BuildContext context) async {
Widget page = NetworkImageDetailPage(path);
var photo = await DatabaseHelper.instance.getPhotoByPath(path);
2020-04-05 14:45:04 +00:00
if (photo != null) {
page = DetailPage(photo);
}
2020-04-05 14:00:44 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
}
}