Init core module

This commit is contained in:
dab246
2021-07-27 12:50:45 +07:00
committed by Dat H. Pham
parent e26927790b
commit 5b0a8f6fb2
30 changed files with 1029 additions and 0 deletions
@@ -0,0 +1,133 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
class TreeView extends InheritedWidget {
final List<Widget> children;
final bool startExpanded;
TreeView({
Key? key,
required List<Widget> children,
bool startExpanded = false,
}) : this.children = children, this.startExpanded = startExpanded, super (
key: key,
child: _TreeViewData(
children: children,
),
);
static TreeView of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<TreeView>()!;
}
@override
bool updateShouldNotify(TreeView oldWidget) {
if (oldWidget.children == this.children &&
oldWidget.startExpanded == this.startExpanded) {
return false;
}
return true;
}
}
class _TreeViewData extends StatelessWidget {
final List<Widget> children;
const _TreeViewData({
required this.children,
});
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: children.length,
itemBuilder: (context, index) {
return children.elementAt(index);
},
);
}
}
class TreeViewChild extends StatefulWidget {
final bool? startExpanded;
final Widget parent;
final List<Widget> children;
final VoidCallback? onTap;
TreeViewChild({
required this.parent,
required this.children,
this.startExpanded,
this.onTap,
Key? key,
}) : super(key: key);
@override
TreeViewChildState createState() => TreeViewChildState();
TreeViewChild copyWith(
TreeViewChild source, {
bool? startExpanded,
Widget? parent,
List<Widget>? children,
VoidCallback? onTap,
}) {
return TreeViewChild(
parent: parent ?? source.parent,
children: children ?? source.children,
startExpanded: startExpanded ?? source.startExpanded,
onTap: onTap ?? source.onTap,
);
}
}
class TreeViewChildState extends State<TreeViewChild> {
bool? isExpanded;
@override
void initState() {
super.initState();
isExpanded = widget.startExpanded;
}
@override
void didChangeDependencies() {
isExpanded = widget.startExpanded ?? TreeView.of(context).startExpanded;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
child: widget.parent,
onTap: () {
if (widget.onTap != null) {
widget.onTap!();
}
toggleExpanded();
},
),
AnimatedContainer(
duration: Duration(milliseconds: 400),
child: isExpanded!
? Column(
mainAxisSize: MainAxisSize.min,
children: widget.children,
)
: Offstage(),
),
],
);
}
void toggleExpanded() {
setState(() {
this.isExpanded = !this.isExpanded!;
});
}
}
@@ -0,0 +1,31 @@
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:flutter/material.dart';
class ResponsiveWidget extends StatelessWidget {
final Widget? largeScreen;
final Widget mediumScreen;
final Widget? smallScreen;
final ResponsiveUtils responsiveUtils;
const ResponsiveWidget({
Key? key,
this.largeScreen,
required this.mediumScreen,
this.smallScreen,
required this.responsiveUtils,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
if (responsiveUtils.isLargeScreen(context)) {
return largeScreen ?? mediumScreen;
} else if (responsiveUtils.isMediumScreen(context)) {
return mediumScreen;
} else {
return smallScreen ?? mediumScreen;
}
});
}
}
@@ -0,0 +1,57 @@
import 'package:flutter/material.dart';
abstract class InputDecorationBuilder {
String? prefixText;
String? labelText;
TextStyle? labelStyle;
String? hintText;
TextStyle? hintStyle;
EdgeInsets? contentPadding;
OutlineInputBorder? enabledBorder;
InputDecorationBuilder setPrefixText(String newPrefixText) {
prefixText = newPrefixText;
return this;
}
InputDecorationBuilder setLabelText(String newLabelText) {
labelText = newLabelText;
return this;
}
InputDecorationBuilder setLabelStyle(TextStyle newLabelStyle) {
labelStyle = newLabelStyle;
return this;
}
InputDecorationBuilder setHintText(String newHintText) {
hintText = newHintText;
return this;
}
InputDecorationBuilder setHintStyle(TextStyle newHintStyle) {
hintStyle = newHintStyle;
return this;
}
InputDecorationBuilder setContentPadding(EdgeInsets newContentPadding) {
contentPadding = newContentPadding;
return this;
}
InputDecorationBuilder setEnabledBorder(OutlineInputBorder newEnabledBorder) {
enabledBorder = newEnabledBorder;
return this;
}
InputDecoration build() {
return InputDecoration(
prefixText: prefixText,
labelText: labelText,
labelStyle: labelStyle,
hintText: hintText,
hintStyle: hintStyle,
contentPadding: contentPadding,
enabledBorder: enabledBorder);
}
}
@@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
/// A builder which builds a reusable slogan widget.
/// This contains the logo and the slogan text.
/// The elements are arranged in a column.
class SloganBuilder {
Key? _key;
String? _text;
TextStyle? _textStyle;
TextAlign? _textAlign;
String? _logo;
SloganBuilder key(Key key) {
_key = key;
return this;
}
SloganBuilder setSloganText(String text) {
_text = text;
return this;
}
SloganBuilder setSloganTextStyle(TextStyle textStyle) {
_textStyle = textStyle;
return this;
}
SloganBuilder setSloganTextAlign(TextAlign textAlign) {
_textAlign = textAlign;
return this;
}
SloganBuilder setLogo(String logo) {
_logo = logo;
return this;
}
Widget build() {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_logo != null ? SvgPicture.asset(_logo!, width: 150, height: 150) : SizedBox.shrink(),
Padding(
padding: EdgeInsets.only(top: 16, left: 16, right: 16),
child: Text(_text ?? '', key: _key, style: _textStyle, textAlign: _textAlign),
),
],
);
}
}
@@ -0,0 +1,40 @@
import 'package:core/presentation/utils/style_utils.dart';
import 'package:flutter/material.dart';
class TextBuilder {
String? _text;
TextStyle? _textStyle;
TextAlign? _textAlign;
Key? _key;
TextBuilder key(Key key) {
_key = key;
return this;
}
TextBuilder text(String text) {
_text = text;
return this;
}
TextBuilder textStyle(TextStyle textStyle) {
_textStyle = textStyle;
return this;
}
TextBuilder textAlign(TextAlign textAlign) {
_textAlign = textAlign;
return this;
}
Text build() {
return Text(_text ?? '', key: _key ?? Key('TextBuilder'), style: _textStyle ?? CommonTextStyle.textStyleNormal, textAlign: _textAlign ?? TextAlign.center);
}
}
class CenterTextBuilder extends TextBuilder {
@override
Text build() {
return Text(_text ?? '', key: _key ?? Key('TextBuilder'), style: _textStyle, textAlign: TextAlign.center);
}
}
@@ -0,0 +1,53 @@
import 'package:core/presentation/extensions/color_extension.dart';
import 'package:flutter/material.dart';
class TextFieldBuilder {
Key? _key;
ValueChanged<String>? _onTextChange;
TextStyle? _textStyle;
TextInputAction? _textInputAction;
InputDecoration? _inputDecoration;
bool? _obscureText;
TextFieldBuilder key(Key key) {
_key = key;
return this;
}
TextFieldBuilder onChange(ValueChanged<String> onChange) {
_onTextChange = onChange;
return this;
}
TextFieldBuilder textStyle(TextStyle style) {
_textStyle = style;
return this;
}
TextFieldBuilder textInputAction(TextInputAction inputAction) {
_textInputAction = inputAction;
return this;
}
TextFieldBuilder textDecoration(InputDecoration inputDecoration) {
_inputDecoration = inputDecoration;
return this;
}
TextFieldBuilder obscureText(bool obscureText) {
_obscureText = obscureText;
return this;
}
TextField build() {
return TextField(
key: _key ?? Key('TextFieldBuilder'),
onChanged: _onTextChange,
cursorColor: AppColor.primaryColor,
textInputAction: _textInputAction,
decoration: _inputDecoration,
style: _textStyle ?? TextStyle(color: AppColor.textFieldTextColor),
obscureText: _obscureText ?? false,
);
}
}