new*_*bie 3 setstate flutter bottom-sheet
我有一个按钮,点击我调用下面的函数,它在下面创建一个底部。发生的事情是它根据列表数组值构建一个卡片列表。然后我想根据 onTap 手势更新其值的卡片之一。我注意到它检测到手势但没有更新它的值。最重要的是,我有一个全局变量定义为
String _localPNumberSelected="";
Run Code Online (Sandbox Code Playgroud)
在 onTap 我调用 setState
setState(() {
_localPNumberSelected=vList[index]["pNumber"].toString();
});
Run Code Online (Sandbox Code Playgroud)
但这不会更新
children: [
new Text('$_localPNumberSelected'),
],
Run Code Online (Sandbox Code Playgroud)
这是完整的代码。
void _callQuickListModalBottomSheet(context){
Future<void> future = showModalBottomSheet<void>(
context: context,
builder: (BuildContext bc){
return Container(
height: 280,
margin: new EdgeInsets.fromLTRB(200, 0, 10, 0),
child : new Container(
decoration: new BoxDecoration(
color: Color.fromRGBO(36, 46, 66, 1),
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(20),
topRight: const Radius.circular(20)
),
),
child: new Column(
children: <Widget>[
new Container(
margin: new EdgeInsets.fromLTRB(10, 10, 10, 0),
height:45,
child: new Column(
children: <Widget>[
new Card(
color: Colors.white,
child: Row (
children: <Widget>[
new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
decoration: new BoxDecoration(
color: Colors.green,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(5),
bottomLeft: const Radius.circular(5)
),
),
child: Icon(Icons.check, size: 20.0),
height: 27,
width: 30,
),
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
new Text('$_localPNumberSelected'),
],
),
],
)
)
]
)
),
new Container(
height:190,
child:
new ListView.builder(
shrinkWrap: true,
itemCount: vehicleListdata.length,
itemBuilder: (BuildContext ctxt, int index) {
return Container(
margin: new EdgeInsets.fromLTRB(10, 0, 10, 0),
width: 30.0,
height: 35.0,
child: Card(
color: Colors.white,
child: Row (
children: <Widget>[
new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
decoration: new BoxDecoration(
color: Colors.amber,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(5),
bottomLeft: const Radius.circular(5)
),
),
height: 27,
width: 10,
),
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
new GestureDetector(
onTap: () {
setState(() {
_localPNumberSelected=vList[index]["pNumber"].toString();
});
doSomething(vList[index]["pNumber"].toString());
},
child:new Text(vList[index]["pNumber"].toString()),
),
],
),
],
),
)
);
}
)
)
],
),
)
);
}
);
future.then((void value) => _closeModal(value));
}
Run Code Online (Sandbox Code Playgroud)
Sil*_*der 29
见示例
String update = '??';
///????
void locationSheet() {
// showModalBottomSheet(
// context: context,
// builder: (BuildContext context) {
// return LocationPicker();
// });
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(builder: (context,state){
return Center(
child: FlatButton(
onPressed: () {
updated(state);
},
child: new Text('$update')),
);
});
});
}
Future<Null> updated(StateSetter updateState) async {
updateState(() {
update = '???';
});
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以简单地返回一个有状态的构建器,该构建器又返回您尝试调用的小部件。
showModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState /*You can rename this!*/) {
return Container(
height: heightOfModalBottomSheet,
child: RaisedButton(onPressed: () {
setState(() {
heightOfModalBottomSheet += 10;
});
}),
);
});
Run Code Online (Sandbox Code Playgroud)
});
https://www.codegrepper.com/code-examples/whatever/how+to+change+state+of+Bottomsheet+in+flutter
链接到原始答案 - ShahbazAli(2020 年 11 月 5 日)。
| 归档时间: |
|
| 查看次数: |
5686 次 |
| 最近记录: |