-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor Search to widget and make tags clickable
- Loading branch information
1 parent
5acfa04
commit 1d6d332
Showing
7 changed files
with
164 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:studyu_designer_v2/theme.dart'; | ||
|
||
class Search extends StatefulWidget { | ||
final Function(String) onQueryChanged; | ||
final SearchController? searchController; | ||
final String? hintText; | ||
final String? initialText; | ||
|
||
const Search({ | ||
super.key, | ||
required this.onQueryChanged, | ||
this.searchController, | ||
this.hintText, | ||
this.initialText, | ||
}); | ||
|
||
@override | ||
SearchState createState() => SearchState(); | ||
} | ||
|
||
class SearchState extends State<Search> { | ||
late TextEditingController _searchController; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
widget.searchController?.setText = setText; | ||
_searchController = TextEditingController(text: widget.initialText); | ||
_searchController.addListener(_onSearchPressed); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(Search oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
} | ||
|
||
void _onSearchPressed() { | ||
String query = _searchController.text.toLowerCase(); | ||
widget.onQueryChanged(query); | ||
} | ||
|
||
void setText(String text) { | ||
setState(() { | ||
_searchController.text = text; | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final theme = Theme.of(context); | ||
return SizedBox( | ||
width: 400.0, | ||
child: SearchBar( | ||
hintText: widget.hintText ?? "Search", | ||
controller: _searchController, | ||
leading: const Icon(Icons.search), | ||
backgroundColor: MaterialStateProperty.resolveWith((states) { | ||
return ThemeConfig.sidesheetBackgroundColor(theme).withOpacity(0.5); | ||
}), | ||
) | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_searchController.removeListener(_onSearchPressed); | ||
_searchController.dispose(); | ||
} | ||
} | ||
|
||
class SearchController { | ||
late void Function(String text) setText; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.