I have fetch a temperature converter code from a GitHub repository. I want to show my calculated data history on screen whenever I press Convert button through shared preferences. Can anyone help me out?
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: TempApp(),
);
}
}
class TempApp extends StatefulWidget {
@override
TempState createState() => TempState();
}
class TempState extends State<TempApp> {
double? input;
double? output;
bool? fOrC;
late SharedPreferences prefs;
@override
void initState() {
super.initState();
input = 0.0;
output = 0.0;
fOrC = true;
}
@override
Widget build(BuildContext context) {
TextField inputField = TextField(
keyboardType: TextInputType.number,
onChanged: (str) {
try {
input = double.parse(str);
} catch (e) {
input = 0.0;
}
},
decoration: InputDecoration(
labelText:
"Input a Value in ${fOrC == false ? "Fahrenheit" : "Celsius"}",
),
textAlign: TextAlign.center,
);
AppBar appBar = AppBar(
title: Text("Temperature Calculator"),
);
Container tempSwitch = Container(
padding: EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Fahrenheit"),
Radio<bool>(
groupValue: fOrC,
value: false,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
Text("Celsius"),
Radio<bool>(
groupValue: fOrC,
value: true,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
SizedBox(
height: 90,
)
],
),
);
Container calcBtn = Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size(200, 50),
maximumSize: const Size(200, 50),
textStyle: const TextStyle(fontSize: 20)),
child: Text("Convert"),
onPressed: () {
setState(() {
fOrC == false
? output = (input! - 32) * (5 / 9)
: output = (input! * 9 / 5) + 32;
});
AlertDialog dialog = AlertDialog(
content: fOrC == false
? Text(
"${input?.toStringAsFixed(2)} F : ${output?.toStringAsFixed(2)} C")
: Text(
"${input?.toStringAsFixed(2)} C : ${output?.toStringAsFixed(2)} F"),
);
showDialog(builder: (context) => dialog, context: context);
print('output');
print('input');
},
),
);
return Scaffold(
appBar: appBar,
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
inputField,
tempSwitch,
calcBtn,
],
),
),
);
}
}
I tried of getting string value but I don't understand that how to get read and save data of user input? I am expecting to have a calculated data on screen with input and result data whenever I press the Convert button.