Skip to content

Instantly share code, notes, and snippets.

@zenz
Last active April 18, 2020 11:59
Show Gist options
  • Select an option

  • Save zenz/9b72a8249dec808fad9f4456f3153d1f to your computer and use it in GitHub Desktop.

Select an option

Save zenz/9b72a8249dec808fad9f4456f3153d1f to your computer and use it in GitHub Desktop.
from udemy dart & flutter course
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BillSplitter(),
);
}
}
class BillSplitter extends StatefulWidget {
@override
_BillSplitterState createState() => _BillSplitterState();
}
class _BillSplitterState extends State<BillSplitter> {
int _tipPercentage = 0;
int _personCounter = 1;
double _billAmount = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.1),
color: Colors.white,
child: ListView(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(20.5),
children: <Widget>[
Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.purple.withOpacity(0.1),
borderRadius: BorderRadius.circular(15.0),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'人均分摊',
style: TextStyle(
//fontWeight: FontWeight.bold,
fontSize: 15.0,
color: Colors.purple,
),
),
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
' \¥ ${calculateTotalPerPerson(_billAmount, _personCounter, _tipPercentage)}',
style: TextStyle(
fontSize: 34.9,
fontWeight: FontWeight.bold,
color: Colors.purple,
),
),
),
],
),
),
),
Container(
margin: EdgeInsets.only(top: 20.0),
padding: EdgeInsets.all(12.0),
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
color: Colors.blueGrey,
style: BorderStyle.solid,
),
borderRadius: BorderRadius.circular(12.0),
),
child: Column(
children: <Widget>[
TextField(
keyboardType:
TextInputType.numberWithOptions(decimal: true),
style: TextStyle(color: Colors.purple),
decoration: InputDecoration(
suffixText: '消费总额',
prefixText: '¥',
),
onChanged: (String value) {
try {
_billAmount = double.parse(value);
} catch (exception) {
_billAmount = 0.0;
}
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'分摊',
style: TextStyle(
color: Colors.grey.shade700,
),
),
Row(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
if (_personCounter > 1) {
_personCounter--;
}
});
},
child: Container(
width: 40.0,
height: 40.0,
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
color: Colors.purple.withOpacity(0.1),
),
child: Center(
child: Text(
'-',
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0,
),
),
),
),
),
Text(
'$_personCounter',
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0,
),
),
InkWell(
onTap: () {
setState(() => _personCounter++);
},
child: Container(
width: 40.0,
height: 40.0,
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
color: Colors.purple.withOpacity(0.1),
),
child: Center(
child: Text(
'+',
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0,
),
),
),
),
),
],
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'小费',
style: TextStyle(color: Colors.grey.shade700),
),
Padding(
padding: const EdgeInsets.all(18.0),
child: Text(
' \¥ $_tipPercentage',
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0,
),
),
),
],
),
Column(
children: <Widget>[
Text(
'$_tipPercentage%',
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0,
),
),
Slider(
min: 0,
max: 100,
activeColor: Colors.purple,
inactiveColor: Colors.grey,
divisions: 20,
value: _tipPercentage.toDouble(),
onChanged: (double value) {
setState(() => _tipPercentage = value.round());
},
),
],
),
],
),
)
],
),
),
);
}
String calculateTotalPerPerson(
double billAmount, int splitBy, int tipPercentage) {
var totalPerPerson = (tipPercentage + billAmount) / splitBy;
return totalPerPerson.toStringAsFixed(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment