Skip to content

Instantly share code, notes, and snippets.

@liemvo
liemvo / ActivityProgresss.kt
Created September 9, 2020 20:26
Example run with different thread to show and hide a loading
private fun launchActivity() {
pbLaunching.show()
lifecycleScope.launch(Dispatchers.IO) {
delay(2000)
Log.d("###", "Launching")
launch(Dispatchers.Main) {
pbLaunching.hid()
}
}
}
class ExcerciseViewModel: BaseViewModel() {
val listExercise: LiveData<List<Excercise>?> = Transformations.map(repository.allExcercise) {
it
}
private fun handleGetData() {
if (!hasDataLocal) {
getDataFromFireBase()
}
}
@liemvo
liemvo / hidekeyboard.kt
Created August 27, 2020 02:13
HideKeyboard
fun hideKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemSSservice(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager
val view = activity.currentFocus ?: View(activity)
inputMethodManager?.hideSoftInputFromWindow(view.windowToken, 0)
}
_showAddNoteDialog(BuildContext context) => showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Add your note"),
content: SingleChildScrollView(
child: Container(
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
_showMultiChoiceDialog(BuildContext context) => showDialog(
context: context,
builder: (context) {
final _multipleNotifier = Provider.of<MultipleNotifier>(context);
return AlertDialog(
title: Text("Select one country or many countries"),
content: SingleChildScrollView(
child: Container(
width: double.infinity,
child: Column(
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider<SingleNotifier>(
create: (_) => SingleNotifier(),
),
ChangeNotifierProvider<MultipleNotifier>(
create: (_) => MultipleNotifier([]),
)
],
class MultipleNotifier extends ChangeNotifier {
List<String> _selectedItems;
MultipleNotifier(this._selectedItems);
List<String> get selectedItems => _selectedItems;
bool isHaveItem(String value) => _selectedItems.contains(value);
addItem(String value) {
if (!isHaveItem(value)) {
_selectedItems.add(value);
notifyListeners();
}
_showSingleChoiceDialog(BuildContext context) => showDialog(
context: context,
builder: (context) {
var _singleNotifier = Provider.of<SingleNotifier>(context);
return AlertDialog(
title: Text("Select one country"),
content: SingleChildScrollView(
child: Container(
width: double.infinity,
child: Column(
import 'package:examples_dialogs/data.dart';
import 'package:flutter/material.dart';
class SingleNotifier extends ChangeNotifier {
String _currentCountry = countries[0];
SingleNotifier();
String get currentCountry => _currentCountry;
updateCountry(String value) {
if (value != _currentCountry) {
_currentCountry = value;
notifyListeners();
_showMessageDialog(BuildContext context) => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Are you sure"),
content: Text("Do you want to delete these items?"),
actions: <Widget>[
FlatButton(
child: Text("Yes"),
onPressed: () {
Navigator.of(context).pop();