Last active
March 8, 2023 01:05
-
-
Save chunhunghan/7d581652f41ddea9a742d5b98fa7ae7a to your computer and use it in GitHub Desktop.
flutter CustomInputBorder
This file contains hidden or 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
| //Based on Flutter 3.7.6 Dart SDK 2.19.3 | |
| import 'package:flutter/material.dart'; | |
| const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData.dark().copyWith( | |
| scaffoldBackgroundColor: darkBlue, | |
| ), | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: Center( | |
| child: SizedBox( | |
| width: 300, | |
| child: TextFormField( | |
| decoration: const InputDecoration( | |
| fillColor: Colors.white, | |
| focusedBorder: ExtendedOutlineInputBorder( | |
| borderSide: BorderSide(color: Colors.grey), | |
| borderRadius: 10.0, | |
| gapPadding: 8.0, | |
| ), | |
| ), | |
| ), | |
| ), | |
| )), | |
| ); | |
| } | |
| } | |
| class ExtendedOutlineInputBorder extends InputBorder { | |
| final double borderRadius; | |
| final double gapPadding; | |
| const ExtendedOutlineInputBorder({ | |
| super.borderSide = const BorderSide(), | |
| this.borderRadius = 0.0, | |
| this.gapPadding = 4.0, | |
| }); | |
| @override | |
| ExtendedOutlineInputBorder copyWith( | |
| {BorderSide? borderSide, double? borderRadius, double? gapPadding}) { | |
| return ExtendedOutlineInputBorder( | |
| borderSide: borderSide ?? this.borderSide, | |
| borderRadius: borderRadius ?? this.borderRadius, | |
| gapPadding: gapPadding ?? this.gapPadding, | |
| ); | |
| } | |
| @override | |
| EdgeInsetsGeometry get dimensions { | |
| return EdgeInsets.all(borderSide.width); | |
| } | |
| @override | |
| Path getInnerPath(Rect rect, {TextDirection? textDirection}) { | |
| final path = Path() | |
| ..addRRect(RRect.fromRectAndCorners( | |
| rect, | |
| topLeft: Radius.circular(borderRadius), | |
| topRight: Radius.circular(borderRadius), | |
| bottomLeft: Radius.circular(borderRadius), | |
| bottomRight: Radius.circular(borderRadius), | |
| )); | |
| return path; | |
| } | |
| @override | |
| Path getOuterPath(Rect rect, {TextDirection? textDirection}) { | |
| final path = Path() | |
| //..fillType = PathFillType.evenOdd | |
| ..addRRect(RRect.fromRectAndCorners( | |
| rect, | |
| topLeft: Radius.circular(borderRadius), | |
| topRight: Radius.circular(borderRadius), | |
| bottomLeft: Radius.circular(borderRadius), | |
| bottomRight: Radius.circular(borderRadius), | |
| )); | |
| return path; | |
| } | |
| @override | |
| ExtendedOutlineInputBorder scale(double t) { | |
| return ExtendedOutlineInputBorder(borderSide: borderSide.scale(t)); | |
| } | |
| @override | |
| bool get isOutline => false; | |
| @override | |
| void paint(Canvas canvas, Rect rect, | |
| {double? gapStart, | |
| double? gapExtent, | |
| double gapPercentage = 0.0, | |
| TextDirection? textDirection}) { | |
| final Paint paint = borderSide.toPaint(); | |
| paint | |
| ..color = Colors.lightBlue | |
| ..strokeWidth = 7.0; | |
| final Path path = | |
| getOuterPath(rect.inflate(4), textDirection: textDirection); | |
| canvas.drawPath(path, paint); | |
| final Paint paint1 = Paint(); | |
| paint1 | |
| ..color = Colors.deepOrange | |
| ..style = PaintingStyle.stroke | |
| ..strokeWidth = 2.0; | |
| canvas.drawRRect( | |
| RRect.fromRectAndCorners(rect.inflate(2), | |
| topLeft: Radius.circular(borderRadius), | |
| topRight: Radius.circular(borderRadius), | |
| bottomLeft: Radius.circular(borderRadius), | |
| bottomRight: Radius.circular(borderRadius)), | |
| paint1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment