Skip to content

Instantly share code, notes, and snippets.

@burhanrashid52
Created November 19, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burhanrashid52/5f7c03f9b7668dc7ad3f8d90f88d5575 to your computer and use it in GitHub Desktop.
Save burhanrashid52/5f7c03f9b7668dc7ad3f8d90f88d5575 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModel<CounterModel>(
model: CounterModel(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
),
);
}
}
class CounterModel extends Model {
int _counter = 0;
int get counter => _counter;
void increment() {
// First, increment the counter
_counter++;
// Then notify all the listeners.
notifyListeners();
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Scope Model"),
actions: <Widget>[CartCount()],
),
body: Center(
child: ScopedModelDescendant<CounterModel>(
builder: (context, child, model) {
return Text(model.counter.toString(),
style: Theme.of(context).textTheme.display1);
},
),
),
floatingActionButton: ScopedModelDescendant<CounterModel>(
builder: (context, child, model) {
return FloatingActionButton(
onPressed: () {
model.increment();
},
child: Icon(Icons.add),
);
},
),
);
}
}
class CartCount extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Stack(
fit: StackFit.loose,
children: <Widget>[
Align(
alignment: AlignmentDirectional.center,
child: Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Icon(Icons.shopping_cart),
)),
Align(
alignment: AlignmentDirectional.topEnd,
child: Padding(
padding: const EdgeInsets.only(left: 16.0),
child: CircleAvatar(
backgroundColor: Colors.red,
radius: 10.0,
child: ScopedModelDescendant<CounterModel>(
builder: (context, child, model) =>
Text(model.counter.toString())),
),
),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment