Created with <3 with dartpad.dev.
Last active
February 21, 2023 02:50
-
-
Save AnthonyDS/5b89a235df932652f6c57d8274fb63f6 to your computer and use it in GitHub Desktop.
Counter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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