当前位置:网站首页>Valuenotifier and valuelistenablebuilder in fluent
Valuenotifier and valuelistenablebuilder in fluent
2022-06-30 09:44:00 【InfoQ】

About setState
setState() To update our screen UIsetState(() { _myState = newValue; });
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Demo',
home: TestScreen(),
);
}
}
class TestScreen extends StatefulWidget {
const TestScreen({Key? key}) : super(key: key);
@override
_TestScreenState createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
int counter = 0;
int buildCount = 0;
@override
Widget build(BuildContext context) {
log('-----build called------');
buildCount++;
return Scaffold(
appBar: AppBar(
title: Text('$buildCount'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Build method called ...',
style: TextStyle(fontSize: 24.0, color: Colors.grey),
),
const SizedBox(
height: 5.0,
),
Text(
'$buildCount times',
style: const TextStyle(fontSize: 20.0, color: Colors.black),
),
const SizedBox(
height: 20.0,
),
const Text(
'Counter',
style: TextStyle(fontSize: 24.0, color: Colors.grey),
),
const SizedBox(
height: 5.0,
),
Text(
'$counter times',
style: const TextStyle(fontSize: 20.0, color: Colors.black),
),
const SizedBox(
height: 50.0,
),
ElevatedButton(
onPressed: () => setState(() {
buildCount = 0;
counter = 0;
}),
child: const Text('Reset'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
counter++;
}),
child: const Icon(Icons.add),
),
);
}
}
Value notifier
ValueNotifiersetStateStatefulWidgetValueNotitierValueNotifier< Your data type > variable_name = ValueNotifier(initial_value);
ValueNotifier<int> counter = ValueNotifier(0);
ValueListenableBuilder
const ValueListenableBuilder({
required this.valueListenable,
required this.builder,
this.child,
})
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Demo',
home: TestScreen(),
);
}
}
class TestScreen extends StatefulWidget {
const TestScreen({Key? key}) : super(key: key);
@override
_TestScreenState createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
ValueNotifier<int> counter = ValueNotifier(0);
int buildCount = 0;
@override
void dispose() {
counter.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
log('-----build called------');
buildCount++;
return Scaffold(
appBar: AppBar(
title: Text('$buildCount'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Build method called ...',
style: TextStyle(fontSize: 24.0, color: Colors.grey),
),
const SizedBox(
height: 5.0,
),
Text(
'$buildCount times',
style: const TextStyle(fontSize: 20.0, color: Colors.black),
),
const SizedBox(
height: 20.0,
),
const Text(
'Counter',
style: TextStyle(fontSize: 24.0, color: Colors.grey),
),
const SizedBox(
height: 5.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ValueListenableBuilder<int>(
valueListenable: counter,
builder: (context, value, child) => Text(
'${counter.value} times',
style: const TextStyle(fontSize: 20.0, color: Colors.black),
),
),
const SizedBox(
width: 10.0,
),
const Icon(
Icons.animation_rounded,
color: Colors.black,
),
],
),
const SizedBox(
height: 50.0,
),
ElevatedButton(
onPressed: () {
buildCount = 0;
counter.value = 0;
},
child: const Text('Reset'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
counter.value++;
},
child: const Icon(Icons.add),
),
);
}
}
ValueNotifier<int> counter = ValueNotifier(0);
@override
void dispose() {
super.dispose();
counter.dispose();
}
ValueListenableBuilder<int>(
valueListenable : counter,
builder : ( context , value , child ) => Text(
'${ counter . value } times',
style : const TextStyle( fontSize : 20.0, color : Colors.black),
),
),

边栏推荐
- Linear-gradient()
- Enum demo
- Distributed session
- Self service terminal development process
- 3.集成eslint、prettier
- Design of mfc+mysql document data management system based on VS2010
- Electron, which can wrap web page programs into desktop applications
- Thrift easy to use
- Express の post request
- Torch learning summary
猜你喜欢

Applet learning path 2 - event binding

Microsoft. Bcl. Async usage summary -- in Net framework 4.5 project Net framework version 4.5 and above can use async/await asynchronous feature in C 5

布隆过滤器

【Ubuntu-redis安装】

【新书推荐】Deno Web Development

2021-10-20

八大排序(二)

目标检测yolov5开源项目调试

Flutter 中的 ValueNotifier 和 ValueListenableBuilder

Cb/s Architecture - Implementation Based on cef3+mfc
随机推荐
机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)
POJ 1753 flip game (DFS 𞓜 bit operation)
utils 协程
Solution to the eighth training competition of 2020 Provincial Games
Cronexpression expression explanation and cases
工作小记: sendto失败 errno 22
Task summary in NLP
Xlnet (generalized autorefressive trainingfor language understanding) paper notes
MySQL explain
About the smart platform solution for business hall Terminal Desktop System
Using OpenCV Net for image restoration
Linear-gradient()
Flutter的特别之处在哪里
Initialize static resource demo
Distributed ID
【Ubuntu-redis安装】
Review the old and know the new
Pass anonymous function to simplification principle
JWT expiration processing - single token scheme
Deep Learning with Pytorch - autograd