Provider

作者: 温水煮青蛙a | 来源:发表于2021-02-19 17:34 被阅读0次

    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();
      }
    
    
    }
    

    相关文章

      网友评论

          本文标题:Provider

          本文链接:https://www.haomeiwen.com/subject/mywaxltx.html