import 'package:flutter/material.dart';
import './StringTool.dart';

class PAFFStlCustomBankCardListTitle extends StatelessWidget {
final EdgeInsets edgeInsets;
final String leftText;
final TextStyle leftTextStyle;
final String rightText;
final TextStyle rightTextStyle;

const PAFFStlCustomBankCardListTitle({this.edgeInsets,
@required this.leftText,
this.leftTextStyle,
this.rightText,
this.rightTextStyle,Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
padding: edgeInsets ?? EdgeInsets.fromLTRB(15, 10, 15, 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(leftText,style: leftTextStyle ?? TextStyle(fontSize:14,color:Colors.black)),
Expanded(child: Container(),flex: 1,),
Text(_bankCardNumStr(numStr:rightText ?? '',head: 1, trail: 1),
style: rightTextStyle ?? TextStyle(color:Colors.black,fontSize:16,)),
],
),
);
}

String _bankCardNumStr({String numStr, int head = 4, int trail = 4}){
// String nStr = numStr.stringSeparateAndCenterReplace(beginIndex:4, endIndex: 4, fromRightToLeft: false, centerReplace : '*');


String nStr = bankNumStr(numStr,centerReplace: '*',head: head,end: trail);
return nStr;
}
}
 
 
 
 
 
 
 
 
 
 
//扩展String,增加字符串分隔方法
//扩展String,增加字符串分隔方法
String bankNumStr(String numStr,
{int count = 4,
int head = 0,
int end = 0,
List<String> separator,
String joinStr = ' ',
bool fromRightToLeft = false,
String centerReplace = '*'}) {
int beginIndex = head;
int endIndex = end;
if (numStr.isEmpty) {
return "";
}

if (count < 1) {
return numStr;
}
// String tmpStr = this.replaceAll(new RegExp(r"\s*"), "");
var tempStr = numStr;
tempStr = numStr.replaceAll(' ', "");
if (separator != null) {
for (var item in separator) {
tempStr = numStr.replaceAll(item, "");
}
}

 
if (count > tempStr.length) {
return numStr;
}
if (beginIndex + endIndex > tempStr.length) {
return numStr;
}
var nStr = '';
for (var i = 0; i < tempStr.length; i++) {
if (i < beginIndex) {
nStr = nStr + tempStr[i];
} else if (i == beginIndex) {
if (i == 0) {
nStr = nStr + tempStr[i];
} else {
nStr = nStr + joinStr;
if(centerReplace != null || centerReplace != '' || centerReplace.trim() != ''){
nStr = nStr + centerReplace;
}else{
nStr = nStr + tempStr[i];
}
 
}
} else if (i < tempStr.length - endIndex) {
// nStr = nStr + tempStr[i];
if(centerReplace != null || centerReplace != '' || centerReplace.trim() != ''){
nStr = nStr + centerReplace;
}else{
nStr = nStr + tempStr[i];
}
if ((i + 1 - beginIndex) % count == 0) {
nStr = nStr + joinStr;
}
} else if (i == tempStr.length - endIndex) {
if (endIndex == 0) {
nStr = nStr + tempStr[i];
} else {
nStr = nStr + joinStr;
nStr = nStr + tempStr[i];
// if(centerReplace != null || centerReplace != '' || centerReplace.trim() != ''){
// nStr = nStr + centerReplace;
// }else{
// nStr = nStr + tempStr[i];
// }
}
} else {
nStr = nStr + tempStr[i];
}
}

// for (var i = 0; i < chars.length -1; i++) {
// separatedChars[j] = String.fromCharCode(chars[i]);
// if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
// j += 1;
// separatedChars[j] = separator;
// }

// j += 1;
// }

return fromRightToLeft
? String.fromCharCodes(nStr.runes.toList().reversed)
: nStr;
}
 
 
 
 
 
 
 
 
 
新版本下
import 'dart:ui';

import 'package:flutter/material.dart';
import './str_util.dart';

class PAFFStlCustomBankCardListTitle extends StatelessWidget {
EdgeInsets edgeInsets = EdgeInsets.all(20);
String leftText;
TextStyle leftTextStyle = TextStyle(color: Colors.red, fontSize: 14);
String rightText;// = 'right';
TextStyle rightTextStyle = TextStyle(color: Colors.red, fontSize: 14);

PAFFStlCustomBankCardListTitle(
{required EdgeInsets edgeInsets,
this.leftText = '',
required leftTextStyle,
this.rightText = '',
required rightTextStyle,
Key? key}) : super(key: key);

// PAFFStlCustomBankCardListTitle(
// {@required this.edgeInsets,
// @required this.leftText,
// @required.leftTextStyle,
// @required.rightText,
// @required.rightTextStyle,
// Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
padding: edgeInsets,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(leftText,
style: leftTextStyle),
Expanded(
child: Container(),
flex: 1,
),
Text(_bankCardNumStr(numStr: rightText, head: 1, trail: 2),
style: rightTextStyle),
],
),
);
}

String _bankCardNumStr({String? numStr, int head = 4, int trail = 4}) {
// String nStr = numStr.stringSeparateAndCenterReplace(beginIndex:4, endIndex: 4, fromRightToLeft: false, centerReplace : '*');
print('numStr.length ${numStr!.length}');
String nStr =
bankNumStr(numStr, centerReplace: '*', head: head, end: trail);
return nStr;
}
}