Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created March 21, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/e1582dff8384c7aa6ffea7640d7b8db2 to your computer and use it in GitHub Desktop.
Save graphicbeacon/e1582dff8384c7aa6ffea7640d7b8db2 to your computer and use it in GitHub Desktop.
Slideout container
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Page()
)
));
class Page extends StatefulWidget {
@override
PageState createState() => PageState();
}
class PageState extends State<Page> {
double left = 0;
double leftStart;
final double startW = 100;
double initW = 100;
double containerWidth = 100;
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Positioned(
left: left,
child: GestureDetector(
onHorizontalDragStart: (details) {
setState(() {
leftStart = details.globalPosition.dx;
});
},
onHorizontalDragUpdate: (details) {
var offsetWidth = details.globalPosition.dx - leftStart;
var updatedWidth = (offsetWidth + initW).roundToDouble();
var screenWidth = MediaQuery.of(context).size.width;
setState(() {
if (updatedWidth < startW) {
containerWidth = startW;
return;
}
if (updatedWidth > screenWidth) {
containerWidth = screenWidth;
return;
}
containerWidth = updatedWidth;
});
},
onHorizontalDragEnd: (details) {
setState(() {
initW = containerWidth.roundToDouble();
});
},
child: Container(
width: containerWidth,
height: MediaQuery.of(context).size.height,
color: Colors.purple,
child: Text(containerWidth.toString())
)
)
)
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment