47 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Dart
		
	
	
		
		
			
		
	
	
			47 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Dart
		
	
	
|  | import 'dart:ffi'; | ||
|  | 
 | ||
|  | import 'package:flutter/material.dart'; | ||
|  | import 'package:dotted_border/dotted_border.dart'; | ||
|  | 
 | ||
|  | class DottedBorderBox extends StatelessWidget { | ||
|  |   final Widget? child; | ||
|  |   final Color color; | ||
|  |   final double strokeWidth; | ||
|  |   final List<double> dashPattern; | ||
|  |   final BorderRadius borderRadius; | ||
|  |   final EdgeInsets padding; | ||
|  |   final StrokeCap strokeCap; | ||
|  | 
 | ||
|  |   const DottedBorderBox({ | ||
|  |     super.key, | ||
|  |     this.child, | ||
|  |     this.color = Colors.black26, | ||
|  |     this.strokeWidth = 1.5, | ||
|  |     this.dashPattern = const [6, 3], | ||
|  |     this.borderRadius = const BorderRadius.all(Radius.circular(8)), | ||
|  |     this.padding = const EdgeInsets.all(8), | ||
|  |     this.strokeCap = StrokeCap.butt, | ||
|  |   }); | ||
|  | 
 | ||
|  |   @override | ||
|  |   Widget build(BuildContext context) { | ||
|  |     return DottedBorder( | ||
|  |       options: RoundedRectDottedBorderOptions( | ||
|  |         // 控制内边距(虚线与外部的间隔)
 | ||
|  |         borderPadding: EdgeInsets.zero, | ||
|  |         // 控制虚线与 child 的间距(虚线内侧留白)
 | ||
|  |         padding: padding, | ||
|  |         color: color, | ||
|  |         strokeWidth: strokeWidth, | ||
|  |         dashPattern: dashPattern, | ||
|  |         strokeCap: strokeCap, radius: Radius.circular(0), | ||
|  |         // 如果需要,可以传 gradient 等
 | ||
|  |       ), | ||
|  |       child: ClipRRect( | ||
|  |         borderRadius: borderRadius, | ||
|  |         child: child ?? const SizedBox.shrink(), | ||
|  |       ), | ||
|  |     ); | ||
|  |   } | ||
|  | } |