2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/entities/user.dart';
|
|
|
|
|
import 'package:fcs/helpers/theme.dart';
|
|
|
|
|
import 'package:fcs/pages/package/model/package_model.dart';
|
|
|
|
|
import 'package:fcs/pages/user_search/user_list_row.dart';
|
2024-01-25 17:40:35 +06:30
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-09-15 07:13:41 +06:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
2021-01-10 15:56:27 +06:30
|
|
|
typedef OnUserSelect(User suer);
|
|
|
|
|
typedef OnUserRowSelect(User suer);
|
2020-09-15 07:13:41 +06:30
|
|
|
|
2021-09-10 16:33:52 +06:30
|
|
|
Future<User?> searchUser(BuildContext context,
|
|
|
|
|
{required OnUserSelect onUserSelect, bool popPage = false}) async =>
|
2020-09-15 07:13:41 +06:30
|
|
|
await showSearch<User>(
|
|
|
|
|
context: context,
|
2021-01-10 15:56:27 +06:30
|
|
|
delegate:
|
|
|
|
|
UserSearchDelegate(onUserSelect: onUserSelect, popPage: popPage),
|
2020-09-15 07:13:41 +06:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
class UserSearchDelegate extends SearchDelegate<User> {
|
2021-09-10 14:29:55 +06:30
|
|
|
final OnUserSelect? onUserSelect;
|
2021-01-10 15:56:27 +06:30
|
|
|
final bool popPage;
|
2020-09-15 07:13:41 +06:30
|
|
|
|
2021-09-10 16:33:52 +06:30
|
|
|
UserSearchDelegate({required this.onUserSelect, required this.popPage});
|
2020-09-15 07:13:41 +06:30
|
|
|
|
|
|
|
|
@override
|
2020-09-16 02:29:50 +06:30
|
|
|
String get searchFieldLabel => 'Search by FCS ID or Name';
|
2020-09-15 07:13:41 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ThemeData appBarTheme(BuildContext context) {
|
|
|
|
|
final ThemeData theme = Theme.of(context);
|
|
|
|
|
return theme.copyWith(
|
2021-09-13 11:10:17 +06:30
|
|
|
appBarTheme: AppBarTheme(color: primaryColor),
|
2024-01-25 17:40:35 +06:30
|
|
|
iconButtonTheme: IconButtonThemeData(
|
|
|
|
|
style: ButtonStyle(
|
|
|
|
|
iconColor: MaterialStateProperty.all<Color>(Colors.white))),
|
2020-09-15 07:13:41 +06:30
|
|
|
inputDecorationTheme: InputDecorationTheme(
|
2021-09-13 10:17:04 +06:30
|
|
|
border: InputBorder.none,
|
2024-01-25 17:40:35 +06:30
|
|
|
hintStyle: TextStyle(color: Colors.grey, fontSize: 14)),
|
2021-09-13 11:10:17 +06:30
|
|
|
textTheme: TextTheme(
|
2024-01-25 17:40:35 +06:30
|
|
|
displayLarge: TextStyle(
|
|
|
|
|
color: theme.primaryTextTheme.displayLarge?.color,
|
2021-09-13 11:10:17 +06:30
|
|
|
fontSize: 16,
|
|
|
|
|
backgroundColor: primaryColor)),
|
2020-09-15 07:13:41 +06:30
|
|
|
primaryColor: primaryColor,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
List<Widget> buildActions(BuildContext context) {
|
|
|
|
|
return [
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: Icon(Icons.clear),
|
|
|
|
|
onPressed: () => query = '',
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget buildLeading(BuildContext context) {
|
|
|
|
|
return IconButton(
|
2024-01-25 17:40:35 +06:30
|
|
|
icon: Icon(CupertinoIcons.back),
|
2021-09-10 16:33:52 +06:30
|
|
|
onPressed: () => close(context, new User()),
|
2020-09-15 07:13:41 +06:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget buildResults(BuildContext context) {
|
|
|
|
|
final packageModel = Provider.of<PackageModel>(context);
|
|
|
|
|
return FutureBuilder(
|
|
|
|
|
future: packageModel.searchUser(query),
|
|
|
|
|
builder: (context, AsyncSnapshot<List<User>> snapshot) {
|
|
|
|
|
if (snapshot.hasData) {
|
2021-09-10 16:33:52 +06:30
|
|
|
if (snapshot.data == null || snapshot.data!.length == 0) {
|
2020-09-15 07:13:41 +06:30
|
|
|
return Container(
|
|
|
|
|
child: Center(
|
|
|
|
|
child: Text(
|
|
|
|
|
"No result found",
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return Container(
|
|
|
|
|
padding: EdgeInsets.only(top: 15),
|
|
|
|
|
child: ListView(
|
2021-09-10 16:33:52 +06:30
|
|
|
children: snapshot.data!
|
2020-09-15 07:13:41 +06:30
|
|
|
.map((u) => UserListRow(
|
|
|
|
|
user: u,
|
2021-01-10 15:56:27 +06:30
|
|
|
onUserRowSelect: (u) => _onUserRowSelect(context, u),
|
2020-09-15 07:13:41 +06:30
|
|
|
))
|
|
|
|
|
.toList(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
|
return Container(
|
|
|
|
|
child: Center(
|
|
|
|
|
child: Text(
|
|
|
|
|
'${snapshot.error}',
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return Container(
|
|
|
|
|
child: Center(
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
valueColor:
|
|
|
|
|
new AlwaysStoppedAnimation<Color>(primaryColor)),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget buildSuggestions(BuildContext context) {
|
|
|
|
|
return Container(
|
|
|
|
|
child: Center(
|
|
|
|
|
child: Opacity(
|
|
|
|
|
opacity: 0.2,
|
2020-09-18 04:04:21 +06:30
|
|
|
child: Icon(Icons.perm_identity, size: 200, color: primaryColor)),
|
2020-09-15 07:13:41 +06:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-01-10 15:56:27 +06:30
|
|
|
|
|
|
|
|
_onUserRowSelect(BuildContext context, User user) {
|
|
|
|
|
if (onUserSelect != null) {
|
2021-09-10 14:29:55 +06:30
|
|
|
onUserSelect!(user);
|
2021-01-10 15:56:27 +06:30
|
|
|
}
|
|
|
|
|
if (popPage) {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-15 07:13:41 +06:30
|
|
|
}
|