Не обновляется вывод переменной после ее изменения в другом методе flutter
Столкнулся с проблемой, что метод, который срабатывает при нажатии на кнопку меняет значения переменных, но в выводе они обновляются только после быстрой перезагрузки всего проекта.
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
class Homepage extends StatefulWidget {
const Homepage({super.key});
@override
State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
int newHeight = 0, newWight = 0, counter = 0;
double x = 0, y = 0;
void rand () {
Random random = Random();
newHeight = random.nextInt(384);
newWight = random.nextInt(200);
counter = counter + 1;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: const Text('Custom Painter App'),
backgroundColor: Colors.indigo,
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Center(
child: Container(
width: 384,
height: 200,
decoration: BoxDecoration(border: Border.all(color: Colors.indigo)),
child: SomeClass(
NewHeight: newHeight.toDouble(),
NewWight: newWight.toDouble(),
counter: counter,
),
),
),
Row(
children: [
Text("""
X: $newHeight
Y: $newWight
№: $counter""",
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.w400,
color: Colors.yellow),
),
],
),
ElevatedButton(style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo,), onPressed: (rand), child: const Text('Новая точка')),
],
),
),
);
}
}
В другом проекте писал точно так же и все работало(при нажатии на кнопку добавлялся символ к строке и в выводе она обновлялась автоматически после каждого нажатия). Кто знает в чем может быть проблема? Заранее спасибо.
Источник: Stack Overflow на русском