Flutter 在PageView不同类中重建/刷新特定小部件

Flutter 在PageView不同类中重建/刷新特定小部件,flutter,setstate,Flutter,Setstate,我有一个包含页面视图的主屏幕。pageview有三个子类,每个子类都是单独的类: children: <Widget>[ FirstScreen(), SecondScreen(), ThirdScreen(), 这些屏幕从数据库加载数据,是有状态的小部件。我从主屏幕上打开的一个单独的屏幕向该数据库添加数据 我想在将数据添加到数据库后触发这三个屏幕的setState方法 当这些屏幕位于不同的类中时,如何访问它们?您需要的是状态管理 确保业务逻辑/

我有一个包含页面视图的主屏幕。pageview有三个子类,每个子类都是单独的类:

children: <Widget>[
      FirstScreen(),
      SecondScreen(),
      ThirdScreen(),
这些屏幕从数据库加载数据,是有状态的小部件。我从主屏幕上打开的一个单独的屏幕向该数据库添加数据

我想在将数据添加到数据库后触发这三个屏幕的setState方法


当这些屏幕位于不同的类中时,如何访问它们?

您需要的是状态管理

确保业务逻辑/存储和小部件之间的通信有多种可能性

我可以推荐BLoC和BlocProvider进行状态管理。简单地说,它允许您的小部件对特定事件、应用程序中的状态更改做出反应,并将业务逻辑与视图分离


我添加这个来显示示例,为您的数据库修改这个

主屏幕

新屏幕

添加后


谢谢你的回答!不幸的是,使用类似BLoC的东西需要我重写应用程序的大部分内容&目前这不是一个选项。有没有办法只从另一个类访问一个特定的小部件而不使用任何第三方工具?如果这三个小部件具有相同的父小部件,您可以通过父小部件控制它们,例如提供VoidCallback、ValueChanged或其他定义的函数类型来传播信息。您也可以通过为顶部小部件中的每个小部件定义GlobalKey并通过.currentState访问它们的状态来实现。。或者,将侦听器应用于所需的类并跟踪参数更改。然而,我仍然建议继续进行州管理方案。链接:
class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  List<Map> list = [];

  @override
  void initState() {
    super.initState();
    list.add({'name': 'Raj', 'phone': '1234567890'});
    list.add(
      {'name': 'Naveen', 'phone': '1122334455'},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.black,
          title: Text('Main Screen', style: TextStyle(color: Colors.white)),
          actions: [
            IconButton(
                icon: Icon(Icons.add_circle, color: Colors.white),
                onPressed: () {
                  addNewItem();
                })
          ]),
      body: Container(
        padding:EdgeInsets.all(15),
          color: Colors.white,
          child: Center(
            child: ListView.builder(
                itemCount: list.length,
                itemBuilder: (con, ind) {
                  return ListTile(
                      title: Text(list[ind]['name'],
                          style: TextStyle(color: Colors.black)),
                      subtitle: Text(list[ind]['phone'],
                          style: TextStyle(color: Colors.black)));
                }),
          )),
    );
  }

  void addNewItem() async {
    Map newItem = await Navigator.push(
        context, MaterialPageRoute(builder: (cc) => NewScreen()));
    if (newItem != null) {
      setState(() {
        list.add(newItem);
      });
    }
  }
}
class NewScreen extends StatefulWidget {
  @override
  _NewScreenState createState() => _NewScreenState();
}

class _NewScreenState extends State<NewScreen> {
  //Declare your new item here....
  Map newItem;

  //I m using strigns, but use TextEditingControllers here...
  String name = '', phone = '';

  @override
  void initState() {
    super.initState();
    //database initialisation...
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: onBackPressed,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          title: Text('New Screen', style: TextStyle(color: Colors.white)),),
        body: Container(
          padding:EdgeInsets.all(15),
          color:Colors.white,
            child: Center(
                child: Column(
          children: <Widget>[
            TextField(
                decoration: InputDecoration(labelText: 'Name',
                                           labelStyle: TextStyle(color: Colors.black)),
                onChanged: (v) {
                  setState(() {
                    name = v;
                  });
                },
            style: TextStyle(color: Colors.black)),
            TextField(
                decoration: InputDecoration(labelText: 'Phone',
                                           labelStyle: TextStyle(color: Colors.black)),
                onChanged: (v) {
                  setState(() {
                    phone = v;
                  });
                },
            style: TextStyle(color: Colors.black)),
            RaisedButton(
                child: Text('Add'),
                onPressed: () {
                  if (name.isNotEmpty && phone.isNotEmpty) {
                    newItem = Map();
                    newItem['name'] = name;
                    newItem['phone'] = phone;
                    //You may close new screen if you want !
                    //But call sendNewItemBack();
                  }
                })
          ],
        ))),
      ),
    );
  }

  Future<bool> onBackPressed() async {
    sendNewItemBack();
  }

  sendNewItemBack() {
    //add new item to database
    Navigator.of(context).pop(newItem);
  }
}