In the code below, only one 'build' will be print every time multiple 'details' are print. I want it to print one 'build' every time it print one'details'. What should I do?
In the code below, only one 'build' will be print every time multiple 'details' are print. I want it to print one 'build' every time it print one'details'. What should I do?
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyApp();
}
}
class _MyApp extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Material(
child: FlutterTest(),
),
);
}
}
class FlutterTest extends StatefulWidget {
@override
_FlutterTestState createState() => _FlutterTestState();
}
class _FlutterTestState extends State<FlutterTest> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => Controller(),
child: Demo(),
);
}
}
class Controller extends ChangeNotifier {
int count = 0;
void countIncrement() {
count++;
notifyListeners();
}
}
class Demo extends StatefulWidget {
@override
_DemoState createState() => _DemoState();
}
class _DemoState extends State<Demo> {
@override
Widget build(BuildContext context) {
print("build");
return GestureDetector(
child: Container(
color: Colors.yellow,
alignment: Alignment.center,
width: 300,
height: 300,
child: Text(context.watch<Controller>().count.toString()),
),
onScaleUpdate: (details) {
print("details");
context.read<Controller>().countIncrement();
},
);
}
}