0

我正在使用全局键从父级调用子小部件的方法。我发现了这里选项 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
    });
  }
4

2 回答 2

0

我已将答案更改为您想要做的工作示例。我在这和你的代码之间看到的唯一区别是你的 chid 小部件没有构建功能,我想知道这是否会干扰你的状态。

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  GlobalKey<_ChildWidgetState> _childKey = GlobalKey<_ChildWidgetState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ChildWidget(key: _childKey),
            RaisedButton(
              onPressed: () => callChildFunction(),
              child: Text('Print'),
            )
          ],
        ),
      ),
    );
  }

  void callChildFunction(){
    _childKey.currentState.changeChildText();
  }
}

class ChildWidget extends StatefulWidget {
  ChildWidget({Key key}): super(key: key);

  @override
  _ChildWidgetState createState() => _ChildWidgetState();
}

class _ChildWidgetState extends State<ChildWidget> {
  String text = 'Child widget';

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(text),
    );
  }

  void changeChildText(){
    setState(() {
      text = 'called function';
    });
  }
}
于 2020-01-05T21:22:57.110 回答
0

很难说只有片段。我认为search方法是使用您的方法触发的,但您的 UI 可能没有任何区别,因为您没有在search方法运行后触发构建方法。只需尝试在课堂上调用_customerScreenState1.currentState.search(newQuery);内部setState回调即可。_HomeState

void updateSearchQuery(String newQuery) {
setState(() {
  searchQuery = newQuery;
   _customerScreenState1.currentState.search(newQuery);
  });
  print("search query " + newQuery); //This is getting printed
}
于 2020-01-05T22:15:13.960 回答