\n","author":{"@type":"Person","name":"RuslanBek"},"upvoteCount":0,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"
you can try the following, using a card, an AnimatedPositioned, and some more widgets
\nimport 'dart:developer';\n\nimport 'package:flutter/material.dart';\n\nvoid main() {\n runApp(MyApp());\n}\n\nclass MyApp extends StatelessWidget {\n // This widget is the root of your application.\n @override\n Widget build(BuildContext context) {\n return MaterialApp(\n title: 'Stack Demo',\n theme: ThemeData(\n primarySwatch: Colors.blue,\n visualDensity: VisualDensity.adaptivePlatformDensity,\n ),\n home: MyHomePage(title: 'Stack Demo'),\n );\n }\n}\n\nclass MyHomePage extends StatefulWidget {\n MyHomePage({Key key, this.title}) : super(key: key);\n\n final String title;\n\n @override\n _MyHomePageState createState() => _MyHomePageState();\n}\n\nclass _MyHomePageState extends State<MyHomePage> {\n int _counter = 0;\n bool showed = false;\n double incrementBoxHeight = 50;\n\n void incrementCounter() {\n setState(() {\n _counter++;\n });\n }\n\n void decrementCounter() {\n setState(() {\n if (_counter > 0) {\n _counter--;\n }\n });\n }\n\n void togleDetail() {\n setState(() {\n showed = !showed;\n });\n }\n\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n title: Text(widget.title),\n ),\n body: Center(\n child: Card(\n child: Stack(\n children: [\n Image(image: AssetImage('asset/img/indice.jpeg')),\n AnimatedPositioned(\n duration: Duration(seconds: 1),\n bottom: showed ? incrementBoxHeight : 0,\n right: 0,\n left: 0,\n child: GestureDetector(\n onTap: togleDetail,\n child: Container(\n height: 50, //you can do it with mediaQuery\n decoration: BoxDecoration(\n boxShadow: [\n BoxShadow(\n color: Colors.black,\n blurRadius: 5.0,\n spreadRadius: 2.0,\n ),\n ],\n color: Colors.blueAccent,\n borderRadius: BorderRadius.only(\n topLeft: Radius.circular(10),\n topRight: Radius.circular(10),\n ),\n ),\n child: Row(\n children: [\n SizedBox(\n width: 10,\n ),\n Column(\n children: [\n Text(\n "Topt",\n style: TextStyle(fontSize: 20),\n ),\n Text(\n "8 TMT",\n style: TextStyle(fontSize: 20, color: Colors.red),\n )\n ],\n ),\n ],\n ),\n ),\n ),\n ),\n Visibility(\n visible: showed,\n child: Positioned(\n bottom: 0,\n right: 0,\n left: 0,\n child: Container(\n height: incrementBoxHeight, //you can do it with mediaQuery\n decoration: BoxDecoration(\n boxShadow: [\n BoxShadow(\n color: Colors.black,\n blurRadius: 5.0,\n spreadRadius: 2.0,\n ),\n ],\n color: Colors.blueAccent,\n borderRadius: BorderRadius.only(\n topLeft: Radius.circular(10),\n topRight: Radius.circular(10),\n ),\n ),\n child: Row(\n mainAxisAlignment: MainAxisAlignment.spaceAround,\n children: [\n GestureDetector(\n onTap: decrementCounter,\n child: CircleAvatar(\n child: Icon(\n Icons.remove,\n size: 30,\n ),\n ),\n ),\n Text(\n _counter.toString(),\n style: TextStyle(fontSize: 48),\n ),\n GestureDetector(\n onTap: incrementCounter,\n child: CircleAvatar(\n child: Icon(\n Icons.add,\n size: 30,\n )),\n )\n ],\n ),\n ),\n ),\n ),\n ],\n )),\n ),\n );\n }\n}\n\n\n
\nand here is the result, you can edit to achieve your ui, because this is just a general structure of your interface!
\nReputation: 2009
What i want to achieve is to stack one widget onto another one with smooth animation when button pressed. Below is a screenshot of what i am trying to achieve (from left to right). Appreciate any help!
Upvotes: 0
Views: 1238
Reputation: 149
you can try the following, using a card, an AnimatedPositioned, and some more widgets
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stack Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Stack Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool showed = false;
double incrementBoxHeight = 50;
void incrementCounter() {
setState(() {
_counter++;
});
}
void decrementCounter() {
setState(() {
if (_counter > 0) {
_counter--;
}
});
}
void togleDetail() {
setState(() {
showed = !showed;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Card(
child: Stack(
children: [
Image(image: AssetImage('asset/img/indice.jpeg')),
AnimatedPositioned(
duration: Duration(seconds: 1),
bottom: showed ? incrementBoxHeight : 0,
right: 0,
left: 0,
child: GestureDetector(
onTap: togleDetail,
child: Container(
height: 50, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
children: [
SizedBox(
width: 10,
),
Column(
children: [
Text(
"Topt",
style: TextStyle(fontSize: 20),
),
Text(
"8 TMT",
style: TextStyle(fontSize: 20, color: Colors.red),
)
],
),
],
),
),
),
),
Visibility(
visible: showed,
child: Positioned(
bottom: 0,
right: 0,
left: 0,
child: Container(
height: incrementBoxHeight, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
onTap: decrementCounter,
child: CircleAvatar(
child: Icon(
Icons.remove,
size: 30,
),
),
),
Text(
_counter.toString(),
style: TextStyle(fontSize: 48),
),
GestureDetector(
onTap: incrementCounter,
child: CircleAvatar(
child: Icon(
Icons.add,
size: 30,
)),
)
],
),
),
),
),
],
)),
),
);
}
}
and here is the result, you can edit to achieve your ui, because this is just a general structure of your interface!
Upvotes: 2