//注冊數(shù)據(jù)監(jiān)聽器 <> 內(nèi)是數(shù)據(jù)類型 你想要的類型 ()內(nèi)是默認值
ValueNotifier<bool> _valueNotifier = ValueNotifier<bool>(false);
@override
Widget buildWidget(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: _valueNotifier,
builder: (context, value, child) {
return Visibility(
visible: value == true,
child: Container(
color: baseColor(),
width: getScreenWidth(),
height: MediaQuery.of(context).padding.top,
),
);
},
);
}
改變值 value可以是任何值 看你定義的值是什么
XXBaseBtn(
title: '點擊',
ontap: () {
_valueNotifier.value = true;
},
)
全部代碼 model
class XXValueNotifierTest extends StatefulWidget {
XXValueNotifierTest({Key key}) : super(key: key);
@override
_XXValueNotifierTestState createState() => _XXValueNotifierTestState();
}
class _XXValueNotifierTestState extends State<XXValueNotifierTest> {
//注冊數(shù)據(jù)監(jiān)聽器 <> 內(nèi)是數(shù)據(jù)類型 你想要的類型 ()內(nèi)是默認值
ValueNotifier<bool> _valueNotifier = ValueNotifier<bool>(false);
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
ValueListenableBuilder<bool>(
valueListenable: _valueNotifier,
builder: (context, value, child) {
return Visibility(
visible: value == true,
child: Container(
color: baseColor(),
),
);
},
),
XXBaseBtn(
title: '點擊',
ontap: () {
_valueNotifier.value = true;
},
)
],
),
);
}
}