Flutter Assignment
Flutter Assignment
Assignment 1 :- - Develop a Flutter app using Dart programming that allows users to convert
temperatures between Celsius, Fahrenheit, and Kelvin. The app should include an input field for the
temperature value, drop-down menus to select the input and output units, and a button to perform the
conversion. Display the converted temperature result within the app's interface.
Code(main.dart) :-
import 'package:flutter/material.dart';
final List<String> _units = ['Celsius', 'Fahrenheit', 'Kelvin'];
void main() {
runApp(TemperatureConverterApp()); void _convertTemperature() {
} double inputTemp = double.tryParse(_controller.text) ?? 0.0;
double outputTemp = inputTemp;
class TemperatureConverterApp extends StatelessWidget {
static final ValueNotifier<ThemeMode> _themeNotifier = if (_fromUnit == _toUnit) {
ValueNotifier(ThemeMode.light); _result = '$inputTemp $_toUnit';
} else {
@override if (_fromUnit == 'Celsius') {
Widget build(BuildContext context) { if (_toUnit == 'Fahrenheit') {
return ValueListenableBuilder<ThemeMode>( outputTemp = (inputTemp * 9 / 5) + 32;
valueListenable: _themeNotifier, } else if (_toUnit == 'Kelvin') {
builder: (context, currentTheme, child) { outputTemp = inputTemp + 273.15;
return MaterialApp( }
debugShowCheckedModeBanner: false, } else if (_fromUnit == 'Fahrenheit') {
title: 'Temperature Converter', if (_toUnit == 'Celsius') {
theme: ThemeData.light(), outputTemp = (inputTemp - 32) * 5 / 9;
darkTheme: ThemeData.dark(), } else if (_toUnit == 'Kelvin') {
themeMode: currentTheme, outputTemp = (inputTemp - 32) * 5 / 9 + 273.15;
home: TemperatureConverterScreen(), }
); } else if (_fromUnit == 'Kelvin') {
}, if (_toUnit == 'Celsius') {
); outputTemp = inputTemp - 273.15;
} } else if (_toUnit == 'Fahrenheit') {
outputTemp = (inputTemp - 273.15) * 9 / 5 + 32;
static void toggleTheme() { }
_themeNotifier.value = _themeNotifier.value == }
ThemeMode.light
? ThemeMode.dark _result = '${outputTemp.toStringAsFixed(2)} $_toUnit';
: ThemeMode.light; }
}
setState(() {});
static bool isDarkMode() { }
return _themeNotifier.value == ThemeMode.dark;
} @override
} Widget build(BuildContext context) {
bool isDark = TemperatureConverterApp.isDarkMode();
class TemperatureConverterScreen extends StatefulWidget {
@override return Scaffold(
_TemperatureConverterScreenState createState() => appBar: AppBar(
_TemperatureConverterScreenState(); centerTitle: true,
} title: Text('Temperature Converter'),
actions: [
class _TemperatureConverterScreenState // Sun/Moon Toggle Button
extends State<TemperatureConverterScreen> { IconButton(
final TextEditingController _controller = icon: Icon(isDark ? Icons.nightlight_round :
TextEditingController(); Icons.wb_sunny),
String _fromUnit = 'Celsius'; onPressed: () {
String _toUnit = 'Fahrenheit'; TemperatureConverterApp.toggleTheme();
String _result = ''; },
Mobile App Development
3
), Icon(Icons.arrow_forward),
], DropdownButton<String>(
), value: _toUnit,
body: Padding( items: _units.map((String unit) {
padding: const EdgeInsets.all(16.0), return DropdownMenuItem<String>(
child: Column( value: unit,
mainAxisAlignment: MainAxisAlignment.center, child: Text(unit),
children: [ );
TextField( }).toList(),
controller: _controller, onChanged: (String? newValue) {
keyboardType: TextInputType.number, setState(() {
decoration: InputDecoration( _toUnit = newValue!;
labelText: 'Enter Temperature', });
border: OutlineInputBorder(), },
), ),
), ],
SizedBox(height: 16), ),
Row( SizedBox(height: 16),
mainAxisAlignment: MainAxisAlignment.spaceBetween, ElevatedButton(
children: [ onPressed: _convertTemperature,
DropdownButton<String>( child: Text('Convert'),
value: _fromUnit, ),
items: _units.map((String unit) { SizedBox(height: 16),
return DropdownMenuItem<String>( Text(
value: unit, _result,
child: Text(unit), style: TextStyle(fontSize: 20, fontWeight:
); FontWeight.bold),
}).toList(), ),
onChanged: (String? newValue) { ],
setState(() { ),
_fromUnit = newValue!; ),
}); );
}, }
), }
Output:-