如果我選擇了該專案,我需要隱藏按鈕,直到我從串列中選擇一個專案我的按鈕應該是可見的
喜歡
bool visible = false
if(itemSelected is Selected) {
visible = true;
} else {
visible = false;
};
像這樣 ?
uj5u.com熱心網友回復:
像這樣使用
您可以使用VisibilityWidget . 而不是 dummycontainer或.thatsizedbox小部件有自己的功能或屬性。
在這里,我們在有狀態類之外創建簡單List<object>的,或者您可以使用widget.listobject(**當在變數內時:**所以每當單擊串列視圖時重新構建應用程式會重置 isshow 屬性,因此我們無法管理顯示和隱藏)。
每當我們點擊 listview 專案時,在element.isshow里面設定為 true 或 falsesetstate
var getdata = Diohelper.getdata();
class _staState extends State<sta> {
@override
Widget build(BuildContext context) {
List mwidge = [];
int index = 0;
getdata.forEach((element) {
mwidge.add(ListTile(
onTap: () {
setState(() {
element.isShow = element.isShow ? false : true;
});
},
hoverColor: Colors.amber,
title: Text(element.name.toString()),
trailing: element.isShow
? SizedBox(
width: 100,
height: 50,
child: OutlinedButton(onPressed: () {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(element.name.toString())));
}, child: Text("Show")),
)
: Container(
width: 100,
),
));
index ;
});
return GestureDetector(
child: Center(
child: Column(
children: [...mwidge],
),
),
);
}
}
全碼:
import 'package:flutter/material.dart';
void main() =>
runApp(MaterialApp(home: Scaffold(appBar: AppBar(), body: sta())));
class sta extends StatefulWidget {
const sta({Key? key}) : super(key: key);
@override
State<sta> createState() => _staState();
}
var isShow = false;
var getdata = Diohelper.getdata();
class _staState extends State<sta> {
@override
Widget build(BuildContext context) {
List mwidge = [];
int index = 0;
getdata.forEach((element) {
mwidge.add(ListTile(
onTap: () {
setState(() {
element.isShow = element.isShow ? false : true;
});
},
hoverColor: Colors.amber,
title: Text(element.name.toString()),
trailing: element.isShow
? SizedBox(
width: 100,
height: 50,
child: MaterialButton(onPressed: () {}, child: Text("Show")),
)
: Container(
width: 100,
),
));
index ;
});
return GestureDetector(
child: Center(
child: Column(
children: [...mwidge],
),
),
);
}
}
class productModel {
String? name;
String? price;
String? category;
bool isShow = false;
productModel({this.name, this.price, this.category, this.isShow = false});
productModel.fromJson(Map<String, dynamic> json) {
name = json['name'];
price = json['price'];
category = json['category'];
isShow = json['isShow'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['price'] = this.price;
data['category'] = this.category;
data['isShow'] = this.isShow;
return data;
}
}
class Diohelper {
static List<productModel> getdata() {
List<productModel> list = [];
list.add(productModel(name: "broast", price: "100", category: "chicken"));
list.add(productModel(name: "mandi", price: "100", category: "chicken"));
list.add(productModel(name: "mandi", price: "100", category: "veg"));
return list;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427327.html
下一篇:Flutter中的其他過濾器
