rehana 0.0.4 rehana: ^0.0.4 copied to clipboard
Rehana Flutter App Engine is a flutter kit which contains ready to use components.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rehana/dialogs/about_app.dart';
import 'package:rehana/dialogs/base.dart';
import 'package:rehana/dialogs/info.dart';
import 'package:rehana/dialogs/loading.dart';
import 'package:rehana/helpers/clipboard.dart';
import 'package:rehana/helpers/console.dart';
import 'package:rehana/helpers/security.dart';
import 'package:rehana/helpers/toast.dart';
import 'package:rehana/services/navigation.dart';
import 'package:rehana/services/dialog.dart';
import 'package:rehana/services/url.dart';
import 'package:rehana/services/search.dart';
import 'package:rehana/models/label_value.dart';
import 'package:rehana/models/app_bar_item.dart';
import 'package:rehana/widgets/drop_down_menu.dart';
import 'package:rehana/widgets/flexible_textfield.dart';
import 'package:rehana/locator.dart';
import 'package:rehana/themes.dart';
/// Example Service
class MyService {
info() {
console(ConsoleLevel.Info, "Hello World!");
}
}
/// Example Widget
class MyWidget extends StatefulWidget {
final String completerId;
final dynamic document;
MyWidget({Key key, this.completerId, this.document}) : super(key: key);
@override
_MyWidgetState createState() => new _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
_closeDialog() {
ServiceManager<DialogService>().completeDialog(
context: context,
completerId: widget.completerId,
result: {'success': true});
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.document['title']),
actionsPadding: EdgeInsets.all(0),
titlePadding: EdgeInsets.fromLTRB(16, 16, 16, 0),
contentPadding: EdgeInsets.fromLTRB(16, 0, 16, 0),
content: Container(),
actions: <Widget>[
RaisedButton(
child: Text("Okay"),
onPressed: () {
/// Okay pressed
},
),
RaisedButton(
child: Text("Cancel"),
onPressed: () {
this._closeDialog();
},
),
],
);
}
}
/// Example Dialog showing usage of BaseDialog to create custom dialogs
class MyDialog extends BaseDialog {
@override
void show(BuildContext context, String completerId, {dynamic data}) {
showDialog(
context: context,
builder: (context) => MyWidget(
completerId: "CompleterId", document: {'title': 'MyWidget'}));
}
}
/// Using themes
/// rehana comes with a few default themes which can be accessed using
class MyTheme {
static ThemeData myTheme = RehanaThemes.themeRed;
}
/// Using various models
/// LabelValuePair
List<LabelValuePair> myLabelValuePairs = [
LabelValuePair(label: 'Arts', value: 'Arts'),
LabelValuePair(label: 'Business', value: 'Business'),
LabelValuePair(label: 'Development', value: 'Development'),
LabelValuePair(label: 'Other', value: 'Other')
];
/// Using various Dialogs
/// Rehana comes with many dialogs which can be used as follows
class DialogsExamples {
showAboutDialog(context) {
ServiceManager<DialogService>().showDialog(context, new AboutAppDialog(),
data: {'title': 'My Application', 'text': 'I write great software!'});
}
/// Loading Dialog
loadingDialogExample(context) {
String _loadingDialogCompleterId = ServiceManager<DialogService>()
.showDialog(context, new LoadingDialog(),
data: {'text': 'Logging in...'});
/// perform task
ServiceManager<DialogService>().completeDialog(
context: context, completerId: _loadingDialogCompleterId, result: null);
}
/// Info Dialog
infoDialogExample(context) {
ServiceManager<DialogService>().showDialog(context, new InfoDialog(),
data: {'title': 'Error', 'text': 'Please fill all fields!'});
ServiceManager<DialogService>().showDialog(context, new InfoDialog(),
data: {'title': 'Success', 'text': 'Signup successful!'});
}
}
/// Services
/// rehana includes many services
class ServicesExamples {
/// ServiceManager uses locator to register services which can be accessed throughtout application
/// To setup locator and set credential key
void serviceManagerExample() {
/// Initialize locators (ServiceManager)
setupRehanaLocators();
/// Add any extra service that you have created
ServiceManager.registerLazySingleton(() => MyService());
/// Run application
/// runApp(Application());
}
/// Navigation Service
navigationServiceExample() async {
/// NavigationService contains one Navigator key
MaterialApp(
title: "Title",
navigatorKey: ServiceManager<NavigationService>().navigatorKey);
/// Go back
ServiceManager<NavigationService>().goBack();
ServiceManager<NavigationService>().goBack(result: true);
/// Navigate to route
ServiceManager<NavigationService>().navigateTo("HOME");
/// Wait for result
dynamic result =
await ServiceManager<NavigationService>().navigateTo("LOGINSIGNUP");
console(ConsoleLevel.Info, result);
}
/// Dialog Service
dialogServiceExample(BuildContext context) {
String completerId = ServiceManager<DialogService>()
.showDialog(context, new MyDialog(), data: null);
ServiceManager<DialogService>().onDialogComplete(completerId).then((value) {
if (value != null && value['success'] == true) {
console(ConsoleLevel.Info, 'Success!');
} else {
console(ConsoleLevel.Error, 'Error');
}
});
}
/// SearchService
seachServiceExample() {
/// Get stream
ServiceManager<SearchService>().stream$;
/// Get current value
ServiceManager<SearchService>().current;
/// Update value
ServiceManager<SearchService>().update("search keyword");
}
/// UrlService
urlServiceExample() async {
bool result =
await ServiceManager<UrlService>().openUrl("https://google.com/");
console(ConsoleLevel.Info, result.toString());
}
}
/// Helpers
helpersExample(BuildContext context) {
/// Console
console(ConsoleLevel.Info, 'Hello World!');
/// Toast
showToast(context, "Not implemented yet!");
/// Clipboard
copyTextToClipboard("Hi!");
copyToClipboard(new ClipboardData());
/// Security
/// Security helper uses AES 256 bit encryption
/// so the passwords/keys need to be multiple of 16
/// Initialize Security module (16 characters)
Security.initialize("___AES_IV_KEY___");
/// Descrypt text
String decryptedResult = Security.decrypt("EncText", "1234567890123456");
console(ConsoleLevel.Debug, decryptedResult);
/// Encrypt text
String encryptedResult = Security.encrypt("Text", "1234567890123456");
console(ConsoleLevel.Debug, encryptedResult);
}
class WidgetsExamples {
/// Flexible Text Field
var _passwordVisible = false;
var _passwordTextEditingController = new TextEditingController();
_togglePasswordVisible() {
/// If using in a widget
/// setState(() {
/// this._passwordVisible = !this._passwordVisible;
/// });
this._passwordVisible = !this._passwordVisible;
}
flexibleTextFieldExample() {
return FlexibleTextField(
readOnly: false,
fixedLengths: [16],
decoration: InputDecoration(
labelText: 'Password',
suffixIcon: IconButton(
icon: Icon(
_passwordVisible ? Icons.visibility : Icons.visibility_off,
semanticLabel:
_passwordVisible ? 'Hide Password' : 'Show Password',
),
onPressed: () {
this._togglePasswordVisible();
},
)),
obscureText: !this._passwordVisible,
controller: this._passwordTextEditingController,
);
}
/// DropDownMenuWidget
dropDownWidgetExample() {
return new DropDownMenuWidget(
value: 'Arts',
itemList: myLabelValuePairs,
onChanged: (value) => {
/// Do something
},
);
}
//AppBarItem
popupMenuButtonExample() {
return new PopupMenuButton<AppBarItem>(
itemBuilder: (BuildContext context) {
return [
AppBarItem(title: 'Delete', icon: Icons.delete),
AppBarItem(title: 'Logout', icon: Icons.power_settings_new)
].map((AppBarItem item) {
return PopupMenuItem<AppBarItem>(
value: item,
child: Row(children: [
Icon(
item.icon,
color: RehanaThemes.themeRed.primaryColor,
),
Padding(
padding: EdgeInsets.fromLTRB(8, 0, 0, 0),
child: Text(item.title),
)
]));
}).toList();
},
);
}
}