sura_flutter
AsurRaa 的一个 Flutter 包,用于小部件和实用函数。
从 2.x 迁移到 3.x
-
重大更改
- 移除 FutureManager, AsyncSubjectManager, FutureManagerBuilder
- 所有管理器类现在都有一个单独的包
dependencies: sura_manager: any
安装
将此添加到 pubspec.yaml
dependencies:
sura_flutter: ^0.4.2
小部件
| Widget | 描述 |
|---|---|
| SuraRaisedButton | 带有加载通知器的自定义 ElevatedButton |
| SuraBadge | 类似通知的小徽章 |
| SuraActionSheet | 用于选项选择器的自定义 CupertinoActionSheet |
| ConditionalWidget | 根据布尔条件构建小部件 |
| SuraToolbar | 自定义工具栏或 AppBar |
| SuraFutureHandler | 具有更少样板代码的 FutureBuilder |
| SuraAccordian | 自定义 ExpansionTile |
| SuraExpandable | 类似于 SuraAccordion,但用途不同 |
| SuraConfirmationDialog | 平台自适应 AlertDialog,具有取消和确认操作 |
| SuraAsyncButton | 完全自定义 Material ElevatedButton,用于异步 onPressed 回调 |
| SuraLoadingDialog | 创建和管理加载对话框 |
| SuraPlatformChecker | 平台自适应小部件 |
| SuraSimpleDialog | 简单的平台自适应 AlertDialog |
| SuraListTile | 自定义 ListTile |
| SuraPaginatedList | 支持分页的 ListView |
| SuraPaginatedGridBuilder | 支持分页的 Gridview |
| SuraIconButton | 自定义 IconButton |
| SuraFlatButton | 自定义 TextButton 或 FlatButton |
| SpaceX | 仅带有宽度的 SizedBox |
| SpaceY | 仅带有高度的 SizedBox |
| SuraStreamHandler | 具有更少样板代码的 Streambuilder |
| SuraNotifier | 具有更少样板代码的 ValueListenableBuilder |
Mixin
AfterBuildMixin
创建一个覆盖方法,该方法将在 build 方法被调用后调用
class _HomePageState extends State<NewPage> with AfterBuildMixin {
//this method will call after widget has been build
@override
void afterBuild(BuildContext context) {
}
@override
Widget build(BuildContext context) {
return Container();
}
}
SuraFormMixin
在使用 Form 时提供一些属性和方法
字段和属性
- formKey: 表单的 key
- loadingNotifier: 一个布尔 ValueNotifier
- passwordObsecureNotifier: 用于切换密码可见字段的布尔 ValueNotifier
- isFormValidated: 通过验证 formKey 返回的布尔值
method
- toggleLoading: 切换 loadingNotifier
- togglePasswordObsecure: 切换 passwordObsecureNotifier
class _HomePageState extends State<NewPage> with SuraFormMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(key: formKey, child: child)
);
}
}
BoolNotifierMixin
提供一个 ValueNotifier<bool>和一个值切换函数
- boolNotifier: 一个布尔 ValueNotifier
method
- toggleValue: 切换 loadingNotifier
class _HomePageState extends State`<NewPage>` with BoolNotifierMixin {
@override
Widget build(BuildContext context) {
return Container();
}
}
Widget 的扩展
padding, margin
Text("Hello Flutter").padding(EdgeInsets.all(16.0)) // defaulat value is EdgeInsets.all(8.0)
Text("Hello Flutter").margin(EdgeInsets.all(16.0)) // defaulat value is EdgeInsets.all(8.0)
///As a value
Text("Hello Flutter").marginValue(all: 12)
Text("Hello Flutter").paddingValue(horizontal: 12, vertical: 8)
cssSpacing
Text("Hello Flutter").cssSpacing(margin: [10,10], padding:[16])
//css margin and padding rule
rotate (以度为单位)
Text("Hello Flutter").rotate(45)
flexible, expanded, clipOval, opacity
Text("Hello Flutter").flexible
Text("Hello Flutter").expanded
Text("Hello Flutter").clipOval
Text("Hello Flutter").opacity(0.5)
TextStyle 扩展
Text("Hello Flutter", style: TextStyle().normal)
Text("Hello Flutter", style: TextStyle().medium)
Text("Hello Flutter", style: TextStyle().bold)
Text("Hello Flutter", style: TextStyle().applyColor(Colors.white))
Text("Hello Flutter", style: TextStyle().applFontSize(24))
其他扩展
BuildContext 扩展
Size screenSize = context.screenSize;
Color primaryColor = context.primaryColor;
Color accentColor = context.accentColor;
TextTheme textTheme = context.textTheme;
Theme theme = context.theme;
DateTime 扩展
DateTime.now().format(format: "dd mmm yyyy", locale: context.locale)
DateTime.now().isTheSameDay(DateTime.now())
DateTime.now().formatToLocalDate(format: "dd mmm yyyy", locale: context.locale)
String 扩展
String name = "chunlee".capitalize() // => Chunlee
实用程序和样式
DotTabIndicator

TabBar(
...
indicator: DotTabIndicator(
color: Colors.blue,
dotAlignment: TabAlignment.bottom,
)
...
)
SmallUnderlineTabIndicator

TabBar(
...
isScrollable: true, //This indicator work best with scrollable tab bar
indicator: SmallUnderlineTabIndicator(
color: Colors.blue,
paddingLeft: 16,
alignment: TabAlignment.bottom,
)
...
)
ShadowInputBorder
这个输入边框解决了 TextField 没有默认高程的问题。

TextFormField(
...
decoration: InputDecoration(
border: ShadowInputBorder(
elevation: 2.0, //required
fillColor: Colors.white, //required
borderRadius: SrauStyle.radius(),
shadowColor: Colors.black87,
),
)
...
)
SuraColor
//Get Color from hex string
Color green = SuraColor.fromCode("42f545")
//Get Color from RGB without Alpha or Opacity
Color newColor = SuraColor.fromRGB(8, 182, 155)
//Convert color to MaterialColor
MaterilColor newMaterialColor = SuraColor.toMaterial(0xFF869CF4)
SuraUtils
//Get byte from asset bundle
Future`<Uint8List>` imageByte = await SuraUtils.getBytesFromAsset("image asset path", 200); //200 is imagewidth
//Get random image from unsplash
String carUrlImage = SuraUtils.unsplashImage(width: 200, height: 200, category: "car");
//Get random from picsum with provided: width and height
String randomUrlImage = SuraUtils.picsumImage(200,300);
SuraFormValidator
提供一些字段验证
TextFormField(
validator: (value) => SuraFormValidator.validateField(value, field:"username"),
)
PageNavigator 和 SuraNavigator
PageNavigator 支持 push, pushReplacement 和 pushAndRemove 方法
PageNavigator.push(context, DetailPage());
PageNavigator.pushReplacement(context, HomePage());
PageNavigator.pushAndRemove(context, RootPage());
SuraNavigator 也支持 push, pushReplacement, pushAndRemove,无需提供 context,但您需要将 SuraNavigator.navigatorKey 添加到 MaterialApp
MaterialApp(
...
navigatorKey: SuraNavigator.navigatorKey,
...
home: MyHomePage(),
)
SuraNavigator.push(DetailPage());
SuraNavigator.pushReplacement(HomePage());
SuraNavigator.pushAndRemove(RootPage());
SuraNavigator 也可以显示对话框,无需提供 context
var result = await SuraNavigator.dialog(MyDialog());
SuraDecoration
RoundedRectangleBorder roundRectangle = SuraDecoration.roundRect(12);//default value is 8
BorderRadius radius = SuraDecoration.radius(12); //default value is 8