diff --git a/example/lib/data/extension/example_num_ext.dart b/example/lib/data/extension/example_num_ext.dart new file mode 100644 index 0000000..c56114e --- /dev/null +++ b/example/lib/data/extension/example_num_ext.dart @@ -0,0 +1,89 @@ +import 'package:ansu_ui/ansu_ui.dart'; +import 'package:flutter/material.dart'; + +class ExampleNumExt extends StatefulWidget { + ExampleNumExt({Key key}) : super(key: key); + + @override + _ExampleNumExtState createState() => _ExampleNumExtState(); +} + +class _ExampleNumExtState extends State { + @override + Widget build(BuildContext context) { + return ASScaffold( + title: 'Num Ext', + body: ListView( + children: [ + ListTile(title: Text('Comma')), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('1000'), + subtitle: Text(1000.comma()), + ), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('1000000'), + subtitle: Text(1000000.comma()), + ), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('100.00'), + subtitle: Text(100.00.comma()), + ), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('0.123456'), + subtitle: Text(0.123456.comma()), + ), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('123456.789'), + subtitle: Text(123456.789.comma()), + ), + ListTile( + leading: Chip(label: Text('.comma')), + title: Text('123456.7899999'), + subtitle: Text(123456.7899999.comma(fixed: -1)), + trailing: Chip(label: Text('without Fixed')), + ), + ListTile(title: Text('SizedBox')), + ListTile( + leading: Chip(label: Text('.wb')), + title: Text('Width Box'), + subtitle: Text('40.wb'), + trailing: Container( + height: 2.w, + width: 40.w, + color: Colors.red, + ), + ), + ListTile( + leading: Chip(label: Text('.hb')), + title: Text('Height Box'), + subtitle: Text('40.hb'), + trailing: Container( + height: 40.w, + width: 2.w, + color: Colors.red, + ), + ), + ListTile(title: Text('Radius')), + ListTile( + leading: Chip(label: Text('.radius')), + title: Text('Radius Gen'), + subtitle: Text('10.radius'), + trailing: Container( + height: 30.w, + width: 30.w, + decoration: BoxDecoration( + borderRadius: 10.radius, + color: Colors.pink, + ), + ), + ), + ], + ), + ); + } +} diff --git a/example/lib/data/extension/example_string_ext.dart b/example/lib/data/extension/example_string_ext.dart new file mode 100644 index 0000000..175c227 --- /dev/null +++ b/example/lib/data/extension/example_string_ext.dart @@ -0,0 +1,27 @@ +import 'package:ansu_ui/ansu_ui.dart'; +import 'package:flutter/material.dart'; + +class ExampleStringExt extends StatefulWidget { + ExampleStringExt({Key key}) : super(key: key); + + @override + _ExampleStringExtState createState() => _ExampleStringExtState(); +} + +class _ExampleStringExtState extends State { + @override + Widget build(BuildContext context) { + return ASScaffold( + title: 'String Ext', + body: ListView( + children: [ + ListTile( + leading: Chip(label: Text('.phone')), + title: Text('18888888888'), + subtitle: Text('18888888888'.phone), + ), + ], + ), + ); + } +} diff --git a/example/lib/example_extension.dart b/example/lib/example_extension.dart deleted file mode 100644 index 989fbcf..0000000 --- a/example/lib/example_extension.dart +++ /dev/null @@ -1,47 +0,0 @@ -import 'package:ansu_ui/ansu_ui.dart'; -import 'package:flutter/material.dart'; -import 'package:ansu_ui/ansu_ui.dart'; - -class ExampleExtension extends StatefulWidget { - ExampleExtension({Key key}) : super(key: key); - - @override - _ExampleExtensionState createState() => _ExampleExtensionState(); -} - -class _ExampleExtensionState extends State { - @override - Widget build(BuildContext context) { - return ASScaffold( - title: 'Extension', - body: ListView( - children: [ - ListTile( - title: Text('.phone'), - trailing: Text('17855823545 => ${'17855823545'.phone}'), - ), - ListTile( - title: Text('.comma'), - trailing: Text('1000 => ${1000.comma}'), - ), - ListTile( - title: Text('.comma'), - trailing: Text('1000000 => ${1000000.comma}'), - ), - ListTile( - title: Text('.comma'), - trailing: Text('100.00 => ${100.00.comma}'), - ), - ListTile( - title: Text('.comma'), - trailing: Text('0.123456 => ${0.123456.comma}'), - ), - ListTile( - title: Text('.comma'), - trailing: Text('123456.789 => ${123456.789.comma}'), - ), - ], - ), - ); - } -} diff --git a/example/lib/main_extention.dart b/example/lib/main_extention.dart index fc58003..953b8e9 100644 --- a/example/lib/main_extention.dart +++ b/example/lib/main_extention.dart @@ -1,4 +1,8 @@ +import 'package:example/data/extension/example_num_ext.dart'; +import 'package:example/data/extension/example_string_ext.dart'; import 'package:flutter/material.dart'; +import 'package:ansu_ui/ansu_ui.dart'; +import 'package:get/get.dart'; class MainExtention extends StatefulWidget { MainExtention({Key key}) : super(key: key); @@ -8,8 +12,39 @@ class MainExtention extends StatefulWidget { } class _MainExtentionState extends State { + _innerButton({ + VoidCallback onPressed, + Widget child, + Widget icon, + }) { + return TextButton( + onPressed: onPressed, + child: Row( + children: [ + Expanded(child: icon), + child, + Spacer(), + ], + ), + ); + } + @override Widget build(BuildContext context) { - return ListView(); + return ListView( + padding: EdgeInsets.all(16.w), + children: [ + _innerButton( + onPressed: () => Get.to(ExampleNumExt()), + child: Text('Num Ext'), + icon: Icon(Icons.filter_9), + ), + _innerButton( + onPressed: () => Get.to(ExampleStringExt()), + child: Text('String Ext'), + icon: Icon(Icons.text_fields), + ), + ], + ); } } diff --git a/example/lib/main_home.dart b/example/lib/main_home.dart index 58f4ac4..f7362dc 100644 --- a/example/lib/main_home.dart +++ b/example/lib/main_home.dart @@ -3,7 +3,6 @@ import 'package:example/example_bottom_button.dart'; import 'package:example/example_box.dart'; import 'package:example/example_dialog.dart'; import 'package:example/example_drawer.dart'; -import 'package:example/example_extension.dart'; import 'package:example/example_listtile.dart'; import 'package:example/example_refresh.dart'; import 'package:example/example_tag.dart'; @@ -79,10 +78,6 @@ class _MainHomeState extends State { title: '选框 Box', onPressed: () => Get.to(ExampleBox()), ), - ASButton.info( - title: '插件 Extension', - onPressed: () => Get.to(ExampleExtension()), - ), ], ); } diff --git a/lib/extension/num_extension.dart b/lib/extension/num_extension.dart index 257736d..bd6e0ab 100644 --- a/lib/extension/num_extension.dart +++ b/lib/extension/num_extension.dart @@ -13,8 +13,10 @@ extension NumExt on num { BorderRadius get radius => BorderRadius.circular(this.w); ///每三位数加逗号 - String comma ({int fixed}){ - String _num = this.toStringAsFixed(fixed??2); + String comma({int fixed = 2}) { + bool notUseFixedFlag = fixed == -1; + String _num = + notUseFixedFlag ? this.toString() : this.toStringAsFixed(fixed); String str = ''; int count = 0; if (_num.indexOf('.') == -1) { @@ -29,14 +31,14 @@ extension NumExt on num { return str; } else { for (var i = _num.indexOf('.') - 1; i >= 0; i--) { - if (count % 3 == 0 && count!= 0) { + if (count % 3 == 0 && count != 0) { str = _num.substring(i, i + 1) + ',' + str; } else { str = _num.substring(i, i + 1) + str; } count++; } - str=str+(_num.substring(_num.indexOf('.'),_num.length)); + str = str + (_num.substring(_num.indexOf('.'), _num.length)); return str; } }