我如何从B类调用A类的setState()

时间:2019-12-02 19:52:18

标签: flutter dart

当我单击IconButton()从列表movies中删除所有项目时,直到再次重新打开页面,我才能看到该更改...

任何人都知道我该如何解决

这是我的infoPage((“ B类”)):

class InfoPage extends StatefulWidget {
  int id;
  int pageId;
  InfoPage(this.id,this.pageId);
  @override
  _InfoPageState createState() => _InfoPageState(id,pageId);
}

class _InfoPageState extends State<InfoPage> {
  var db = DatabaseHelper();
  String title = "";
  String about = "";
  String rate = "";
  String date = "";
  int id;
  int pageId;
  _InfoPageState(this.id,this.pageId);

  @override
  void initState() {
    super.initState();
    if(pageId == 1){
      _getMovie();
    }
  }
  void _getMovie() async {
    Movie thisMovie = await db.getMovie(id);
    setState(() {
      title = thisMovie.name;
      about = thisMovie.description;
      rate = thisMovie.rate;
      date = thisMovie.date;
    });
  }
  _deleteMovie() async{
    await db.deleteMovie(id);
    Navigator.pop(context);
    setState(() {
      CardsListViewState(pageId).deleteAllList();
    });
  }
  @override
  Widget build(BuildContext context) {
    Navigator.canPop(context);
    return Scaffold(
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 10),
              child:Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    child: Container(),
                  ),
                  Container(
                      margin: EdgeInsets.only(right: 10),
                      child: IconButton(
                          icon: Icon(Icons.delete,color: Color(0xffFC4D4D),),
                          onPressed: (){
                            _deleteMovie();
                          }
                      )
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

这是我的CardsListView((“ A类”))

class CardsListView extends StatefulWidget {
  int whereComeFrom;
  CardsListView(this.whereComeFrom);
  @override
  CardsListViewState createState() => CardsListViewState(whereComeFrom);
}

class CardsListViewState extends State<CardsListView> {
  int whereComeFrom;
  CardsListViewState(this.whereComeFrom);
  var db = DatabaseHelper();
  List mainList = [];
  final List<Movie> movies = <Movie>[];
  deleteAllList() async{
    await db.deleteMovies();
    setState(() {
      movies.clear();
    });
  }
         @override
   void initState() {
    super.initState();
    _readUnites();
    if(whereComeFrom == 1){
      mainList = movies;
    }
  }
@override
  Widget build(BuildContext context) {
    return Scaffold(
        body:
        GridView.count(
            crossAxisCount: 3,
            addAutomaticKeepAlives: true,
            childAspectRatio: (1/1.5),
            children: List.generate(mainList.length, (index){
              return CardUnite(mainList[index].name,mainList[index].id,whereComeFrom);
            })
        ),
    );
  }

1 个答案:

答案 0 :(得分:0)

您可以使用提供给子类的父类中的回调函数。
请记住,函数是Dart中的first class objects
只需将一个调用setState的函数传递给子对象,然后让子对象调用该函数即可。