base-converter/lib/main.dart

200 lines
5.7 KiB
Dart
Raw Normal View History

2021-09-26 17:28:36 +02:00
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
2021-09-26 20:19:45 +02:00
title: 'Base Converter',
2021-09-26 17:28:36 +02:00
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
2021-09-26 20:19:45 +02:00
home: const MyHomePage(),
2021-09-26 17:28:36 +02:00
);
}
}
class MyHomePage extends StatefulWidget {
2021-09-26 20:19:45 +02:00
const MyHomePage({Key? key}) : super(key: key);
2021-09-26 17:28:36 +02:00
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
2021-09-26 20:19:45 +02:00
String fromBase = "One";
String toBase = "One";
String result = "";
late TextEditingController _controller;
RichText richText = RichText(text: const TextSpan());
@override
void initState() {
super.initState();
_controller = TextEditingController();
2021-09-26 17:28:36 +02:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2021-09-26 20:19:45 +02:00
title: const Text("Base Converter"),
actions: [
IconButton(
onPressed: () => {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("How to use:"),
content: const Text(
"Input the number you want to convert, select the base it is in and the base you want convert it into. Then click \"Calculate\" and the result will appear at the bottom of the screen!",
style: TextStyle(fontSize: 13),
),
contentPadding:
const EdgeInsets.fromLTRB(24, 16, 24, 8),
actions: [
TextButton(
onPressed: () => {Navigator.pop(context)},
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: const [Text("Close")]))
]);
}),
},
icon: const Icon(Icons.info_outline_rounded))
],
),
body: SafeArea(
child: Container(
margin: const EdgeInsets.fromLTRB(24, 32, 24, 32),
child: Column(children: [
mainBody(),
Expanded(
child: Center(child: richText),
)
]),
),
2021-09-26 17:28:36 +02:00
),
2021-09-26 20:19:45 +02:00
);
}
Container mainBody() {
return Container(
height: 320,
margin: const EdgeInsets.all(0),
padding: const EdgeInsets.all(4),
child: Center(
2021-09-26 17:28:36 +02:00
child: Column(
2021-09-26 20:19:45 +02:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
controller: _controller,
decoration: const InputDecoration(hintText: "Number "),
2021-09-26 17:28:36 +02:00
),
2021-09-26 20:19:45 +02:00
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
baseSelector("From:", fromBase),
baseSelector("To:", toBase)
]),
Row(children: [
Expanded(
child: TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.blue)),
onPressed: calculate,
child: const Text("CALCULATE!",
style: TextStyle(color: Colors.white))))
]),
2021-09-26 17:28:36 +02:00
],
),
),
2021-09-26 20:19:45 +02:00
);
}
void calculate() {}
void decimalToBase() {}
void baseToDecimal() {}
Container baseSelector(String labelText, String value) {
return Container(
margin: const EdgeInsets.all(4),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(labelText),
const SizedBox(
width: 10,
),
dpb(value),
],
),
);
}
DropdownButton dpb(String dropdownValue) {
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>[
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Hex',
'BCD',
'2\'s CP'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
2021-09-26 17:28:36 +02:00
);
}
}