Created
January 12, 2022 19:59
-
-
Save osaxma/c8831a85512a80bd07c012ae969e7cc4 to your computer and use it in GitHub Desktop.
this is for SO question: https://stackoverflow.com/q/70686375/10976714
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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const WindowApp()); | |
} | |
class WindowApp extends StatelessWidget { | |
const WindowApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: const Scaffold(body: SizeableWindow()), | |
); | |
} | |
} | |
class SizeableWindow extends StatefulWidget { | |
const SizeableWindow({Key? key}) : super(key: key); | |
@override | |
State<SizeableWindow> createState() => _SizeableWindowState(); | |
} | |
class _SizeableWindowState extends State<SizeableWindow> { | |
late double rightBoxPercentage = 0.5; | |
late double leftBoxPercentage = 0.5; | |
final mimimumSizePerBox = 0.15; | |
final dragHandleSize = 24.0; | |
void updateWidths(Offset offset) { | |
final width = MediaQuery.of(context).size.width; | |
final delta = offset.dx; | |
if (delta.isNegative) { | |
leftBoxPercentage = max(mimimumSizePerBox, ((leftBoxPercentage * width) - delta.abs()) / width); | |
rightBoxPercentage = 1 - leftBoxPercentage; | |
} else { | |
rightBoxPercentage = max(mimimumSizePerBox, ((rightBoxPercentage * width) - delta.abs()) / width); | |
leftBoxPercentage = 1 - rightBoxPercentage; | |
} | |
setState(() {}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// in case the window size has changed | |
// calling this here will make the widget build whenever the window size chagnes | |
final windowSize = MediaQuery.of(context).size; | |
return Stack( | |
children: [ | |
Row( | |
children: [ | |
SizedBox( | |
width: windowSize.width * leftBoxPercentage, | |
height: windowSize.height, | |
child: Container(color: Colors.blueGrey), | |
), | |
SizedBox( | |
width: windowSize.width * rightBoxPercentage, | |
height: windowSize.height, | |
child: Container(color: Colors.brown), | |
), | |
], | |
), | |
// keep at the bottom so it's shown above all widgets within the stack | |
Positioned( | |
left: (windowSize.width * leftBoxPercentage) - (dragHandleSize / 2.0), | |
top: (windowSize.height * 0.5), | |
child: GestureDetector( | |
behavior: HitTestBehavior.opaque, | |
onHorizontalDragUpdate: (details) { | |
updateWidths(details.delta); | |
}, | |
child: Container( | |
height: dragHandleSize, | |
width: dragHandleSize, | |
decoration: const BoxDecoration( | |
shape: BoxShape.circle, | |
color: Colors.red, | |
), | |
), | |
), | |
), | |
Positioned( | |
left: (windowSize.width * leftBoxPercentage) - (60 / 2.0), | |
top: (windowSize.height * 0.5) - 25, | |
child: const SizedBox( | |
height: 20, | |
width: 60, | |
child: Center( | |
child: Text( | |
'Drag Me', | |
style: TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.bold, | |
), | |
), | |
), | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment