1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-21 13:37:29 +01:00

Add willpopscope to search page

This commit is contained in:
Oystein Kristoffer Tveit 2020-08-24 23:46:04 +02:00
parent c7f6f8c4b2
commit afe9243607
2 changed files with 38 additions and 31 deletions

View File

@ -28,7 +28,7 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
yield SearchError('Something went wrong'); yield SearchError('Something went wrong');
} }
} else if (event is ReturnToInitialState) { } else if (event is ReturnToInitialState) {
yield SearchInitial();
} }
} }
} }

View File

@ -8,33 +8,40 @@ class SearchView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocListener<SearchBloc, SearchState>( return BlocListener<SearchBloc, SearchState>(
listener: (context, state) { listener: (context, state) {},
}, child: BlocBuilder<SearchBloc, SearchState>(
child: BlocBuilder<SearchBloc, SearchState>( builder: (context, state) {
builder: (context, state) { if (state is SearchInitial)
if (state is SearchInitial) return _InitialView(); return _InitialView();
else if (state is SearchLoading) return LoadingScreen(); else if (state is SearchLoading)
else if (state is SearchFinished) { return LoadingScreen();
return ListView( else if (state is SearchFinished) {
children: state.results.map((result) => SearchResultCard(result)).toList(), 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';
},
));
} }
} }
class _InitialView extends StatelessWidget { class _InitialView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
mainAxisAlignment: MainAxisAlignment.center, SearchBar(),
children: [ ]);
SearchBar(),
]
);
} }
} }
@ -49,12 +56,11 @@ class _LanguageOption extends StatelessWidget {
padding: EdgeInsets.symmetric(vertical: 10.0), padding: EdgeInsets.symmetric(vertical: 10.0),
child: Center(child: Text(_language)), child: Center(child: Text(_language)),
decoration: BoxDecoration( decoration: BoxDecoration(
border: Border.all( border: Border.all(
color: Colors.black, color: Colors.black,
width: 1.0, width: 1.0,
), ),
color: _color color: _color),
),
), ),
); );
} }
@ -70,7 +76,8 @@ class SearchBar extends StatelessWidget {
child: Column( child: Column(
children: [ children: [
TextField( TextField(
onSubmitted: (text) => BlocProvider.of<SearchBloc>(context).add(GetSearchResults(text)), onSubmitted: (text) => BlocProvider.of<SearchBloc>(context)
.add(GetSearchResults(text)),
controller: TextEditingController(), controller: TextEditingController(),
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Search', labelText: 'Search',
@ -84,7 +91,7 @@ class SearchBar extends StatelessWidget {
), ),
Row( Row(
children: [ children: [
_LanguageOption('Auto', Colors.white), _LanguageOption('Auto', Colors.white),
_LanguageOption('English', Colors.white), _LanguageOption('English', Colors.white),
_LanguageOption('Japanese', Colors.blue), _LanguageOption('Japanese', Colors.blue),
], ],
@ -93,4 +100,4 @@ class SearchBar extends StatelessWidget {
), ),
); );
} }
} }