ChangeNotifierProvider
import 'package:flutter/material.dart';
import 'package:controller/aaa_state.dart';
import 'package:provider/provider.dart';
class AAA extends StatefulWidget {
@override
_AAAState createState() => _AAAState();
}
class _AAAState extends State<AAA> {
AAASate _aaaSate = AAASate();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("AAA"),
),
body: ChangeNotifierProvider(
create: (_) => _aaaSate,
child: Consumer(builder: (BuildContext context, AAASate value, Widget child) {
return Column(
children: [
Text("num== ${_aaaSate.num}"),
GestureDetector(
onTap: () {
_aaaSate.setNum(n: _aaaSate.num+1);
},
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
)
],
);
}),
),
);
}
}
import 'package:flutter/cupertino.dart';
class AAASate extends ChangeNotifier {
int _num = 0;
int get num => _num;
void setNum({int n}) {
_num = n;
notifyListeners();
}
}
网友评论