idkit 0.4.7 idkit: ^0.4.7 copied to clipboard
idkit is a package that makes it easier for developers to develop projects, and contains a variety of functions and components.
import 'dart:async';
import 'dart:developer';
import 'dart:ui';
import 'dart:ui' as ui;
import 'package:example/fam/fam.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:idkit/idkit.dart';
void main() {
runApp(const Widgets());
}
void timeFormatHandle() {
/// Time formatting.
final DateTime dateTime = DateTime(2022, 6, 12, 9, 22, 50);
final String style1 = dateTime.format();
debugPrint(style1); // 2022-06-12 09:22:50
final String style2 = dateTime.format(style: 'yyyy/MM/dd hh:mm:ss');
debugPrint(style2); // 2022/06/12 09:22:50
final String style3 = dateTime.format(style: 'yyyy-MM-dd');
debugPrint(style3); // 2022-06-12
final String style4 = dateTime.format(style: 'hh:mm:ss');
debugPrint(style4); // 09:22:50
final String style5 = dateTime.format(style: 'yyyy-M-d');
debugPrint(style5); // 2022-6-12
final String style6 = dateTime.format(style: 'h:m:s');
debugPrint(style6); // 9:22:50
final String style7 = dateTime.format(style: 'yyyy~MM+dd hh:mm:ss');
debugPrint(style7); // 2022~06+12 hh:mm:ss
/// Friendly time formatting.
const Locale locale = Locale('zh');
// 1) Just now
final DateTime dateTime1 = DateTime.now();
debugPrint(dateTime1.friendFormat());
debugPrint(dateTime1.friendFormat(locale: locale));
// 2) Minutes ago
final DateTime dateTime2 = dateTime1.subtract(const Duration(minutes: 2));
debugPrint(dateTime2.friendFormat());
debugPrint(dateTime2.friendFormat(locale: locale));
// 3) Am & Pm
final DateTime dateTime3 = dateTime1.subtract(const Duration(minutes: 120));
debugPrint(dateTime3.friendFormat());
debugPrint(dateTime3.friendFormat(locale: locale));
// 4) Yesterday
final DateTime dateTime4 =
dateTime1.subtract(const Duration(days: 1, hours: 4));
debugPrint(dateTime4.friendFormat());
debugPrint(dateTime4.friendFormat(locale: locale));
// 5) Day of the week
final DateTime dateTime5 =
dateTime1.subtract(const Duration(days: 3, hours: 8));
debugPrint(dateTime5.friendFormat());
debugPrint(dateTime5.friendFormat(locale: locale));
// 6) Time of year
final DateTime dateTime6 =
dateTime1.subtract(const Duration(days: 240, hours: 10));
debugPrint(dateTime6.friendFormat());
debugPrint(dateTime6.friendFormat(locale: locale));
// 7) Outside one year
final DateTime dateTime7 =
dateTime1.subtract(const Duration(days: 400, hours: 12));
debugPrint(dateTime7.friendFormat());
debugPrint(dateTime7.friendFormat(locale: locale));
/// The given time is the day of the week.
final DateTime dateTime8 = DateTime(2022, 6, 12);
debugPrint(dateTime8.getWeak());
debugPrint(dateTime8.getWeak(locale: locale));
final DateTime dateTime9 = DateTime(2022, 5, 22);
debugPrint(dateTime9.getWeak());
debugPrint(dateTime9.getWeak(locale: locale));
final DateTime dateTime10 = DateTime(2022, 3, 2);
debugPrint(dateTime10.getWeak());
debugPrint(dateTime10.getWeak(locale: locale));
}
void thousandsHandle() {
// int
debugPrint('// Int ');
debugPrint((-0).formatThousands());
debugPrint((-123).formatThousands());
debugPrint((-1234).formatThousands());
debugPrint(123456.formatThousands());
debugPrint(1234567.formatThousands());
debugPrint(123456789.formatThousands());
debugPrint(1234567890.formatThousands());
// double
debugPrint('\n// Double ');
debugPrint(0.0.formatThousands());
debugPrint(123.12.formatThousands());
debugPrint(1234.123.formatThousands());
debugPrint(123456.1234.formatThousands());
debugPrint(1234567.12345.formatThousands());
debugPrint(123456789.123456.formatThousands());
debugPrint(1234567890.1234567.formatThousands());
// String
debugPrint('\n// String ');
debugPrint('0.0'.formatThousands());
debugPrint('123.12'.formatThousands());
debugPrint('1234.123'.formatThousands());
debugPrint('- 123456.1234'.formatThousands());
debugPrint('+ 1234567.12345 '.formatThousands());
debugPrint('-1234567 89.123456'.formatThousands());
debugPrint('+ 1234 567890.123 4567 '.formatThousands());
// Custom thousandth symbol
debugPrint('\n// Custom thousandth symbol ');
debugPrint(1234567.formatThousands(symbol: '_'));
debugPrint(1234567890.2.formatThousands(symbol: '、'));
// Retain a certain number of decimal places
debugPrint('\n// Retain a certain number of decimal places;Whether to round');
debugPrint(0.56.formatThousands(fractionDigits: 0));
debugPrint(0.56.formatThousands(fractionDigits: 0, round: false));
debugPrint(1234.5.formatThousands(fractionDigits: 2));
debugPrint(1234567.123490.formatThousands(fractionDigits: 4));
debugPrint(1234567.123490.formatThousands(fractionDigits: 4, round: false));
}
/// Desensitized
void desensitizedMethod() {
/// Phone number desensitization
final phone = '18801210283'.phoneDesensitized;
// Handling extra spaces
final phone1 = ' 18801210835 '.phoneDesensitized;
final phone2 = '188 0121 0283'.phoneDesensitized;
debugPrint('\n// Phone number desensitization');
debugPrint(phone); // 188****0283
debugPrint(phone1); // 188****0835
debugPrint(phone2); // 188****0283
/// Bank card number desensitization
final String bank16 = '6333234433554466'.bankDesensitized;
final String bank17 = '63332344335544661'.bankDesensitized;
final String bank19 = '6333234433554466234'.bankDesensitized;
debugPrint('\n// Bank card number desensitization');
debugPrint(bank16); // ****4466
debugPrint(bank17); // ****4661
debugPrint(bank19); // ****6234
// Mid
final String bankMid16 = '6333234433554466'.bankMidDesensitized;
final String bankMid17 = '63332344335544661'.bankMidDesensitized;
final String bankMid19 = '6333234433554466234'.bankMidDesensitized;
debugPrint('\n// Bank card number middle desensitization');
debugPrint(bankMid16); // 6333****4466
debugPrint(bankMid17); // 6333****4661
debugPrint(bankMid19); // 6333****6234
/// ID card number desensitization
final String id = '411122128995160537'.idDesensitized;
debugPrint('\n// ID card number desensitization');
debugPrint(id); // 411122********0537
}
void numberFormatMethod() {
// Phone number format
final String phone = '18702210183'.phoneFormat;
final String phone1 = '187 3221 0183'.phoneFormat;
final String phone2 = ' 187 42220183 '.phoneFormat;
debugPrint('\n/// Number Formatting');
debugPrint('// Phone number format');
debugPrint(phone); // 187 0221 0183
debugPrint(phone1); // 187 3221 0183
debugPrint(phone2); // 187 4222 0183
// Bank card number format
final String bank16 = '6222333444455551'.bankFormat;
final String bank17 = '62223334444555562'.bankFormat;
final String bank19 = '6222333444455556785'.bankFormat;
debugPrint('\n// Bank card number format');
debugPrint(bank16); // 6222 3334 4445 5551
debugPrint(bank17); // 6222 3334 4445 5556 2
debugPrint(bank19); // 6222 3334 4445 5556 785
// ID number format
final String id18 = '411422189806789226'.idFormat;
debugPrint('\n// ID number format');
debugPrint(id18); // 6222 3334 4445 5551
}
/// Widgets Test
class Widgets extends StatelessWidget {
const Widgets({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Widgets Test')),
body: Container(
alignment: Alignment.center,
child: const ImageHandle(),
),
),
);
}
}
/// Extension of text style
class TestTextStyle extends StatelessWidget {
const TestTextStyle({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
const Text(
'idkit makes development easy!',
style: TextStyle(),
),
Text(
'idkit makes development easy!',
style: const TextStyle().bold,
),
Text(
'idkit makes development easy!',
style: const TextStyle().c(Colors.red),
),
Text(
'idkit makes development easy!',
style: const TextStyle().hexColor('#3A4E5F'),
),
Text(
'idkit makes development easy!',
style: const TextStyle().italic,
),
Text(
'idkit makes development easy!',
style: const TextStyle().fSize(20),
),
Text(
'idkit makes development easy!',
style: const TextStyle().h(3),
),
Text(
'idkit makes development easy!',
style: const TextStyle().lSpacing(3),
),
Text(
'idkit makes development easy!',
style: const TextStyle().wSpacing(5),
),
Text(
'idkit makes development easy!',
style: const TextStyle()
.medium
.italic
.c(Colors.cyan)
.fSize(20)
.h(3)
.lSpacing(3)
.wSpacing(5),
),
],
);
}
}
/// Padding
class PaddingTest extends StatelessWidget {
const PaddingTest({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: Colors.red,
child: Column(
children: <Widget>[
Container(
color: Colors.purple,
height: 100,
).insets(const EdgeInsets.all(10)),
Container(
height: 100,
color: Colors.blue,
).insetsOnly(right: 120),
Container(
height: 100,
color: Colors.blue,
).insetsSymmetric(horizontal: 20),
CustomScrollView(
shrinkWrap: true,
slivers: [
SliverToBoxAdapter(
child: Container(
height: 50,
color: Colors.yellow,
),
).silverInsets(const EdgeInsets.all(10)),
SliverToBoxAdapter(
child: Container(
height: 50,
color: Colors.purple,
),
).silverInsetsSymmetric(horizontal: 20),
SliverToBoxAdapter(
child: Container(
height: 50,
color: Colors.cyan,
),
).silverInsetsOnly(left: 20, top: 20),
],
)
],
),
);
}
}
/// Color
class ColorWidget extends StatelessWidget {
const ColorWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// HEX
Color color = '#3767FE'.hexColor;
color = '#3767FE'.color;
Color color1 = '3707FE'.hexColor;
color1 = '3707FE'.color;
// AHEX
Color color2 = '#FF37670E'.aHexColor;
color2 = 'FF37670E'.aHexColor;
// HEXA
Color color3 = '#A067FEFF'.hexAColor;
color3 = 'A067FEFF'.hexAColor;
return Scaffold(
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 100,
width: 100,
color: color,
),
Container(
height: 100,
width: 100,
color: color1,
),
Container(
height: 100,
width: 100,
color: color2,
),
Container(
height: 100,
width: 100,
color: color3,
),
],
),
),
);
}
}
/// Gap Test
class GapTest extends StatelessWidget {
const GapTest({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(height: 40, color: Colors.red),
20.vGap,
Container(height: 20, color: Colors.green),
50.vGap,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(width: 100, height: 20, color: Colors.red),
100.hGap,
Container(height: 20, width: 100, color: Colors.pink),
],
)
],
);
}
}
/// Choice Widget
class ChoiceWidget extends StatelessWidget {
const ChoiceWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final List<String> list = ['王昭君', '甄姬', '妲己', '不知火舞', '貂蝉'];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/// list
Text(
'IDKitChoice.list',
style: const TextStyle().bold.fSize(20),
),
IDKitChoice<String>.list(
type: ChoiceType.single,
isCancelAll: false,
shrinkWrap: true,
sources: list,
maximumChoice: 2,
isManipulate: (i) {
return true;
},
chosenIndexs: const [2],
itemBuilder: (context, state, data) {
final color = state ? Colors.red : Colors.blue;
return Container(
width: 200,
color: color,
alignment: Alignment.center,
child: Text(data),
);
},
choiceCallMethod: (result, results) {
debugPrint(result);
},
).insetsOnly(bottom: 20),
/// listSeparated
Text(
'IDKitChoice.listSeparated',
style: const TextStyle().bold.fSize(20),
),
IDKitChoice<String>.listSeparated(
type: ChoiceType.multiple,
isCancelAll: true,
shrinkWrap: true,
sources: list,
maximumChoice: 2,
isManipulate: (i) {
return true;
},
// chosenIndexs: const [3, 1],
separatorBuilder: (_, state, result) {
return const Divider(
height: 2,
color: Colors.red,
thickness: 2,
);
},
itemBuilder: (context, state, data) {
final color = state ? Colors.red : Colors.blue;
return Container(
width: 200,
color: color,
alignment: Alignment.center,
child: Text(data),
);
},
choiceCallMethod: (result, results) {
debugPrint(results.toString());
},
).insetsOnly(bottom: 20),
/// warp
Text(
'IDKitChoice.warp',
style: const TextStyle().bold.fSize(20),
),
IDKitChoice<String>.warp(
type: ChoiceType.single,
sources: list,
isManipulate: (i) {
return true;
},
itemBuilder: (context, state, data) {
final color = state ? Colors.red : Colors.blue;
return ColoredBox(
color: color,
child: Text(data),
).insetsOnly(right: 10);
},
choiceCallMethod: (result, results) {
debugPrint(result);
},
).insetsOnly(bottom: 20),
/// grid
Text(
'IDKitChoice.grid',
style: const TextStyle().bold.fSize(20),
),
IDKitChoice<String>.grid(
type: ChoiceType.single,
sources: list,
isCancelAll: false,
shrinkWrap: true,
itemBuilder: (context, state, data) {
final color = state ? Colors.red : Colors.blue;
return Container(
width: 20,
height: 20,
color: color,
alignment: Alignment.center,
child: Text(data),
);
},
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 3,
),
choiceCallMethod: (result, results) {
debugPrint(result);
},
).insetsOnly(bottom: 20),
],
);
}
}
/// multi-finger detection
class MultiFingerWidget extends StatelessWidget {
const MultiFingerWidget({super.key});
@override
Widget build(BuildContext context) {
return IDKitGestureRecognizer(
fingerCount: (p0) {
log('$p0 fingers identified');
},
onTap: (p0) {
// for (var i = 0; i < 10; i++) {
// print(DateTime.now().microsecondsSinceEpoch);
// }
},
onStart: (p0) {
log('Zoom start -- onStart -- ${p0.points.first.localPosition}');
// log(p0.points.toString());
},
onUpdate: (IDKitUpdateDetails p0) {
log('Zoom update -- onUpdate - ${p0.pointerCount}');
},
onEnd: (p0) {
log('Zoom end -- onEnd');
},
pressOnStart: (p0) {
// log('Pressure Sensitivity Start -- ${p0.pressure}');
},
pressOnUpdate: (p0) {
// log('Pressure update -- ${p0.pressure}');
},
pressOnEnd: (p0) {
// log('Pressure Sensitivity End -- ${p0.pressure}');
},
duration: const Duration(microseconds: 10),
child: Container(
color: Colors.red,
),
).insetsAll(100);
}
}
/// ui.image loading
class UIImageLoad extends StatelessWidget {
const UIImageLoad({super.key});
// load ui.image
Future<ui.Image> loadImage() async {
final ImmutableBuffer buffer =
await rootBundle.loadBuffer(FamManager.uiImage);
final Codec codec = await instantiateImageCodecFromBuffer(buffer);
final FrameInfo frameInfo = await codec.getNextFrame();
return frameInfo.image;
}
Future<ByteData> loadByteData() => rootBundle.load(FamManager.uiImage);
@override
Widget build(BuildContext context) {
return Column(
children: [
FutureBuilder<ByteData>(
future: loadByteData(),
builder: (context, snapshot) {
if (snapshot.data != null) {
return Container(
color: Colors.red,
child: IDKitImage.byteBuffer(
byteBuffer: null,
width: 100,
height: 100,
errorChild: Container(
color: Colors.red,
),
),
);
}
return Container();
},
)
],
);
}
}
class SliderWidget extends StatelessWidget {
const SliderWidget({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: IDKitSlider.slider(
trackIndent: 6,
isThumbAnimated: true,
initValue: 0.2,
maxValue: 0.8,
),
);
}
}
class AlertWidget extends StatelessWidget {
const AlertWidget({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(onPressed: () {}, child: const Text('Alert_Loading'))
],
),
);
}
}
class ImageHandle extends StatefulWidget {
const ImageHandle({super.key});
@override
State<ImageHandle> createState() => _ImageHandleState();
}
class _ImageHandleState extends State<ImageHandle> {
Future<ui.Image> loadImage(String image) async {
final ImmutableBuffer buffer = await rootBundle.loadBuffer(image);
final Codec codec = await instantiateImageCodecFromBuffer(buffer);
final FrameInfo frameInfo = await codec.getNextFrame();
return frameInfo.image;
}
ui.Image? sImg;
ui.Image? wImg;
ui.Image? wMImg;
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FutureBuilder<ui.Image>(
future: loadImage(FamManager.aa),
builder: (context, snapshot) {
if (snapshot.data != null) {
sImg = snapshot.data!;
return Container(
color: Colors.red,
child: IDKitImage.image(
image: snapshot.data,
height: 200,
),
);
}
return Container();
},
),
20.hGap,
FutureBuilder<ui.Image>(
future: loadImage(FamManager.mark),
builder: (context, snapshot) {
if (snapshot.data != null) {
wImg = snapshot.data!;
return Container(
color: Colors.red,
child: IDKitImage.image(
image: snapshot.data,
height: 200,
width: 200,
),
);
}
return Container();
},
)
],
),
20.hGap,
TextButton(
onPressed: () {
wImg!.realContentSize().then((value) => IDKitLog.value(value.size));
setState(() {
wMImg = sImg!.addWaterMarkTextSync(
'我是谁',
style: const TextStyle(
height: 5, letterSpacing: 10, color: Colors.red),
angle: 45,
hOffset: 10,
vOffset: 10,
sizeScale: 1,
opacity: 0.3,
location: WMLocation.tile,
);
});
},
child: const Text('图像水印'),
),
if (wImg.isNoNull)
FutureBuilder<ui.Image?>(
future: sImg!
.realContentImage(includeIgnoreColor: false, tolerance: 0.1),
builder: (context, snapshot) {
if (snapshot.data != null) {
sImg = snapshot.data!;
return IDKitImage.image(
image: snapshot.data,
).insetsAll(10);
}
return Container();
},
),
],
);
}
}