Jisho-Study-Tool/lib/screens/search.dart

104 lines
2.9 KiB
Dart
Raw Normal View History

2020-07-09 22:17:10 +02:00
import 'package:flutter/material.dart';
2020-08-19 18:25:45 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:jisho_study_tool/bloc/search/search_bloc.dart';
import 'package:jisho_study_tool/components/loading.dart';
import 'package:jisho_study_tool/components/search/search_card.dart';
2020-07-09 22:17:10 +02:00
class SearchView extends StatelessWidget {
2020-08-19 18:25:45 +02:00
@override
Widget build(BuildContext context) {
return BlocListener<SearchBloc, SearchState>(
2020-08-24 23:46:04 +02:00
listener: (context, state) {},
child: BlocBuilder<SearchBloc, SearchState>(
builder: (context, state) {
if (state is SearchInitial)
return _InitialView();
else if (state is SearchLoading)
return LoadingScreen();
else if (state is SearchFinished) {
return WillPopScope(
child: ListView(
children: state.results
.map((result) => SearchResultCard(result))
.toList(),
),
onWillPop: () async {
BlocProvider.of<SearchBloc>(context)
.add(ReturnToInitialState());
print('Popped');
return false;
},
);
}
throw 'No such event found';
},
));
2020-08-19 18:25:45 +02:00
}
}
class _InitialView extends StatelessWidget {
2020-07-09 22:17:10 +02:00
@override
Widget build(BuildContext context) {
2020-08-24 23:46:04 +02:00
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
SearchBar(),
]);
2020-07-09 22:17:10 +02:00
}
2020-07-13 21:38:24 +02:00
}
class _LanguageOption extends StatelessWidget {
final String _language;
final Color _color;
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Center(child: Text(_language)),
decoration: BoxDecoration(
2020-08-24 23:46:04 +02:00
border: Border.all(
color: Colors.black,
width: 1.0,
),
color: _color),
2020-07-13 21:38:24 +02:00
),
);
}
_LanguageOption(this._language, this._color);
}
class SearchBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
children: [
TextField(
2020-08-24 23:46:04 +02:00
onSubmitted: (text) => BlocProvider.of<SearchBloc>(context)
.add(GetSearchResults(text)),
2020-07-13 21:38:24 +02:00
controller: TextEditingController(),
decoration: InputDecoration(
labelText: 'Search',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
SizedBox(
height: 10.0,
),
Row(
children: [
2020-08-24 23:46:04 +02:00
_LanguageOption('Auto', Colors.white),
2020-07-13 21:38:24 +02:00
_LanguageOption('English', Colors.white),
_LanguageOption('Japanese', Colors.blue),
],
),
],
),
);
}
2020-08-24 23:46:04 +02:00
}