Skip to content

Instantly share code, notes, and snippets.

@AnthonyDS
Last active February 21, 2023 02:50
Show Gist options
  • Save AnthonyDS/5b89a235df932652f6c57d8274fb63f6 to your computer and use it in GitHub Desktop.
Save AnthonyDS/5b89a235df932652f6c57d8274fb63f6 to your computer and use it in GitHub Desktop.
Counter example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'IndexedStack Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'IndexedStack'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int stackIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: GestureDetector(
onTap: () {
setState(() {
stackIndex = stackIndex < 2 ? stackIndex + 1 : 0;
});
},
onLongPress: () {
setState(() {
stackIndex = stackIndex > 0 ? stackIndex - 1 : 2;
});
},
child: IndexedStack(
index: stackIndex,
children: [
Container(
color: Colors.red,
),
Container(
color: Colors.green,
),
Container(
color: Colors.blue,
),
],
),
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment