mirror of
https://github.com/flutter/samples.git
synced 2026-05-29 18:38:57 +00:00
Add provider_shopper (#87)
This is the code for the sample at https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple.
This commit is contained in:
44
provider_shopper/lib/main.dart
Normal file
44
provider_shopper/lib/main.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider_shopper/common/theme.dart';
|
||||
import 'package:provider_shopper/models/cart.dart';
|
||||
import 'package:provider_shopper/models/catalog.dart';
|
||||
import 'package:provider_shopper/screens/cart.dart';
|
||||
import 'package:provider_shopper/screens/catalog.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Using MultiProvider is convenient when providing multiple objects.
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
// In this sample app, CatalogModel never changes, so a simple Provider
|
||||
// is sufficient.
|
||||
Provider(builder: (context) => CatalogModel()),
|
||||
// CartModel is implemented as a ChangeNotifier, which calls for the use
|
||||
// of ChangeNotifierProvider. Moreover, CartModel depends
|
||||
// on CatalogModel, so a ProxyProvider is needed.
|
||||
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
||||
builder: (context, catalog, previousCart) =>
|
||||
CartModel(catalog, previousCart)),
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: 'Provider Demo',
|
||||
theme: appTheme,
|
||||
initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => MyCatalog(),
|
||||
'/cart': (context) => MyCart(),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user