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

边栏推荐
- JVM tuning tool introduction and constant pool explanation
- QR code generation and analysis
- JVM memory common parameter configuration set
- Configuring MySQL for error reporting
- 单片机 MCU 固件打包脚本软件
- ES6 learning path (II) let & const
- utils 协程
- 【新书推荐】Cleaning Data for Effective Data Science
- JWT expiration processing - single token scheme
- Numpy (constant)
猜你喜欢

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

训练一个图像分类器demo in PyTorch【学习笔记】

Distributed ID

Express - static resource request

Abstract classes and interfaces

JVM tuning tool commands (notes)

GPT (improving language understanding generative pre training) paper notes

I once met a girl whom I most wanted to take care of all my life. Later... No later

Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)

布隆过滤器
随机推荐
布隆过滤器
Work notes: SendTo failed errno 22
Pytorch graduate warm LR installation
Xlnet (generalized autorefressive trainingfor language understanding) paper notes
Properties of string
Solution to pychart's failure in importing torch package
MySQL index optimization miscellaneous
2021-10-20
Pipe pipe --namedpipe and anonymouspipe
JVM memory common parameter configuration set
Deberta (decoding enhanced Bert with distinguished attention)
Niuke IOI weekly competition 20 popularization group (problem solving)
Deep Learning with Pytorch- A 60 Minute Blitz
小程序开发踩坑之旅
ReturnJson,让返回数据多一些自定义数据或类名
Generate directory in markdown
JVM tuning tool introduction and constant pool explanation
Challenge transform() 2D
5. Messager framework and imessager interface
NTP of Prometheus monitoring_ exporter