当前位置:网站首页>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),
),
),

边栏推荐
- 银河麒麟server-V10配置镜像源
- Flutter 中的 ValueNotifier 和 ValueListenableBuilder
- ES6 learning path (IV) operator extension
- 12. problem set: process, thread and JNI architecture
- I'm late for school
- Cronexpression expression explanation and cases
- Deep Learning with Pytorch-Train A Classifier
- Express file download
- MySQL index optimization miscellaneous
- Initialize static resource demo
猜你喜欢

Express の Hello World

MySQL index and data storage structure foundation

JVM notes (III): analysis of JVM object creation and memory allocation mechanism

oracle跨数据库复制数据表-dblink

近期学习遇到的比较问题
Recommend a very easy-to-use network communication framework HP socket

Xlnet (generalized autorefressive trainingfor language understanding) paper notes

MySQL internal component structure

小程序手持弹幕的原理及实现(uni-app)

【新书推荐】MongoDB Performance Tuning
随机推荐
qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
2020-11-02
utils 协程
Flutter 0001, environment configuration
Numpy (data type)
OCX child thread cannot trigger event event (forward)
8.8 heap insertion and deletion
Pytorch for former Torch users - Tensors
thrift简单使用
Redis docker master-slave mode and sentinel
Express file upload
Initialize static resource demo
utils session&rpc
Electron, which can wrap web page programs into desktop applications
I once met a girl whom I most wanted to take care of all my life. Later... No later
直播带货源码开发中,如何降低直播中的延迟?
Cronexpression expression explanation and cases
Why won't gold depreciate???
ES6 learning path (II) let & const
Oracle cross database replication data table dblink