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

边栏推荐
- 12. problem set: process, thread and JNI architecture
- 2020-11-02
- Flutter的特别之处在哪里
- JVM tuning tool commands (notes)
- Function simplification principle: save if you can
- JVM memory common parameter configuration set
- Mysq database remote connection error, remote connection is not allowed
- What is the difference between ZigBee, Bluetooth and WiFi (copy and reprint)
- Self service terminal development process
- Acquisition de 100% des actions de Guilin latex par Guilin Robust Medical pour combler le vide de la gamme de produits Latex
猜你喜欢

【新书推荐】Deno Web Development

近期学习遇到的比较问题

JVM tuning tool commands (notes)

Xlnet (generalized autorefressive trainingfor language understanding) paper notes

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

仿照微信Oauth2.0接入方案

云技能提升好伙伴,亚马逊云师兄今天正式营业

Solution to pychart's failure in importing torch package

Self service terminal handwritten Chinese character recognition input method library tjfink introduction

Bloom filter
随机推荐
Pytorch for former Torch users - Tensors
JWT expiration processing - single token scheme
MySQL-- Entity Framework Code First(EF Code First)
Redis docker 主从模式与哨兵sentinel
NTP of Prometheus monitoring_ exporter
直播带货源码开发中,如何降低直播中的延迟?
Function simplification principle: save if you can
【新书推荐】Cleaning Data for Effective Data Science
Utils collaboration
Bluetooth BT RF test (forwarding)
float
布隆过滤器
Electron, which can wrap web page programs into desktop applications
Startup of MySQL green edition in Windows system
Deep Learning with Pytorch- A 60 Minute Blitz
Self service terminal handwritten Chinese character recognition input method library tjfink introduction
ES6 learning road 5 symbol
桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
Research on lg1403 divisor
Linear-gradient()