반응형
ValueNotifier 선언
final ValueNotifier<int> _counter = ValueNotifier<int>(0);
데이터 커스텀 알리미..
//데이터 선언
class MyValuesObject{
String name;
int age;
ui.Image? thumbnail;
}
//알리미 선언
class MyNotifier extends ValueNotifier<MyValuesObject>{
MyNotifier(MyValuesObject value) : super(value);
}
==>ValueNotifier 에는 value getter/setter와 tostring() 만 있음.
상위 클래스로 가면 notify관련 메소드들이 있음.
//알리미 변수 선언
final MyNotifier _myNotifier = MyNotifier(MyValuesObject());
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 MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final ValueNotifier<int> _counter = ValueNotifier<int>(0);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("aaaaaa")
),
body: Center(
child: ValueListenableBuilder<int>(
builder: (BuildContext context, int value, Widget? child) {
//_counter가 업데이트 될때만 builder 함수가 호출됨
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('$value'),
child!, //밑에 파라미터로 넘긴 child
],
);
},
valueListenable: _counter,
child: const Text('Good job!'),
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.plus_one),
onPressed: () => _counter.value += 1,
),
);
}
}
https://api.flutter.dev/flutter/widgets/ValueListenableBuilder-class.html
https://medium.com/flutter-community/flutter-notifications-bubble-up-and-values-go-down-c1a499d22d5f
'flutter.widget' 카테고리의 다른 글
Builder (0) | 2019.11.27 |
---|---|
[내용추가 필요]PageView (0) | 2019.11.25 |
Card (0) | 2019.11.21 |
탭사용하기 (TabBar, Tab, TabBarView, TabController) (0) | 2019.11.20 |
class (0) | 2019.11.20 |