Flutter:提供者可以重建模型吗?

时间:2020-09-01 18:29:02

标签: flutter dart flutter-provider

我正在使用provider: ^4.3.2+1,并且在我的应用程序中有一个AddScreen。当我回到HomeScreen并再次导航到AddScreen时,将维护模型的属性。 是否有可能总是在用户导航到AddScreen时重建新的模型?

ROOT

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MultiProvider(
        providers: [
          ChangeNotifierProvider<AttrListModel>.value(
            value: AttrListModel(),
          ),
        ],
        child: MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.purple,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          debugShowCheckedModeBanner: false,
          home: HomeScreen(),
        ),
      );
}

AddScreen-提供商使用情况

class AttrList extends StatelessWidget {
  final List<AttrTileModel> tiles;

  AttrList({@required this.tiles});

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: getChildrenAttrs(),
    );
  }

  List<Widget> getChildrenAttrs() {
    return tiles.map((tile) => AttrListItem(tile: tile)).toList();
  }
}

// Called at AddScreen during Body Build method.
class AllAttrList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Consumer<AttrListModel>(
        builder: (context, attrList, child) =>
            AttrList(tiles: attrList.attrTiles),
      ),
    );
  }
}

0 个答案:

没有答案