Skip to content

Instantly share code, notes, and snippets.

@AlexKenbo
Created January 31, 2020 16:15
Show Gist options
  • Save AlexKenbo/06d8cb734378e8b1fe97f756a8c5c51c to your computer and use it in GitHub Desktop.
Save AlexKenbo/06d8cb734378e8b1fe97f756a8c5c51c to your computer and use it in GitHub Desktop.
Flutter design patterns - singleton
import 'package:flutter/material.dart';
abstract class ExampleStateBase {
@protected
String initialText;
@protected
String stateText;
String get currentText => stateText;
void setStateText(String text) {
stateText = text;
}
void reset() {
stateText = initialText;
}
}
//Singleton’s implementation by definition
class ExampleStateByDefinition extends ExampleStateBase {
static ExampleStateByDefinition _instance;
ExampleStateByDefinition._internal() {
initialText = "A new 'ExampleStateByDefinition' instance has been created.";
stateText = initialText;
print(stateText);
}
static ExampleStateByDefinition getState() {
if (_instance == null) {
_instance = ExampleStateByDefinition._internal();
}
return _instance;
}
}
//Class ExampleState implements a Singleton design pattern “the Dart way”
class ExampleState extends ExampleStateBase {
static final ExampleState _instance = ExampleState._internal();
factory ExampleState() {
return _instance;
}
ExampleState._internal() {
initialText = "A new 'ExampleState' instance has been created.";
stateText = initialText;
print(stateText);
}
}
//Just a simple implementation of the state class without bothering it with Singleton or any other ”fancy-schmancy” design patterns.
class ExampleStateWithoutSingleton extends ExampleStateBase {
ExampleStateWithoutSingleton() {
initialText =
"A new 'ExampleStateWithoutSingleton' instance has been created.";
stateText = initialText;
print(stateText);
}
}
void main() {
runApp(SingletonExample());
}
class SingletonExample extends StatefulWidget {
@override
_SingletonExampleState createState() => _SingletonExampleState();
}
class _SingletonExampleState extends State<SingletonExample> {
final List<ExampleStateBase> stateList = [
ExampleState(),
ExampleStateByDefinition.getState(),
ExampleStateWithoutSingleton()
];
void _setTextValues([String text = 'Singleton']) {
for (var state in stateList) {
state.setStateText(text);
}
setState(() {});
}
void _reset() {
for (var state in stateList) {
state.reset();
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return ScrollConfiguration(
behavior: ScrollBehavior(),
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: paddingL),
child: Column(
children: <Widget>[
for (var state in stateList)
Padding(
padding: const EdgeInsets.only(bottom: paddingL),
child: SingletonExampleCard(
text: state.currentText,
),
),
const SizedBox(height: spaceL),
PlatformButton(
child: Text("Change states\' text to 'Singleton'"),
materialColor: Colors.black,
materialTextColor: Colors.white,
onPressed: _setTextValues,
),
PlatformButton(
child: Text("Reset"),
materialColor: Colors.black,
materialTextColor: Colors.white,
onPressed: _reset,
),
const SizedBox(height: spaceXL),
Text(
'Note: change states\' text and navigate the application (e.g. go to the tab "description" or main menu, then go back to this example) to see how the Singleton state behaves!',
textAlign: TextAlign.justify,
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment