Skip to content

Commit

Permalink
dart 3 sealed class to github_search example
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed May 11, 2024
1 parent 4f3274f commit da93c7c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SearchResultWidget extends StatelessWidget {
final item = items[index];

return InkWell(
key: ValueKey(item.url),
onTap: () => showItem(context, item),
child: Container(
alignment: FractionalOffset.center,
Expand Down
5 changes: 4 additions & 1 deletion examples/flutter/github_search/lib/search_state.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter/foundation.dart';

import 'github_api.dart';

// The State represents the data the View requires. The View consumes a Stream
Expand All @@ -9,7 +11,8 @@ import 'github_api.dart';
//
// The State Stream responds to input from the View by accepting a
// Stream<String>. We call this Stream the onTextChanged "intent".
class SearchState {}
@immutable
sealed class SearchState {}

class SearchLoading extends SearchState {}

Expand Down
23 changes: 11 additions & 12 deletions examples/flutter/github_search/lib/search_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,17 @@ class SearchScreenState extends State<SearchScreen> {
}

Widget _buildChild(SearchState state) {
if (state is SearchNoTerm) {
return const SearchIntro();
} else if (state is SearchEmpty) {
return const EmptyWidget();
} else if (state is SearchLoading) {
return const LoadingWidget();
} else if (state is SearchError) {
return const SearchErrorWidget();
} else if (state is SearchPopulated) {
return SearchResultWidget(items: state.result.items);
switch (state) {
case SearchNoTerm():
return const SearchIntro();
case SearchEmpty():
return const EmptyWidget();
case SearchLoading():
return const LoadingWidget();
case SearchError():
return const SearchErrorWidget();
case SearchPopulated():
return SearchResultWidget(items: state.result.items);
}

throw Exception('${state.runtimeType} is not supported');
}
}

0 comments on commit da93c7c

Please sign in to comment.