Dardila11
4/21/2020 - 3:31 AM

Provider

example using Provider

import 'package:flutter/material.dart';
import 'package:flutter_provider/src/providers/heroesinfo.dart';
import 'package:flutter_provider/src/screens/home_screen.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => HeroesInfo(),
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Material App',
        initialRoute: 'home',
        routes: {
          'home': (context) => HomeScreen(),
        },
      ),
    );
  }
}
import 'package:flutter/material.dart';

class HeroesInfo with ChangeNotifier {
  String _heroe = "Capitan Marvel";
  Color color = Colors.blue;

  get heroe{
    return _heroe;
  }

  set heroe(String nombre){
    _heroe = nombre;
    this.color = (nombre == "Capitan Marvel") ? Colors.blue : Colors.red;
    notifyListeners();
  } 
  
}
import 'package:flutter/material.dart';
import 'package:flutter_provider/src/providers/heroesinfo.dart';
import 'package:provider/provider.dart';

class SuperFAB extends StatelessWidget {
  const SuperFAB({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    final heroesInfo = Provider.of<HeroesInfo>(context);

    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.red,
          onPressed: () {
            heroesInfo.heroe = "Ironman";
          },
        ),
        SizedBox(height: 10.0),
        FloatingActionButton(
          child: Icon(Icons.remove),
          backgroundColor: Colors.blue,
          onPressed: () {
            heroesInfo.heroe = "Capitan Marvel";
          },
        ),
      ],
    );
  }
}
import 'package:flutter/material.dart';
import 'package:flutter_provider/src/providers/heroesinfo.dart';
import 'package:provider/provider.dart';

class SuperText extends StatelessWidget {
  const SuperText({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
   // final heroesInfo = Provider.of<HeroesInfo>(context);
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Consumer<HeroesInfo>(builder: (context, heroesInfo, _ ) => Text(
            heroesInfo.heroe,
            style: TextStyle(
              fontSize: 30.0,
              color: heroesInfo.color,
            ),
          ),
          ), 
        ],
      ),
    );
  }
}
import 'package:flutter/material.dart';
import 'package:flutter_provider/src/providers/heroesinfo.dart';
import 'package:flutter_provider/src/widgets/super_fab.dart';
import 'package:flutter_provider/src/widgets/super_text.dart';
import 'package:provider/provider.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /**
     * necesario colocarle el tipo entre <>
     * aqui escucha el provider y actualiza la informacion
     */
    final heroesInfo = Provider.of<HeroesInfo>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(heroesInfo.heroe),
      ),
      body: Center(
        child: SuperText(),
      ),
      floatingActionButton: SuperFAB(),
    );
  }
}