Skip to content

Instantly share code, notes, and snippets.

@zohaib304
Created August 22, 2023 09:55
Show Gist options
  • Save zohaib304/dae3e7428efadebf4ddf25e898381720 to your computer and use it in GitHub Desktop.
Save zohaib304/dae3e7428efadebf4ddf25e898381720 to your computer and use it in GitHub Desktop.
Canvas Scroll Flutter
import 'package:flutter/material.dart';
class myscroll extends statelesswidget {
@override
widget build(buildcontext context) {
return new materialapp(
title: 'flutter demo',
theme: new themedata(
primaryswatch: colors.blue,
),
home: new myhomepage(title: 'canvas scroller'),
);
}
}
class myhomepage extends statefulwidget {
myhomepage({key key, this.title}) : super(key: key);
final string title;
@override
_myhomepagestate createstate() => new _myhomepagestate();
}
class _myhomepagestate extends state<myhomepage> {
@override
widget build(buildcontext context) {
final width = mediaquery.of(context).size.width;
final height = mediaquery.of(context).size.height;
return new scaffold(
appbar: new appbar(
title: new text(widget.title),
),
body: new center(
child: new singlechildscrollview(
scrolldirection: axis.horizontal,
child: new custompaint(
painter: new mycanvasview(),
size: new size(width*2, height/2),
),
),
),
);
}
}
class mycanvasview extends custompainter{
@override
void paint(canvas canvas, size size) {
var paint = new paint();
paint..shader = new lineargradient(colors: [colors.yellow[700], colors.redaccent],
begin: alignment.centerright, end: alignment.centerleft).createshader(new offset(0.0, 0.0)&size);
canvas.drawrect(new offset(0.0, 0.0)&size, paint);
var path = new path();
path.moveto(0.0, size.height);
path.lineto(1*size.width/4, 0*size.height/4);
path.lineto(2*size.width/4, 2*size.height/4);
path.lineto(3*size.width/4, 0*size.height/4);
path.lineto(4*size.width/4, 4*size.height/4);
canvas.drawpath(path, new paint()..color = colors.yellow ..strokewidth = 4.0 .. style = paintingstyle.stroke);
}
@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