Learn RadioButton in flutter using simple examples and apps.
Example 1: Temperature Conversion App
A simple Temperature Conversion app to introduce you to different widgets including RadioButton.
The radio
widget will be used to choose between Fahrenheit and Celcius.
Step 1: Create Project
Start by creating an empty Flutter Project in your favorite editor or IDE.
Step 2: Add Dependencies
No external packages are needed for this project.
Step 3: Write Code
Here is how you build a container containing two radiobutton widgets:
Container tempSwitch = Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("F"),
Radio<bool>(
groupValue: fOrC,
value: false,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
Text("C"),
Radio<bool>(
groupValue: fOrC,
value: true,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
],
),
);
When an button is clicked we convert and show the result in an alert dialog:
Container calcBtn = Container(
child: RaisedButton(
child: Text("Calculate"),
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(context: context, child: dialog);
},
),
);
Here is the full code:
main.dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
build(BuildContext context) {
return MaterialApp(
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;
@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(
children: <Widget>[
//Text("Choose Fahrenheit or Celsius"),
//Switch(
// value: fOrC,
// onChanged: (e) {
// setState(() {
// fOrC = !fOrC;
// });
// },
//)
//Checkbox(
// value: fOrC,
// onChanged: (e) {
// setState(() {
// fOrC = !fOrC;
// });
// },
//),
Text("F"),
Radio<bool>(
groupValue: fOrC,
value: false,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
Text("C"),
Radio<bool>(
groupValue: fOrC,
value: true,
onChanged: (v) {
setState(() {
fOrC = v;
});
}),
],
),
);
Container calcBtn = Container(
child: RaisedButton(
child: Text("Calculate"),
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(context: context, child: dialog);
},
),
);
return Scaffold(
appBar: appBar,
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
inputField,
tempSwitch,
calcBtn,
],
),
),
);
}
}
Run
Copy the code into your project or download the code in the reference links below.
Reference
Find the reference links below:
Number | Link |
---|---|
1. | Download code |
2. | Follow code author |