add project
This commit is contained in:
18
lib/config/DiConfig.dart
Normal file
18
lib/config/DiConfig.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
|
||||
class _LogDioErrorInterceptor extends Interceptor {
|
||||
@override
|
||||
Future onError(DioError err) {
|
||||
// TODO: implement onError
|
||||
return super.onError(err);
|
||||
}
|
||||
}
|
||||
|
||||
Dio getDioInstance(){
|
||||
Dio dio = Dio();
|
||||
dio.options.headers['content-type'] = "application/json";
|
||||
dio.interceptors.add(_LogDioErrorInterceptor());
|
||||
return dio;
|
||||
}
|
||||
113
lib/main.dart
Normal file
113
lib/main.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_service/model/CategoryResponse.dart';
|
||||
import 'package:flutter_service/provider/CategoryProvider.dart';
|
||||
import 'package:flutter_service/sdk/ClientSDK.dart';
|
||||
import 'package:flutter_service/wrapper/ProviderWrapper.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'utils/console.dart';
|
||||
|
||||
void main() async {
|
||||
runApp(MultiProvider(
|
||||
providers: ProviderWrapper.providersLists,
|
||||
child: MyApp()
|
||||
));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
// This widget is the root of your application.
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
home: MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
MyHomePage({Key key, this.title}) : super(key: key);
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
_MyHomePageState createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
ClientSDK client = ClientSDK();
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
|
||||
int _counter = 0;
|
||||
List<CategoryResponse> categories = [];
|
||||
|
||||
void _incrementCounter() {
|
||||
setState(() {
|
||||
_counter++;
|
||||
});
|
||||
}
|
||||
|
||||
void getCategory() async {
|
||||
categories.map((e) => Console.log("text", e.name)).toList();
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
|
||||
|
||||
super.initState();
|
||||
init(context);
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
// TODO: implement didChangeDependencies
|
||||
super.didChangeDependencies();
|
||||
categories = Provider.of<CategoryProvider>(context, listen: true)
|
||||
.getCategories();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void init(BuildContext context) async {
|
||||
List<CategoryResponse> result = await client.clientGetAllCategoryService();
|
||||
Provider.of<CategoryProvider>(context, listen: false).setCategories(result);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
'You have pushed the button this many times:',
|
||||
),
|
||||
Text(
|
||||
'$_counter',
|
||||
style: Theme.of(context).textTheme.headline4,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: getCategory,
|
||||
tooltip: 'Increment',
|
||||
child: Icon(Icons.add),
|
||||
), // This trailing comma makes auto-formatting nicer for build methods.
|
||||
);
|
||||
}
|
||||
}
|
||||
25
lib/model/CategoryResponse.dart
Normal file
25
lib/model/CategoryResponse.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
class CategoryResponse {
|
||||
|
||||
CategoryResponse({
|
||||
this.id,
|
||||
this.code,
|
||||
this.name
|
||||
});
|
||||
|
||||
|
||||
|
||||
int id;
|
||||
String code;
|
||||
String name;
|
||||
Map<String,dynamic> config;
|
||||
|
||||
static CategoryResponse fromResponse(dynamic result){
|
||||
return CategoryResponse(
|
||||
id: int.parse(result["id"]),
|
||||
code: result["code"],
|
||||
name: result["name"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
25
lib/provider/CategoryProvider.dart
Normal file
25
lib/provider/CategoryProvider.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_service/model/CategoryResponse.dart';
|
||||
|
||||
class CategoryProvider with ChangeNotifier {
|
||||
List<CategoryResponse> _categories = [];
|
||||
|
||||
void setCategories(List<CategoryResponse> value){
|
||||
_categories = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<CategoryResponse> getCategories(){
|
||||
return _categories;
|
||||
}
|
||||
|
||||
void removeCategories(){
|
||||
|
||||
}
|
||||
|
||||
void appendCategories(){
|
||||
|
||||
}
|
||||
}
|
||||
14
lib/sdk/ClientSDK.dart
Normal file
14
lib/sdk/ClientSDK.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
import 'package:flutter_service/model/CategoryResponse.dart';
|
||||
import 'package:flutter_service/service/CategoryService.dart';
|
||||
|
||||
class ClientSDK {
|
||||
|
||||
Future<List<CategoryResponse>> clientGetAllCategoryService() async {
|
||||
return await getAllCategoryService();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
19
lib/service/CategoryService.dart
Normal file
19
lib/service/CategoryService.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter_service/config/DiConfig.dart';
|
||||
import 'package:flutter_service/model/CategoryResponse.dart';
|
||||
|
||||
Future<List<CategoryResponse>> getAllCategoryService() async {
|
||||
String url = "http://computer-api.osa.cubetiqs.com/api/frontend/categories";
|
||||
List<CategoryResponse> r = await getDioInstance().get(url).then((value) {
|
||||
|
||||
List<dynamic> result = value.data['data'] as List<dynamic>;
|
||||
|
||||
return result.map((e) => CategoryResponse.fromResponse(e)).toList();
|
||||
|
||||
}).catchError((onError) {
|
||||
print(onError);
|
||||
return null;
|
||||
});
|
||||
|
||||
return r;
|
||||
|
||||
}
|
||||
37
lib/utils/console.dart
Normal file
37
lib/utils/console.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
class Console {
|
||||
static final Console _singleton = Console._internal();
|
||||
factory Console() {
|
||||
return _singleton;
|
||||
}
|
||||
static void log(text, anyObj) {
|
||||
final textLength = text.toString().length + 4;
|
||||
print(" " + '-' * textLength);
|
||||
print("| " + text + " |");
|
||||
print(" " + '-' * textLength);
|
||||
print(anyObj);
|
||||
print("==============================");
|
||||
print("==============================");
|
||||
}
|
||||
|
||||
static void json(data) {}
|
||||
|
||||
static void jsonAll(data) {
|
||||
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
|
||||
String prettylint = encoder.convert(data);
|
||||
|
||||
prettylint
|
||||
.split('\n')
|
||||
.forEach((element) => Console.log("Render JSON", prettylint));
|
||||
}
|
||||
|
||||
static void better(message) {
|
||||
var logger = Logger();
|
||||
logger.e(message);
|
||||
}
|
||||
|
||||
Console._internal();
|
||||
}
|
||||
9
lib/wrapper/ProviderWrapper.dart
Normal file
9
lib/wrapper/ProviderWrapper.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter_service/provider/CategoryProvider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider/single_child_widget.dart';
|
||||
|
||||
class ProviderWrapper {
|
||||
static List<SingleChildWidget> providersLists = [
|
||||
ChangeNotifierProvider(create: (context)=>CategoryProvider()),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user