IndexedStack 组件的前后重叠,通过索引的变化,显示不同的组件。
但是这个和PageView不一样,不能左右划动。
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _widgetIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: const Icon(Icons.shopping_cart),
tooltip: 'Open shopping cart',
onPressed: () {
setState(() {
_widgetIndex++;
if (_widgetIndex == 3) _widgetIndex = 0;
});
},
),
],
),
body: Container(
child: Center(
child: IndexedStack(
index: _widgetIndex,
children: [
Image.network(
"http://inews.gtimg.com/newsapp_bt/0/13580380942/641",
width: 300,
),
Image.network(
"http://inews.gtimg.com/newsapp_bt/0/13580380912/641",
width: 300,
),
Image.network(
"http://inews.gtimg.com/newsapp_bt/0/13580380916/641",
width: 300,
),
],
),
),
),
);
}
}