我想要的是:
我的小部件:
class Buttons extends StatelessWidget {
const Buttons({
Key key,
@required this.selectBloc,
@required this.selectStatus,
}) : super(key: key);
final SelectBloc selectBloc;
final int selectStatus;
@override
Widget build(BuildContext context) {
return BlocBuilder<SelectBloc, SelectState>(
bloc: selectBloc,
builder: (context, state) {
return OutlinedButton(
onPressed: () {
},
child: Text( dynamic, style: TextStyle(color: Colors.grey[500])),
style: ButtonStyle(
side: MaterialStateProperty.all(BorderSide(
color: state.statusSelected == selectStatus
? Colors.blue
: Colors.grey)),
),
);
});
}
}
我用這個:
Buttons(
selectBloc: selectBloc,
selectStatus: 0,
),
Buttons(
selectBloc: selectBloc,
selectStatus: 1,
),
uj5u.com熱心網友回復:
您應該final Function() onTap
在Buttons
. 然后在定義按鈕的地方實作它。
我的意思是這樣的:
class Buttons extends StatelessWidget {
const Buttons({
Key key,
@required this.selectBloc,
@required this.isSelecteed,
@required this.onTap,
}) : super(key: key);
final SelectBloc selectBloc;
final bool isSelecteed;
final Function() onTap;
@override
Widget build(BuildContext context) {
return BlocBuilder<SelectBloc, SelectState>(
bloc: selectBloc,
builder: (context, state) {
return OutlinedButton(
onPressed: onTap,
child: Text( dynamic, style: TextStyle(color: Colors.grey[500])),
style: ButtonStyle(
side: MaterialStateProperty.all(BorderSide(
color: isSelecteed
? Colors.blue
: Colors.grey)),
),
);
});
}
}
然后:
Buttons(
selectBloc: selectBloc,
isSelecteed: 1 == selectedIndex,
onTap:(){
selectedIndex = 1;
setState((){});
}
),
我希望你明白這一點:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529303.html
標籤:扑