Это пример того, что я понял! Это работает для == 100, a! = 00 и задержки по нажатию. Измените его как ваше требование!
double a = 100;
Widget focusedWidget;
@override
void initState() {
super.initState();
//if you want to predefine a initial widget call the initWidget method !
//or call the changeWidget for instant output
changeWidget(a);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Let\'s parse some JSON'),
),
body: Container(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
child: focusedWidget,
));
}
void initWidget(flag) {
if (flag == 100) {
setState(() {
focusedWidget = Card(
color: Colors.green,
child: ListTile(
onTap: () {
changeWidget(50);
//or some value
},
title: Text('Widget A',
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
textAlign: TextAlign.center)),
);
});
} else {
setState(() {
focusedWidget = Card(
color: Colors.red,
child: ListTile(
onTap: () {
changeWidget(100);
},
title: Text('Widget B',
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
textAlign: TextAlign.center)),
);
});
}
}
void changeWidget(flag) {
if (flag == 100) {
setState(() {
focusedWidget = Card(
color: Colors.red,
child: ListTile(
onTap: () {
changeWidget(100);
//or some value
},
title: Text('Widget B',
style:
TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
),
);
});
Future.delayed(Duration(seconds: 5), () {
setState(() {
focusedWidget = Card(
color: Colors.green,
child: ListTile(
onTap: () {
changeWidget(50);
},
title: Text('Widget A',
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
),
);
});
});
} else {
setState(() {
focusedWidget = Card(
color: Colors.red,
child: ListTile(
onTap: () {
changeWidget(100);
},
title: Text('Widget B',
style:
TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
),
);
});
}
}