activity 1.5.2+4 activity: ^1.5.2+4 copied to clipboard
A simple multiplatform State Manager that allows the full power of MVC with ZERO Packages.
import 'package:example/controller.dart';
import 'package:example/task_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:activity/activity.dart';
import 'model.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// try {
//
// ActiveSocket activeSocket = WebSocket();
// activeSocket.open('wss://demo.piesocket.com/v3/channel_123?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV¬ify_self');
// activeSocket.onSuccess(() {
// print("onSuccess");
// });
// activeSocket.onFailure(() {
// print("onFailure");
// });
// activeSocket.onMessage((data) {
// print('onMessage @@@');
// print(data);
// if(data == 'Wewe ni'){
// activeSocket.send('Fala....');
// }
// print('onMessage');
// });
// activeSocket.onClose(() {
// print('onClose');
// });
//
ActiveRequest activeRequest = ActiveRequest();
activeRequest.setUp = RequestSetUp(
idleTimeout: 10,
connectionTimeout: 10,
logResponse: true,
withTrustedRoots: true,
);
ActiveResponse activeResponse = await activeRequest
.getApi(Params(endpoint: 'https://catfact.ninja/fact'));
//
// } catch (error){
// printError(error);
// }
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Activity Task App',
theme: ThemeData(primarySwatch: Colors.blue),
home: Activity(
MainController(),
onActivityStateChanged: ()=>
DateTime.now().microsecondsSinceEpoch.toString(),
child: TaskView(
activeController: TaskController(),
),
),
);
}
}
class TaskView extends ActiveView<TaskController> {
const TaskView({super.key, required super.activeController});
@override
ActiveState<ActiveView<ActiveController>, TaskController> createActivity() =>
_TaskViewState(activeController);
}
class _TaskViewState extends ActiveState<TaskView, TaskController> {
_TaskViewState(super.activeController);
@override
void initState() {
// TODO: implement initState
super.initState();
activeController.initCalculations();
activeController.validateJSON();
}
// late MainController mainController;
// @override
// void didChangeDependencies() {
// mainController = Activity.getActivity<MainController>(context);
// super.didChangeDependencies();
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: true,
title: ActiveString('Prop Title', typeName: 'appTitle').toString(),
theme: ThemeData(
primarySwatch: activeController.tasksLevel.value > 100 ? Colors.red : Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
backgroundColor: activeController.appBackgroundColor.value,
appBar: AppBar(
title: Text(activeController.appTitle.value),
),
body: SafeArea(
child: Column(
children: [
TextButton(
onPressed: () {
double width = context.size!.width;
double height = context.size!.height;
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Add User Task'),
content: SizedBox(
width: width,
height: height,
child: userTaskForm(activeController.globalKey)),
actions: <Widget>[
TextButton(
onPressed: () =>
Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context, 'OK');
if (activeController.userName.text.isNotEmpty &&
activeController.userEmail.text.isNotEmpty &&
activeController.taskName.text.isNotEmpty &&
activeController.taskBody.text.isNotEmpty) {
activeController.saveEntry();
}
},
child: const Text('OK'),
),
],
),
);
},
child: const Text('Add Task')),
Container(
margin: const EdgeInsets.only(
top: 10, bottom: 10, right: 10, left: 10),
child: Card(
child: SizedBox(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(activeController.tasks.length.toString()),
const Text(
'Total tasks',
style: TextStyle(fontSize: 8),
),
],
),
const SizedBox(width: 50,),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(activeController.tasksLevel.toString()),
const Text(
'Total task level : Max > 100',
style: TextStyle(fontSize: 8),
),
],
),
],
),
),
),
),
ifRunning(
const CircularProgressIndicator(),
otherwise: Expanded(
child: Align(
alignment: Alignment.topCenter,
child: ListView.builder(
reverse: true,
shrinkWrap: true,
itemCount: activeController.tasks.length,
itemBuilder: (context, i) {
ActiveModel<Task> taskModel =
activeController.tasks[i];
return ListTile(
title: Text(taskModel.value.name),
subtitle: Text(taskModel.value.body),
leading: IconButton(
onPressed: () {
activeController.userName.text = taskModel.value.user.name;
activeController.userEmail.text = taskModel.value.user.email!;
activeController.taskName.text = taskModel.value.name;
activeController.taskBody.text = taskModel.value.body;
activeController.taskLevel.text = taskModel.value.level.toString();
showDialog<String>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(
'Edit ${taskModel.value.name} Task'),
content: SizedBox(
height: 600,
width: 600,
child: userTaskForm(activeController.globalKey)),
actions: <Widget>[
TextButton(
onPressed: ()
{
/**
* Clear fields and refresh Page
*/
activeController.userName.clear();
activeController.userEmail.clear();
activeController.taskName.clear();
activeController.taskBody.clear();
activeController.taskLevel.clear();
Navigator.pop(context, 'Cancel');
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
if (activeController.userName.text.isNotEmpty &&
activeController.userEmail.text.isNotEmpty &&
activeController.taskName.text.isNotEmpty &&
activeController.taskLevel.text.isNotEmpty &&
activeController.taskBody.text.isNotEmpty) {
activeController.editUserTask(activeController.tasks[i], i);
}
Navigator.pop(context, 'Cancel');
},
child: const Text('SUBMIT'),
),
],
);
});
},
icon: const Icon(Icons.edit)),
trailing: IconButton(
onPressed: () {
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(taskModel.value.name),
content: const Text(
'Are you sure you want to delete this task.?'),
actions: <Widget>[
TextButton(
onPressed: () =>
Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context, 'OK');
activeController.deleteUserTask(activeController.tasks[i]);
},
child: const Text('OK'),
),
],
),
);
},
icon: const Icon(
Icons.delete,
color: Colors.deepOrange,
)),
);
},
),
))
)
],
)),
),
);
}
Widget userTaskForm(GlobalKey globalKey) {
return Form(
key: globalKey,
child: Container(
margin: const EdgeInsets.all(10),
child: ListView(
children: [
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'User Name',
),
controller: activeController.userName,
// validator: (value) {
// if (value == null || value.isEmpty) {
// return 'Please enter User Name';
// }
// return null;
// },
),
const Divider(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'User Email',
),
controller: activeController.userEmail,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter User Email';
}
return null;
},
),
const Divider(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Task Name',
),
controller: activeController.taskName,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter Task Name';
}
return null;
},
),
const Divider(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Task Body',
),
controller: activeController.taskBody,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter Task Body';
}
return null;
},
),
const Divider(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Task Level',
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r"[0-9]"),
)
],
controller: activeController.taskLevel,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter Task Level';
}
return null;
},
),
const Divider(
height: 20,
)
],
),
));
}
}