Skip to content

Instantly share code, notes, and snippets.

@bubnenkoff
Created August 8, 2019 13:23
Show Gist options
  • Save bubnenkoff/0aaf86f31361fdb20f2d57db86225e92 to your computer and use it in GitHub Desktop.
Save bubnenkoff/0aaf86f31361fdb20f2d57db86225e92 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'select_regions.dart';
import 'select_industry.dart';
import 'select_customer.dart'; // заказчик
import 'select_organizer.dart'; // Организатор
import 'select_datetime.dart'; // Организатор
import 'dart:convert';
import 'package:provider/provider.dart';
import 'package:flutter/services.dart';
void main() async {
final TenderApiData _tenderApiData = TenderApiData();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp(_tenderApiData));
});
}
class MyApp extends StatelessWidget {
TenderApiData _tenderApiData;
MyApp(this._tenderApiData) {
_tenderApiData.getApiKey().then((_) => _tenderApiData.getRegionsList());
_tenderApiData.getIndustryList();
}
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// home: ChangeNotifierProvider<TenderApiData>(
// builder: (_) => _tenderApiData, child: HomePage()),
// );
// }
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(builder: (_) => _tenderApiData),
ChangeNotifierProvider(builder: (_) => AppState()),
// ChangeNotifierProvider(builder: (_) => ResultProcessing()),
],
child: MaterialApp(
title: "My App",
home: HomePage()
),
);
}
}
class AppState extends ChangeNotifier
{
String _customer;
List<String> _regions;
List<String> _industry;
List _selected_period; // зададим два значения, чтобы потом их выставлять по индексам
String get customer => _customer;
set customer(String customer)
{
_customer = customer;
notifyListeners();
}
List<String> get regions => _regions;
set regions(List<String> regions)
{
_regions = regions;
notifyListeners();
}
List<String> get industry => _industry;
set industry(List<String> industry)
{
_industry = industry;
notifyListeners();
}
List get selected_period => _selected_period;
set selected_period(List selected_period)
{
_selected_period = selected_period;
notifyListeners();
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(), body: MainContainer());
}
}
class MyContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
// crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[MyTestWidget(), SelectRegions()],
// children: <Widget>[MyTestWidget()],
);
}
}
class TenderApiData with ChangeNotifier {
String access_token;
List<Map<String, String>> id_names;
List<Map<String, String>> id_industry; // Отрасль ниспадающий список
List<Map<String, String>> id_customer; // Заказчик
List<Map<String, String>> id_organizer; // Организатор
String access_token_url =
"https://";
String regions_list_url = "https://";
String industry_list_url = "http://10.0.2.2:8000/api/industry"; // Отрасль
getApiKey() async {
var response = await http
.post(access_token_url, headers: {"Accept": "application/json"});
if (response.statusCode == 200) {
access_token = await json.decode(response.body)['access_token'];
print("access_token: " + access_token);
notifyListeners();
}
print("getApiKey");
}
getRegionsList() async {
var response = await http.get(regions_list_url, headers: {
"Accept": "application/json",
"Authorization": "bearer $access_token"
});
if (response.statusCode == 200) {
List answers = json.decode(response.body)['regions'];
// List answers = [{"id":"1","name":"Республика Адыгея"},{"id":"2","name":"Республика Башкортостан"},{"id":"3","name":"Республика Бурятия"}];
id_names = answers
.map((_) => {"id": _["id"].toString(), "name": _["name"].toString()})
.take(10)
.toList();
// id_names = x.map.cast<String, String>();
print(json.encode(id_names));
print(id_names.runtimeType);
notifyListeners();
} else {
print("response code: ${response.statusCode}");
}
}
getIndustryList() async {
var response = await http.get(industry_list_url);
if (response.statusCode == 200) {
List answers = json.decode(response.body);
print("!!!answers: $answers");
// List answers = [{"id":"1","name":"Республика Адыгея"},{"id":"2","name":"Республика Башкортостан"},{"id":"3","name":"Республика Бурятия"}];
id_industry = answers
.map((_) => {
"code": _["code"].toString(),
"short_name": _["short_name"].toString()
})
.take(10)
.toList();
print(json.encode(id_names));
print(id_names.runtimeType);
notifyListeners();
} else {
print("response code: ${response.statusCode}");
}
}
}
class ResultProcessing
{
}
class MyTestWidget extends StatefulWidget {
MyTestWidgetState createState() => MyTestWidgetState();
}
class MyTestWidgetState extends State<MyTestWidget> {
@override
Widget build(BuildContext context) {
var result = Provider.of<TenderApiData>(context).access_token;
var test = Provider.of<TenderApiData>(context).id_names;
return Column(
children: <Widget>[
// Text("Data: $result"),
Text("test test test"),
RaisedButton(
onPressed: Provider.of<TenderApiData>(context).getRegionsList,
child: Text("get regions"),
),
// Text("some text $test")
],
);
}
}
class MainContainer extends StatelessWidget {
static DateTime _fromDate = DateTime.now().subtract(Duration(days: 350));
static DateTime _toDate = DateTime.now().subtract(Duration(days: 340));
List<String> result_date = [_fromDate.toString().split(' ')[0], _toDate.toString().split(' ')[0]];
@override
Widget build(BuildContext context) {
return Table(
children: [
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 80,
child: Text("Регион"),
),
Expanded(
child: SelectRegions(),
)
],
),
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 85,
child: Text("Отрасль"),
),
Expanded(
child: SelectIndustry(),
)
],
)
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 85,
child: Text("Заказчик"),
),
Expanded(
child: CustomerInput(),
)
],
)
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 85,
child: Text("Организатор"),
),
Expanded(
child: OrganizerInput(),
)
],
)
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 85,
child: Text("НМЦК"),
),
Container(
child: Row(
children: <Widget>[
Container(
width: 70,
child: TextField(),
),
Container(
margin: const EdgeInsets.only(left: 20.0),
width: 70,
child: TextField(),
),
],
))
],
)
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Container(
width: 85,
child: Text("Период"),
),
Expanded(
child: DateAndTimePickerDemo(),
)
],
)
]),
TableRow(children: [
Row(
children: <Widget>[
// Container(child: MyTestWidget()),
Flexible(
flex: 6,
child: Container(),
),
Flexible(
child: Container(
child: RaisedButton(
child: Text("Save"),
onPressed: ()
{
// result_date = Provider.of<AppState>(context).selected_period;
// print("result_date: $result_date");
Map map =
{
"selected_region": Provider.of<AppState>(context).regions,
"selected_industry": Provider.of<AppState>(context).industry,
"selected_period": Provider.of<AppState>(context).selected_period,
};
// print(map);
// теперь мне нужно отправить запрос на сервер
},
)),
),
],
)
]),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment