Skip to content

Instantly share code, notes, and snippets.

View berkanaslan's full-sized avatar
:shipit:
Focusing

Berkan Aslan berkanaslan

:shipit:
Focusing
View GitHub Profile
@berkanaslan
berkanaslan / shared_preferences.dart
Created May 25, 2021 08:51
Flutter Shared Preferences Helper
import 'package:online_tpi_qms_mobile/model/user/user.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefs {
static SharedPreferences _sharedPrefs;
static Map<String, dynamic> _memoryPrefs = Map<String, dynamic>();
factory SharedPrefs() => SharedPrefs._internal();
import 'package:flutter/material.dart';
/// [Screen] Ekran oluşturrurken gereken tüm bilgileri tutacak
class Screen {
 /// BottomNavigationBar'da gösterilecek olan başlık
 final String title;
/// BottomNavigationBar'da gösterilecek olan ikon
 final IconData icon;
/// Ekranda akacak olan WidgetTree tutucusu
const FIRST_SCREEN = 0;
const SECOND_SCREEN = 1;
const THIRD_SCREEN = 2;
const FOURTH_SCREEN = 3;
class FirstScreen extends StatelessWidget {
static const route = '/first';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('First Screen')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
class NavigationProvider extends ChangeNotifier {
/// [NavigationProvider] edinmek için shortcode
static NavigationProvider of(BuildContext context) =>
Provider.of<NavigationProvider>(context, listen: false);
// Açılış sayfası
int _currentScreenIndex = FIRST_SCREEN;
int get currentTabIndex => _currentScreenIndex;
Route<dynamic> onGenerateRoute(RouteSettings settings) {
void setTab(int tab) {
if (tab == currentTabIndex) {
// TODO: Ekranın en üstüne dönme işlemleri: _scrollToTopOfPage();
} else {
_currentScreenIndex = tab;
notifyListeners();
}
}
//...
class Root extends StatelessWidget {
static const route = '/';
@override
Widget build(BuildContext context) {
return Consumer<NavigationProvider>(
builder: (context, provider, child) {
// Tanımladığımız screen'lardan BottomNavigationBar oluşturma
final bottomNavigationBarItems = provider.screens
.map(
Future<bool> onWillPop(BuildContext context) async {
final currentNavigatorState = currentScreen.navigatorState.currentState;
if (currentNavigatorState.canPop()) {
currentNavigatorState.pop();
return false;
} else {
if (currentTabIndex != FIRST_SCREEN) {
setTab(FIRST_SCREEN);
notifyListeners();
....
Route<dynamic> onGenerateRoute(RouteSettings settings) {
print('Oluşturulan rota: ${settings.name}');
switch (settings.name) {
case PushedScreen.route:
return MaterialPageRoute(builder: (_) => PushedScreen());
case SimpleScreen.route:
return MaterialPageRoute(builder: (_) => SimpleScreen());
default:
return MaterialPageRoute(builder: (_) => Root());
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => NavigationProvider()),
],
child: Builder(