|
|
|
// 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';
|
|
|
|
import 'package:provider_shopper/screens/login.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@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(create: (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>(
|
|
|
|
create: (context) => CartModel(),
|
|
|
|
update: (context, catalog, cart) {
|
|
|
|
if (cart == null) throw ArgumentError.notNull('cart');
|
|
|
|
cart.catalog = catalog;
|
|
|
|
return cart;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
child: MaterialApp(
|
|
|
|
title: 'Provider Demo',
|
|
|
|
theme: appTheme,
|
|
|
|
initialRoute: '/',
|
|
|
|
routes: {
|
|
|
|
'/': (context) => const MyLogin(),
|
|
|
|
'/catalog': (context) => const MyCatalog(),
|
|
|
|
'/cart': (context) => const MyCart(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|