import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jetpack/jetpack.dart';
import 'package:my_app/Models/ViewModel.dart';
// Entry point of the application
void main() {
runApp(const MyApp());
}
// Main application widget
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
// Disable debug banner
debugShowCheckedModeBanner: false,
// Set home widget
home: const JectPackCounter(),
);
}
}
// Event class for counter increment
class CounterIncrementedEvent extends CounterEvent {}
// Widget for JetPack counter
class JectPackCounter extends StatelessWidget {
const JectPackCounter({super.key});
// Function to handle events
Future<void> onEvent(BuildContext context, CounterEvent event) async {
await Fluttertoast.showToast(
msg: "Counter Incremented", // Message to show
toastLength: Toast.LENGTH_SHORT, // Duration of toast
gravity: ToastGravity.BOTTOM, // Position of toast
backgroundColor: Colors.green, // Background color of toast
textColor: Colors.white, // Text color of toast
fontSize: 16.0); // Font size of toast
}
@override
Widget build(BuildContext context) {
// Get ViewModel
final IncrementCounterModel viewModel = context.viewModel();
return Scaffold(
appBar: AppBar(
title: const Text('JetPack Counter'), // App bar title
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Center vertically
crossAxisAlignment: CrossAxisAlignment.center, // Center horizontally
children: [
EventListener(
eventQueue: viewModel.eventQueue, // Event queue from ViewModel
onEvent: onEvent, // Event handler
child: LiveDataBuilder<int>(
liveData: viewModel.counter, // Live data from ViewModel
builder: (BuildContext buildContext, int count) => Text(
'Count: $count', // Display counter value
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
),
onPressed: viewModel.increment, // Increment counter on press
child: Text(
"Increment Counter",
style: TextStyle(
color: Colors.white,
),
),
)
],
),
),
);
}
}
// Extension to get ViewModel from BuildContext
extension ViewModelExtension on BuildContext {
IncrementCounterModel viewModel() {
return IncrementCounterModel();
}
}
// Abstract class for counter events
abstract class CounterEvent {}