-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:mynotes/helpers/loading/loading_screen_controller.dart'; | ||
|
||
class LoadingScreen { | ||
factory LoadingScreen() => _shared; | ||
static final LoadingScreen _shared = LoadingScreen._sharedInstance(); | ||
LoadingScreen._sharedInstance(); | ||
|
||
LoadingScreenController? controller; | ||
|
||
void show({ | ||
required BuildContext context, | ||
required String text, | ||
}) { | ||
if (controller?.update(text) ?? false) { | ||
return; | ||
} else { | ||
controller = showOverlay( | ||
context: context, | ||
text: text, | ||
); | ||
} | ||
} | ||
|
||
void hide() { | ||
controller?.close(); | ||
controller = null; | ||
} | ||
|
||
LoadingScreenController showOverlay({ | ||
required BuildContext context, | ||
required String text, | ||
}) { | ||
final _text = StreamController<String>(); | ||
_text.add(text); | ||
|
||
final state = Overlay.of(context); | ||
final renderBox = context.findRenderObject() as RenderBox; | ||
final size = renderBox.size; | ||
|
||
final overlay = OverlayEntry( | ||
builder: (context) { | ||
return Material( | ||
color: Colors.black.withAlpha(150), | ||
child: Center( | ||
child: Container( | ||
constraints: BoxConstraints( | ||
maxWidth: size.width * 0.8, | ||
maxHeight: size.height * 0.8, | ||
minWidth: size.width * 0.5, | ||
), | ||
decoration: BoxDecoration( | ||
color: Colors.white, | ||
borderRadius: BorderRadius.circular(10.0), | ||
), | ||
child: Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: SingleChildScrollView( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const SizedBox(height: 10), | ||
const CircularProgressIndicator(), | ||
const SizedBox(height: 20), | ||
StreamBuilder( | ||
stream: _text.stream, | ||
builder: (context, snapshot) { | ||
if (snapshot.hasData) { | ||
return Text( | ||
snapshot.data as String, | ||
textAlign: TextAlign.center, | ||
); | ||
} else { | ||
return Container(); | ||
} | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
)), | ||
), | ||
); | ||
}, | ||
); | ||
|
||
state?.insert(overlay); | ||
|
||
return LoadingScreenController( | ||
close: () { | ||
_text.close(); | ||
overlay.remove(); | ||
return true; | ||
}, | ||
update: (text) { | ||
_text.add(text); | ||
return true; | ||
}, | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:flutter/foundation.dart' show immutable; | ||
|
||
typedef CloseLoadingScreen = bool Function(); | ||
typedef UpdateLoadingScreen = bool Function(String text); | ||
|
||
@immutable | ||
class LoadingScreenController { | ||
final CloseLoadingScreen close; | ||
final UpdateLoadingScreen update; | ||
|
||
const LoadingScreenController({ | ||
required this.close, | ||
required this.update, | ||
}); | ||
} |
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
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
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
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