Skip to content

Instantly share code, notes, and snippets.

@Andrious
Last active November 25, 2020 16:51
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 Andrious/b421d939ff3ee71b81a4e81e29bed5c2 to your computer and use it in GitHub Desktop.
Save Andrious/b421d939ff3ee71b81a4e81e29bed5c2 to your computer and use it in GitHub Desktop.
The Counter app with const constructors
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