Created
April 6, 2026 11:02
-
-
Save phyowaikyaw-mobiledev/7a3452ca26f4270ada62841782e7e208 to your computer and use it in GitHub Desktop.
Assignment 13 - Logger Class with Colored Logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: LoggerScreen(), | |
| ); | |
| } | |
| } | |
| // Logger Class | |
| class Logger { | |
| static const String _reset = '\x1B[0m'; | |
| static void info(String message) { | |
| print('\x1B[30m[INFO] $message$_reset'); | |
| } | |
| static void success(String message) { | |
| print('\x1B[32m[SUCCESS] $message$_reset'); | |
| } | |
| static void warning(String message) { | |
| print('\x1B[33m[WARNING] $message$_reset'); | |
| } | |
| static void error(String message) { | |
| print('\x1B[31m[ERROR] $message$_reset'); | |
| } | |
| } | |
| // UI Screen | |
| class LoggerScreen extends StatefulWidget { | |
| const LoggerScreen({super.key}); | |
| @override | |
| State<LoggerScreen> createState() => _LoggerScreenState(); | |
| } | |
| class _LoggerScreenState extends State<LoggerScreen> { | |
| final List<Map<String, dynamic>> _logs = []; | |
| void _addLog(String level, String message, Color color) { | |
| setState(() { | |
| _logs.add({'level': level, 'message': message, 'color': color}); | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Logger Demo'), | |
| backgroundColor: Colors.black87, | |
| foregroundColor: Colors.white, | |
| ), | |
| backgroundColor: Colors.grey[100], | |
| body: Column( | |
| children: [ | |
| // Buttons | |
| Padding( | |
| padding: const EdgeInsets.all(16), | |
| child: Wrap( | |
| spacing: 12, | |
| runSpacing: 12, | |
| children: [ | |
| ElevatedButton( | |
| style: ElevatedButton.styleFrom( | |
| backgroundColor: Colors.black87, | |
| foregroundColor: Colors.white, | |
| ), | |
| onPressed: () { | |
| Logger.info('This is an info message'); | |
| _addLog('INFO', 'This is an info message', Colors.black87); | |
| }, | |
| child: const Text('Info'), | |
| ), | |
| ElevatedButton( | |
| style: ElevatedButton.styleFrom( | |
| backgroundColor: Colors.green, | |
| foregroundColor: Colors.white, | |
| ), | |
| onPressed: () { | |
| Logger.success('Operation successful!'); | |
| _addLog('SUCCESS', 'Operation successful!', Colors.green); | |
| }, | |
| child: const Text('Success'), | |
| ), | |
| ElevatedButton( | |
| style: ElevatedButton.styleFrom( | |
| backgroundColor: Colors.orange, | |
| foregroundColor: Colors.white, | |
| ), | |
| onPressed: () { | |
| Logger.warning('This is a warning'); | |
| _addLog('WARNING', 'This is a warning', Colors.orange); | |
| }, | |
| child: const Text('Warning'), | |
| ), | |
| ElevatedButton( | |
| style: ElevatedButton.styleFrom( | |
| backgroundColor: Colors.red, | |
| foregroundColor: Colors.white, | |
| ), | |
| onPressed: () { | |
| Logger.error('An error occurred!'); | |
| _addLog('ERROR', 'An error occurred!', Colors.red); | |
| }, | |
| child: const Text('Error'), | |
| ), | |
| ElevatedButton( | |
| style: ElevatedButton.styleFrom( | |
| backgroundColor: Colors.grey, | |
| foregroundColor: Colors.white, | |
| ), | |
| onPressed: () { | |
| setState(() => _logs.clear()); | |
| }, | |
| child: const Text('Clear'), | |
| ), | |
| ], | |
| ), | |
| ), | |
| // Log Output | |
| Expanded( | |
| child: Container( | |
| margin: const EdgeInsets.all(16), | |
| padding: const EdgeInsets.all(12), | |
| decoration: BoxDecoration( | |
| color: Colors.black, | |
| borderRadius: BorderRadius.circular(10), | |
| ), | |
| child: _logs.isEmpty | |
| ? const Center( | |
| child: Text( | |
| 'Press buttons to see logs...', | |
| style: TextStyle(color: Colors.grey), | |
| ), | |
| ) | |
| : ListView.builder( | |
| itemCount: _logs.length, | |
| itemBuilder: (context, index) { | |
| final log = _logs[index]; | |
| return Padding( | |
| padding: const EdgeInsets.symmetric(vertical: 2), | |
| child: Text( | |
| '[${log['level']}] ${log['message']}', | |
| style: TextStyle( | |
| color: log['color'], | |
| fontFamily: 'monospace', | |
| fontSize: 14, | |
| ), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ), | |
| ), | |
| ], | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment