Skip to content

Instantly share code, notes, and snippets.

@cdigit
Created February 7, 2023 02:55
Show Gist options
  • Save cdigit/ebbe57041bf950018fe5733674c68b20 to your computer and use it in GitHub Desktop.
Save cdigit/ebbe57041bf950018fe5733674c68b20 to your computer and use it in GitHub Desktop.
poetic-glacier-7189
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(
// Application name
title: 'Flutter Stateful Clicker Counter',
theme: ThemeData(
// Application theme data, you can set the colors for the application as
// you want
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Clicker Counter Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
void showValue(val){
double f = val;
setState(() {
_counter ;
});
}
double changeX = 0;
bool isStart = false;
late AnimationController animCtrl;
late Animation growAnimation;
final shape_painter = ShapePainter(100);
@override
initState(){
animCtrl = AnimationController(vsync: this, duration: Duration(seconds: 3))
..addListener(() {
//_incrementCounter();
showValue(growAnimation.value);
// print('GrowAnimation value: ${growAnimation.value}');
});
growAnimation = Tween<double>(begin: 0, end: 200).animate(animCtrl);
animCtrl.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Wrap(
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: TextStyle(fontSize: 25),
),
// Container(
// width: 50,
// height:100,
// child:
// CustomPaint(
// painter: ShapePainter(growAnimation.value),
// child: Container(
// height: 100,
// width: 100,
// )),
Row(
children: [
// left handle
GestureDetector(
onHorizontalDragStart: (DragStartDetails details){
print("st: ${details.localPosition.dx}");
// dx for start is the x offset of the mouse relative to the container
changeX = (_counter as double) - details.localPosition.dx.floor();
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
setState((){
// print(details.localPosition.dx);
_counter = changeX.floor() + details.localPosition.dx.floor();
});
},
child:
Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20),
)
),
width:10, height:200)
),
Container(
// padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child:
ClipRect(
child:
Container(
width: _counter+0.2,
height:200,
child:
AnimatedBuilder(
animation: growAnimation,
builder: (context, snapshot) {
return CustomPaint(
painter: ShapePainter(50),
child: Container(width: 100, height:100),
);
},
)
)
)
),
GestureDetector(
onHorizontalDragStart: (DragStartDetails details){
print("st: ${details.localPosition.dx}");
// dx for start is the x offset of the mouse relative to the container
changeX = (_counter as double) - details.localPosition.dx.floor();
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
setState((){
// print(details.localPosition.dx);
_counter = changeX.floor() + details.localPosition.dx.floor();
});
},
child: Container(
width:10,
height:200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
bottomRight: Radius.circular(20),
)
))
),
]
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class ShapePainter extends CustomPainter {
var h =0.0;
var width =0.0;
ShapePainter(this.width);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.blue
..strokeWidth = 5
..strokeCap = StrokeCap.round;
Offset startingPoint = Offset(0, size.height / 2);
Offset endingPoint = Offset(size.width, size.height / 2);
canvas.drawLine(startingPoint, endingPoint, paint);
paint
..color = Color(0xff638965)
..style = PaintingStyle.fill;
//a rectangle
canvas.drawRect(Offset(0, 0) & Size(200, 200), paint);
//canvas.clipRect;
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment