Flutter,如何使用两个ScopedModels?

时间:2019-02-19 08:39:49

标签: flutter

如果我将它们组合在一起,效果很好,但是如果有多个ScopedModels,则有助于轻松阅读代码。我在启动时需要ScopedModel,并且在启动时需要ScopedModel时有一个不同的ScopedModel。就性能而言,拥有单个ScopedModel或多个ScopedModel更好吗?谢谢。

void main() {
      runApp(new MyApp());
    }

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ScopedModel<MyIntModel>(
            model: MyIntModel(),
            child: MaterialApp(
                home: MySquare()));
      }
    }

    class MyIntModel extends Model{
      int intModel = 0;

      void updateInt(){
        intModel = intModel + 1;
        notifyListeners();
      }

    }

    class MyStringModel extends Model{
      String strModel = 'empty';
      int ctr = 0;

      void updateStr(){
        strModel =  'str' + ctr.toString();
        print('strModel: $strModel');
        ctr = ctr + 1;
        notifyListeners();
      }
    }

    class MySquare extends StatelessWidget{
      @override
      Widget build(BuildContext context) {

        return ScopedModelDescendant<MyIntModel>(
          builder: (context, child, model) => Scaffold(
            appBar: AppBar(title: Text('Scoped Model'),),
            body: Center(
              child: Container(width: 300.0,height: 350.0,
                color: Colors.blueGrey,
                child: Stack(
                  children: <Widget>[
                    Center(
                      child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          /// MyIntModel()
                          Text(model.intModel.toString(),
                            style: TextStyle(fontSize: 40.0),),
                          /// MyStringModel()
                          ScopedModel<MyStringModel>(
                            model: MyStringModel(),
                            child: ScopedModelDescendant<MyStringModel>(
                              builder: (context, child, model) =>
                                  Text(model.strModel,
                                style: TextStyle(fontSize: 40.0),),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Align(alignment: Alignment.bottomCenter,
                      child: ButtonBar(children: <Widget>[
                        RaisedButton(child: Text('int Model'),
                            onPressed: model.updateInt),
                        ScopedModel<MyStringModel>(model: MyStringModel(),
                          child: ScopedModelDescendant<MyStringModel>(
                              builder: (context, child, model) =>
                                  RaisedButton(child: Text('str Model'),
                              onPressed: model.updateStr)),
                        ),
                      ],),
                    )
                  ],
                ),),
            ),
          ),
        );
      }
    }

0 个答案:

没有答案