Last active
November 25, 2020 16:51
-
-
Save Andrious/b421d939ff3ee71b81a4e81e29bed5c2 to your computer and use it in GitHub Desktop.
The Counter app with const constructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
theme: ThemeData( | |
primarySwatch: Colors.green, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
State createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
_MyHomePageState() : bloc = Bloc(); | |
final Bloc bloc; | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have clicked the button this many times:', | |
), | |
Text( | |
bloc.data, | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(bloc.onPressed); | |
}, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
/// A Business Logic Component | |
/// This BLoC relays functionality between your app's data source | |
/// and its interface. It's your app's event handler (i.e. your app's API). | |
/// | |
/// It contains all the 'mutable' elements for the counter app. | |
/// | |
class Bloc { | |
Bloc() { | |
_counter = 0; | |
} | |
int _counter; | |
String get data => _counter.toString(); | |
void onPressed() => _counter++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment