我正在使用全局键从父级调用子小部件的方法。我发现了这里选项 2 中提到的这种方法。 https://medium.com/flutter-community/flutter-communication-between-widgets-f5590230df1e
以下不是确切的代码,而是正在使用的代码片段。即使从父级调用 chid 中的搜索方法也不会被调用。
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
TabController _tabController;
bool showFab = true;
SearchState searchState = SearchState.SHOW_MENU;
TextEditingController _searchQuery;
bool _isSearching = false;
String searchQuery = "Search query";
GlobalKey<CustomersScreenState> _customerScreenState1 = GlobalKey();
void updateSearchQuery(String newQuery) {
setState(() {
searchQuery = newQuery;
});
print("search query " + newQuery); //This is getting printed
_customerScreenState1.currentState.search(newQuery); //Nothing happening here. not getting called
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.7,
leading: _isSearching ? const BackButton() : null,
title: _isSearching ? _buildSearchField() : _buildTitle(context),
actions: _buildActions(),
bottom: TabBar(
controller: _tabController,
indicatorColor: Colors.white,
tabs: <Widget>[
Tab(icon: Icon(Icons.camera_alt)),
Tab(text: "CHATS"),
Tab(
text: "STATUS",
),
Tab(
text: "CALLS",
),
],
)),
body: TabBarView(
controller: _tabController,
children: <Widget>[
CustomersScreen(key: _customerScreenState1)
],
),
}
class CustomersScreen extends StatefulWidget {
const CustomersScreen({Key key}) : super(key: key);
@override
CustomersScreenState createState() => CustomersScreenState();
}
class CustomersScreenState extends State<CustomersScreen> {
Firestore fireStore;
List<Customer> customers = [];
bool isLoading = false; // track if products fetching
bool hasMore = true; // flag for more products available or not
int documentLimit = 10; // documents to be fetched per request
DocumentSnapshot
lastDocument; // flag for last document from where next 10 records to be fetched
ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
fireStore = Firestore.instance;
_scrollController.addListener(() {
double maxScroll = _scrollController.position.maxScrollExtent;
double currentScroll = _scrollController.position.pixels;
double delta = MediaQuery.of(context).size.height * 0.20;
if (maxScroll - currentScroll <= delta) {
getCustomers();
}
});
getCustomers();
}
void search(Sting query){
//This method is not getting called.
}
}
编辑1:
void updateSearchQuery(String newQuery) {
setState(() {
searchQuery = newQuery;
print("search query " + newQuery);
_customerScreenState1.currentState.search(newQuery); //moved inside setState still not working
});
}