mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (single-file apps)
Convert 20 more single-file apps to the validated pattern: lib/main.dart holds only the runApp + MaterialApp bootstrap, while a new lib/example.dart holds the full screen (Scaffold, AppBar) and the focused example code. Apps: enabling_splash_screen, getx_counter_app, image_from_network, infinite_list, load_local_image, load_local_json, persist_key_value, push_notifications, sliver_app_bar_example, stateless_widgets, tic_tac_toe, tip_calculator, using_alert_dialog, using_edittext, using_expansionpanel, using_http_get, using_interactiveviewer, using_snackbar, using_stepper, using_theme
This commit is contained in:
@@ -1,73 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(title: 'Tip Calculator', home: TipCalculator()));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class TipCalculator extends StatefulWidget {
|
||||
const TipCalculator({super.key});
|
||||
|
||||
@override
|
||||
State<TipCalculator> createState() => _TipCalculatorState();
|
||||
}
|
||||
|
||||
class _TipCalculatorState extends State<TipCalculator> {
|
||||
double billAmount = 0.0;
|
||||
double tipPercentage = 0.0;
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Create first input field
|
||||
TextField billAmountField = TextField(
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (String value) {
|
||||
try {
|
||||
billAmount = double.parse(value);
|
||||
} catch (exception) {
|
||||
billAmount = 0.0;
|
||||
}
|
||||
},
|
||||
decoration: InputDecoration(labelText: "Bill amount(\$)"),
|
||||
return MaterialApp(
|
||||
title: 'Tip Calculator',
|
||||
home: const Example(),
|
||||
);
|
||||
|
||||
// Create another input field
|
||||
TextField tipPercentageField = TextField(
|
||||
decoration: InputDecoration(labelText: "Tip %", hintText: "15"),
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (String value) {
|
||||
try {
|
||||
tipPercentage = double.parse(value);
|
||||
} catch (exception) {
|
||||
tipPercentage = 0.0;
|
||||
}
|
||||
});
|
||||
|
||||
// Create button
|
||||
ElevatedButton calculateButton = ElevatedButton(
|
||||
child: Text("Calculate"),
|
||||
onPressed: () {
|
||||
// Calculate tip and total
|
||||
double calculatedTip = billAmount * tipPercentage / 100.0;
|
||||
double total = billAmount + calculatedTip;
|
||||
|
||||
// Generate dialog
|
||||
AlertDialog dialog = AlertDialog(
|
||||
content: Text("Tip: \$$calculatedTip \n"
|
||||
"Total: \$$total"));
|
||||
|
||||
// Show dialog
|
||||
showDialog(
|
||||
context: context, builder: (BuildContext context) => dialog);
|
||||
});
|
||||
|
||||
Container container = Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
children: [billAmountField, tipPercentageField, calculateButton]));
|
||||
|
||||
AppBar appBar = AppBar(title: Text("Tip Calculator"));
|
||||
|
||||
Scaffold scaffold = Scaffold(appBar: appBar, body: container);
|
||||
return scaffold;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user