Last active
May 27, 2021 04:23
-
-
Save mcany/2dffd93e20d860d62f83dcd363cc7a31 to your computer and use it in GitHub Desktop.
New Flutter Project with Bloc
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
export 'app_bloc.dart'; | |
export 'app_event.dart'; | |
export 'app_state.dart'; | |
export 'app_route.dart'; |
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
import 'package:bloc/bloc.dart'; | |
import 'app.dart'; | |
class AppBloc extends Bloc<AppEvent, AppState> { | |
@override | |
AppState get initialState => AppUninitialized(); | |
@override | |
Stream<AppState> mapEventToState(AppEvent event) async* { | |
if (event is AppStarted) { | |
yield AppUnauthenticated(); | |
} | |
if (event is LoggedIn) { | |
yield AppAuthenticated(); | |
} | |
if (event is LoggedOut) { | |
yield AppUnauthenticated(); | |
} | |
} | |
} |
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
import 'package:meta/meta.dart'; | |
import 'package:equatable/equatable.dart'; | |
@immutable | |
abstract class AppEvent extends Equatable { | |
AppEvent([List props = const []]) : super(props); | |
} | |
class AppStarted extends AppEvent { | |
@override | |
String toString() => 'AppStarted'; | |
} | |
class LoggedIn extends AppEvent { | |
@override | |
String toString() => 'LoggedIn'; | |
} | |
class LoggedOut extends AppEvent { | |
@override | |
String toString() => 'LoggedOut'; | |
} |
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
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'app.dart'; | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: BlocBuilder<AppBloc, AppState>( | |
bloc: BlocProvider.of<AppBloc>(context), | |
builder: (BuildContext context, AppState state) { | |
if (state is AppUninitialized) { | |
// return splash page | |
} | |
if (state is AppAuthenticated) { | |
// return home page | |
} | |
if (state is AppUnauthenticated) { | |
// return login page | |
} | |
if (state is AppLoading) { | |
// return loading page | |
} | |
}, | |
), | |
); | |
} | |
} |
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
import 'package:equatable/equatable.dart'; | |
import 'package:meta/meta.dart'; | |
@immutable | |
abstract class AppState extends Equatable {} | |
class AppUninitialized extends AppState { | |
@override | |
String toString() => 'AppUninitialized'; | |
} | |
class AppAuthenticated extends AppState { | |
@override | |
String toString() => 'AppAuthenticated'; | |
} | |
class AppUnauthenticated extends AppState { | |
@override | |
String toString() => 'AppUnauthenticated'; | |
} | |
class AppLoading extends AppState { | |
@override | |
String toString() => 'AppLoading'; | |
} |
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
import 'package:bloc/bloc.dart'; | |
class AppBlocDelegate extends BlocDelegate { | |
@override | |
void onEvent(Bloc bloc, Object event) { | |
super.onEvent(bloc, event); | |
print(event); | |
} | |
@override | |
void onTransition(Bloc bloc, Transition transition) { | |
super.onTransition(bloc, transition); | |
print(transition); | |
} | |
@override | |
void onError(Bloc bloc, Object error, StackTrace stacktrace) { | |
super.onError(bloc, error, stacktrace); | |
print(error); | |
} | |
} |
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
import 'package:bloc/bloc.dart'; | |
import 'package:flutter/widgets.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'app/app.dart'; | |
import 'util/bloc_delegate.dart'; | |
void main() { | |
BlocSupervisor.delegate = AppBlocDelegate(); | |
runApp( | |
BlocProvider<AppBloc>( | |
builder: (context) { | |
return AppBloc()..dispatch(AppStarted()); | |
}, | |
child: App(), | |
), | |
); | |
} | |
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
// Flutter | |
import 'dart:io'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
class _NetworkConstant { | |
static const String baseURL = "https://baseURL"; | |
static const String contentTypeHeader = "application/json"; | |
static const String setCookie = "set-cookie"; | |
static const String cookie = "cookie"; | |
static const List<String> ignoredCookieKeys = ["path", "expires"]; | |
} | |
class OBNetworkManager { | |
static final OBNetworkManager _instance = OBNetworkManager._internal(); | |
factory OBNetworkManager() => _instance; | |
OBNetworkManager._internal(); | |
Map<String, String> _headers = { | |
HttpHeaders.contentTypeHeader: _NetworkConstant.contentTypeHeader | |
}; | |
Map<String, String> _cookies = {}; | |
Future<Map> get(String url) async { | |
final finalURL = _finalURL(url); | |
http.Response response = await http.get(finalURL, headers: _headers); | |
_updateCookie(response); | |
return json.decode(response.body); | |
} | |
Future<Map> post(String url, dynamic data) async { | |
final finalURL = _finalURL(url); | |
final dataJSON = json.encode(data); | |
http.Response response = | |
await http.post(finalURL, body: dataJSON, headers: _headers); | |
_updateCookie(response); | |
final bodyJSON = json.decode(response.body); | |
return bodyJSON; | |
} | |
String _finalURL(String url) { | |
final finalURL = "${_NetworkConstant.baseURL}$url"; | |
return finalURL; | |
} | |
void _updateCookie(http.Response response) { | |
String allSetCookie = response.headers[_NetworkConstant.setCookie]; | |
if (allSetCookie != null) { | |
var setCookies = allSetCookie.split(','); | |
for (var setCookie in setCookies) { | |
var cookies = setCookie.split(';'); | |
for (var cookie in cookies) { | |
_setCookie(cookie); | |
} | |
} | |
_headers[_NetworkConstant.cookie] = _generateCookieHeader(); | |
} | |
} | |
void _setCookie(String rawCookie) { | |
if (rawCookie.length > 0) { | |
var keyValue = rawCookie.split('='); | |
if (keyValue.length == 2) { | |
var key = keyValue[0].trim(); | |
var value = keyValue[1]; | |
// ignore keys that aren't cookies | |
if (_NetworkConstant.ignoredCookieKeys.contains(key)) { | |
return; | |
} | |
this._cookies[key] = value; | |
} | |
} | |
} | |
String _generateCookieHeader() { | |
String cookie = ""; | |
for (var key in _cookies.keys) { | |
if (cookie.length > 0) cookie += ";"; | |
cookie += key + "=" + _cookies[key]; | |
} | |
return cookie; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment