如何从flutter中的状态类访问根类中的方法

问题描述 投票:0回答:2

我的dart文件中有三个类: CardMgmt是我的根类,是无状态的, ExpandableListView是有状态的,_ExpandableListViewState是其父(ExpandableListView)的状态类。 我需要从_ExpandableListViewState访问CardMgmt中的方法。 这是我的代码:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/pages/home_page.dart';
import 'package:flutter_app/model/BankCard.dart';


class CardMgmt extends StatelessWidget {

  Function callback;
  //final VoidCallback callback;
  CardMgmt(this.callback);

  callBack(int n){
    callback(n);
  }



  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Color(0xFF421000),
      appBar: new PreferredSize(child: new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            new GestureDetector(onTap: (){
              _callBack(11);
            }, child:
            Icon(Icons.add_circle_outline, size: 50, color: Colors.white)),
            new Icon(Icons.autorenew, size:50, color: Colors.white)
          ]), preferredSize: Size.fromHeight(40)),
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return ExpandableListView(title: " کارت $index");
        },
        itemCount: 10,
      )
    );
  }

}

class ExpandableListView extends StatefulWidget {

  final String title;
  const ExpandableListView({Key key, this.title}) : super(key: key);

  @override
  _ExpandableListViewState createState() => new _ExpandableListViewState();
}

class _ExpandableListViewState extends State<ExpandableListView> {

  bool expandFlag = false;
  final List<BankCard> items = new List();

  void _showDialog(BuildContext con) {
    showDialog(
        context: con,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(32.0))),
            title: Center(child: Text('هشدار')),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  child: Text(
                    "آیا می خواهید کارت را حذف کنید؟",
                    textAlign: TextAlign.start,
                    style: TextStyle(
                        color: Colors.blue,
                        fontSize: 18,
                        fontWeight: FontWeight.w700),
                  ),
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      FlatButton(
                          child: Text('بله', ),
                          onPressed: () {
                            Navigator.of(context).pop();

                          }),
                      FlatButton(
                          child: Text('خیر'),
                          onPressed: () {
                            Navigator.of(context).pop();
                          })
                    ])
              ],
            ),
          );
        });
  }



  @override
  Widget build(BuildContext context) {
        return new Container(
            margin: new EdgeInsets.symmetric(vertical: 1.0),
            child: new Column(
              children: <Widget>[
                new GestureDetector(
                    onTap: () {
                      setState(() {
                        expandFlag = !expandFlag;
                      });
                    },
                    child: new Container(
                      //this is fixed container
                        height: 100,
                        width: 290,
                        margin: EdgeInsets.only(top: 15),
                        decoration: new BoxDecoration(

                          borderRadius: new BorderRadius.all(Radius.circular(12.0)),
                          color: Colors.white,
                        ),
                        padding: new EdgeInsets.symmetric(horizontal: 5.0),
                        child: new Row(textDirection: TextDirection.ltr,

                            children: <Widget>[
                              Expanded(
                                  child: new Container(
                                      alignment: Alignment.center,
                                      child: new Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: <Widget>[
                                            new Text('کارت سپردم',
                                                style: new TextStyle(
                                                    fontSize: 17.0,
                                                    fontWeight: FontWeight.bold)),
                                            new Text('6104-6587-5841-9955',
                                                style: new TextStyle(
                                                    fontSize: 17.0,
                                                    fontWeight: FontWeight.bold)),
                                            new Text('1254000 ريال'),
                                          ]))),
                              new Container(
                                //width: 25,
                                //color: Colors.red,
                                //margin: EdgeInsets.all(0.0),
                                  alignment: Alignment.centerRight,
                                  child: new Image.asset(
                                      'assets/4.0x/maskanlogoxxxhdpi.png',
                                      width: 40))
                            ]))),
                new ExpandableContainer(
                  //this is expandable container
                    expanded: expandFlag,
                    child: new SingleChildScrollView(
                        child: new Container(
                            height: 200,
                            width: 300,
                            margin: EdgeInsets.only(top: 0.5, bottom: 5),
                            decoration: new BoxDecoration(

                              borderRadius: new BorderRadius.all(Radius.circular(20.0)),
                              color: Colors.white,
                            ),

                            child: new Row(textDirection: TextDirection.ltr,

                                children: <Widget>[
                                  Expanded(
                                      child: new Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          textDirection: TextDirection.rtl,
                                          children: <Widget>[
                                            new FlatButton.icon(
                                              onPressed: () {
                                                _onTapItem(context);
                                              },
                                              icon: new Icon(Icons.do_not_disturb_alt),
                                              label: new Text('غیرفعال کردن'),
                                              padding: EdgeInsets.only(left: 0),
                                            ),
                                            new FlatButton.icon(
                                              onPressed: () {
                                                _onTapItem(context);
                                              },
                                              icon: new Icon(Icons.clear),
                                              label: new Text('مسدود نمودن'),
                                              padding: EdgeInsets.only(left: 0),
                                            ),
                                            new FlatButton.icon(
                                              onPressed: () {
                                                _onTapItem(context);
                                              },
                                              icon: new Icon(Icons.refresh),
                                              label: new Text('فعال سازی'),
                                              padding: EdgeInsets.only(left: 0),
                                            )
                                          ])),
                                  // Expanded(child:
                                  new Column(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      textDirection: TextDirection.rtl,
                                      children: <Widget>[
                                        new FlatButton.icon(
                                            onPressed: () {
                                              _showDialog(context);
                                            },
                                            icon: new Icon(Icons.delete),
                                            label: new Text('حذف کارت')),
                                        new FlatButton.icon(
                                            onPressed: () {

                                              //Here I need to call _callBack()
                                            },
                                            icon: new Icon(Icons.edit),
                                            label: new Text('ویرایش کارت')),
                                        new FlatButton.icon(
                                            onPressed: () {
                                              _onTapItem(context);
                                            },
                                            icon: new Icon(Icons.update),
                                            label: new Text('بروزرسانی سرویس ها')),
                                        new FlatButton.icon(
                                            onPressed: () {
                                              _onTapItem(context);
                                            },
                                            icon: new Icon(Icons.check),
                                            label: new Text('تغییر حساب پیشفرض')),
                                      ])

                                  //  )
                                ]))
                    )
                )
              ]
            )
    );

  }

  void _onTapItem(BuildContext context) {
    Scaffold.of(context)
        .showSnackBar(new SnackBar(content: new Text('item clicked')));
  }
}



我需要在我评论过的地方调用callBack(int n)。

callback dart flutter
2个回答
1
投票

我通过将Function回调传递给_ExpandableListViewState作为类的属性来解决了这个问题:

final String title;
  final Function cbk;
  const ExpandableListView({Key key, this.title, this.cbk}) : super(key: key);

并使用onTap()我需要做回调:

widget.cbk(12);

它工作了。 :-)


0
投票

_callBack方法定义为static

 static void _callBack(int n){
  callback(n);
 }

然后在你的onPressed方法:

 CardMgmt._callBack(8);
© www.soinside.com 2019 - 2024. All rights reserved.