gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:58:30 28 4
gpt4 key购买 nike

我的 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)。

最佳答案

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

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

然后在我需要回调的地方使用 onTap() :

widget.cbk(12);

它奏效了。 :-)

关于callback - 如何从 flutter 中的状态类访问根类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617359/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com