我的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)。
答案 0 :(得分:0)
将_callBack
方法定义为static
:
static void _callBack(int n){
callback(n);
}
然后使用您的onPressed
方法:
CardMgmt._callBack(8);
答案 1 :(得分:0)
我通过将函数回调作为类的属性传递给 _ExpandableListViewState 解决了该问题:
final String title;
final Function cbk;
const ExpandableListView({Key key, this.title, this.cbk}) : super(key: key);
在需要回调的地方使用onTap()
widget.cbk(12);
它奏效了。 :-)