Функции
// Some code
import 'package:flutter/material.dart';
class FunctionScreen extends StatefulWidget {
const FunctionScreen({super.key});
@override
State<FunctionScreen> createState() => _FunctionScreenState();
}
class _FunctionScreenState extends State<FunctionScreen> {
String name = 'Baiastan';
double number = 9;
double weight = 98.99;
bool isSunny = false;
bool isNight = true;
// = , +=, -=, /=, *=
//+, -,\, *, ++, --
// Scaffold, Container, Text, Icon, Image,
Scaffold screen = Scaffold(
backgroundColor: Colors.orange,
);
Container cube = Container();
// Переменные - сущест
//
void increment() {
setState(() {
number += 1;
});
}
void decrement() {
setState(() {
number -= 1;
});
}
void divide() {
setState(() {
number /= 2;
});
}
void multiply() {
setState(() {
number *= 2;
});
}
bool isFavorite = false; // false нет - true да
void like() {
setState(() {
isFavorite = !isFavorite;
});
}
bool isObscureText = true;
void hide() {
setState(() {
isObscureText = !isObscureText;
});
}
// Действие на экране
// не повторять один тот же код -
// Elf, Person, Orc, Gnome
String attack = '';
void attackElf() {
setState(() {
attack = 'Эльф лук';
});
}
void attackPerson() {
setState(() {
attack = 'Человек меч';
});
}
void attackOrc() {
setState(() {
attack = 'Орк топор';
});
}
void attackGnome() {
setState(() {
attack = 'Гном молот';
});
}
// Порядковый параметры
void attackI(String title) {
setState(() {
attack = title;
});
}
//
int c = 0;
void sum(int a, int b) {
setState(() {
c = a + b;
});
}
TextEditingController aController = TextEditingController();
TextEditingController bController = TextEditingController();
// Объязательные параметры
void minus({required int a, required int b}) {
setState(() {
c = a - b;
});
}
// Деление - усножение -
// Контейнер - тернарный оператор
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Text(
'$number',
style: TextStyle(fontSize: 30),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {
increment();
},
icon: Icon(Icons.add),
),
ElevatedButton(
onPressed: () {
decrement();
},
child: Text('минус '),
),
TextButton(
onPressed: () {
divide();
},
child: Text('/'),
),
InkWell(
onTap: () {
multiply();
},
child: Text('*'),
),
],
),
IconButton(
onPressed: () {
like();
},
icon: Icon(
isFavorite ? Icons.favorite : Icons.favorite_border,
color: isFavorite ? Colors.red : Colors.black,
size: 50,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
obscureText: isObscureText,
decoration: InputDecoration(
border: OutlineInputBorder(),
suffixIcon: InkWell(
onTap: () {
hide();
},
child: Icon(
isObscureText ? Icons.visibility : Icons.visibility_off,
),
),
),
),
),
SizedBox(
height: 30,
),
Text(
'Атака - $attack',
style: TextStyle(fontSize: 40),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
attackI('Лук');
},
child: Text('Эльф'),
),
ElevatedButton(
onPressed: () {
attackI('Меч');
},
child: Text('Человек'),
),
ElevatedButton(
onPressed: () {
attackI('Топор');
},
child: Text('Орк'),
),
ElevatedButton(
onPressed: () {
attackI('Молот');
},
child: Text('Гном'),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: aController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'a',
),
),
),
SizedBox(
width: 10,
),
Expanded(
child: TextField(
controller: bController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'b',
),
),
),
],
),
),
Text(
'$c',
style: TextStyle(fontSize: 40),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {
sum(
int.tryParse(aController.text)!,
int.tryParse(bController.text)!,
);
},
child: Text('Sum'),
),
ElevatedButton(
onPressed: () {
minus(
b: int.tryParse(bController.text)!,
a: int.tryParse(aController.text)!,
);
},
child: Text('Minus'),
),
],
),
],
),
),
);
}
}Last updated