Skip to content

Instantly share code, notes, and snippets.

@diegoveloper
Created September 5, 2019 16:00
Show Gist options
  • Save diegoveloper/a5eeda2f807a0595a9cc7602ddbf1c9e to your computer and use it in GitHub Desktop.
Save diegoveloper/a5eeda2f807a0595a9cc7602ddbf1c9e to your computer and use it in GitHub Desktop.
class Sample extends StatefulWidget {
@override
_Sample createState() => _Sample();
}
class _Sample extends State<Sample> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
Image.asset(
"images/background.png",
fit: BoxFit.cover,
),
Positioned(
left: 0.0,
right: 0.0,
top: MediaQuery.of(context).size.height / 2,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: TextFieldWithGradientBorder(),
),
),
],
),
);
}
}
class TextFieldWithGradientBorder extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: TextFieldBorderPainter(
LinearGradient(
colors: [
Colors.yellow,
Colors.green,
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
child: Container(
padding: EdgeInsets.all(5),
child: TextField(
style: TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.all(10.0),
),
),
),
);
}
}
class TextFieldBorderPainter extends CustomPainter {
final LinearGradient linearGradient;
TextFieldBorderPainter(this.linearGradient);
@override
void paint(Canvas canvas, Size size) {
final rect = Rect.fromPoints(Offset.zero, Offset(size.width, size.height));
final paint = Paint()
..strokeWidth = 2.0
..style = PaintingStyle.stroke
..shader = linearGradient.createShader(rect);
canvas.drawRect(rect, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment