Skip to content

Instantly share code, notes, and snippets.

@siteslave
Created September 27, 2022 02:36
Show Gist options
  • Save siteslave/82a8a095208112612ac03076a7e498a5 to your computer and use it in GitHub Desktop.
Save siteslave/82a8a095208112612ac03076a7e498a5 to your computer and use it in GitHub Desktop.

ElevatedButton

ElevatedButton(
  style: ElevatedButton.styleFrom(
      padding: EdgeInsets.all(20),
      shape:
          RoundedRectangleBorder(
              borderRadius:
                  BorderRadius
                      .circular(
                          30))),
  child: Text('Login'),
  onPressed: () {},
)

DatePicker

  DateTime? pickedDate =
  await showDatePicker(
      context: context,
      locale:
          Locale('th', 'TH'),
      initialDate: DateTime
          .now(), //get today's date
      firstDate: DateTime(
          2000), //DateTime.now() - not to allow to choose before today.
      lastDate:
          DateTime(2101));

Localization

pubspec.yaml

  flutter_localizations:
    sdk: flutter
localizationsDelegates: [
  GlobalMaterialLocalizations.delegate,
  GlobalWidgetsLocalizations.delegate,
  DefaultCupertinoLocalizations.delegate,
  GlobalCupertinoLocalizations.delegate,
],
locale: Locale('en', 'US'),
supportedLocales: [
  const Locale('en', 'US'), // English
  const Locale('th', 'TH'), // Thai
],
theme: ThemeData(
    primarySwatch: Colors.indigo,
    fontFamily: "NotoSansThai",
    textTheme: TextTheme(
      bodyText1: TextStyle(fontSize: 16.0),
      bodyText2: TextStyle(fontSize: 16.0),
      button: TextStyle(fontSize: 16),
    )),

Routing

initialRoute: '/',
routes: {
  '/': (context) => const HomePage(),
  AuthenCodePage.routeName: (context) => const AuthenCodePage(),
}

Get params

import 'package:flutter/material.dart';

class AuthenCodePage extends StatefulWidget {
  static const routeName = '/authen';
  const AuthenCodePage({Key? key}) : super(key: key);

  @override
  State<AuthenCodePage> createState() => _AuthenCodePageState();
}

class _AuthenCodePageState extends State<AuthenCodePage> {
  @override
  Widget build(BuildContext context) {
    final args = ModalRoute.of(context)!.settings.arguments as ScreenArguments;

    return Scaffold(
      appBar: AppBar(
        title: Text('Authen'),
      ),
      body: Center(
        child: Text(args.message),
      ),
    );
  }
}

class ScreenArguments {
  final String title;
  final String message;

  ScreenArguments(this.title, this.message);
}

@siteslave
Copy link
Author

import 'package:demo_app/authen_code.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _isObscure = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Container(
            margin: EdgeInsets.only(top: 40),
            child: Image.asset(
              "assets/logo/moph-logo.png",
              width: 160,
              height: 160,
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 20, horizontal: 40),
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  SizedBox(height: 20.0),
                  DefaultTabController(
                      length: 2, // length of tabs
                      initialIndex: 0,
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            Container(
                              child: TabBar(
                                labelColor: Colors.indigo,
                                labelStyle:
                                    TextStyle(fontWeight: FontWeight.bold),
                                unselectedLabelColor: Colors.grey,
                                unselectedLabelStyle:
                                    TextStyle(fontWeight: FontWeight.normal),
                                indicatorWeight: 4.0,
                                tabs: [
                                  Tab(text: 'Login'),
                                  Tab(text: 'Register'),
                                ],
                              ),
                            ),
                            Container(
                                height: 400, //height of TabBarView
                                child: TabBarView(children: <Widget>[
                                  Container(
                                    child: Column(
                                      children: [
                                        TextFormField(
                                          decoration: InputDecoration(
                                            suffixIcon: Icon(Icons.email),
                                            filled: true,
                                            label: Text("Username"),
                                            hintText: 'Enter username',
                                            // enabledBorder: OutlineInputBorder(
                                            //     borderSide: BorderSide.none,
                                            //     borderRadius:
                                            //         BorderRadius.circular(20))
                                          ),
                                        ),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        TextFormField(
                                          obscureText: _isObscure,
                                          decoration: InputDecoration(
                                            filled: true,
                                            suffixIcon: IconButton(
                                              icon: Icon(
                                                _isObscure
                                                    ? Icons.visibility
                                                    : Icons.visibility_off,
                                              ),
                                              onPressed: () {
                                                setState(() {
                                                  _isObscure = !_isObscure;
                                                });
                                              },
                                            ),
                                            label: Text("Password"),
                                            hintText: 'Enter Password',
                                            // enabledBorder: OutlineInputBorder(
                                            //     borderSide: BorderSide.none,
                                            //     borderRadius:
                                            //         BorderRadius.circular(20))
                                          ),
                                        ),
                                        SizedBox(
                                          height: 20,
                                        ),
                                        Container(
                                          child: Row(
                                            children: [
                                              Expanded(
                                                  child: ElevatedButton(
                                                style: ElevatedButton.styleFrom(
                                                    padding: EdgeInsets.all(20),
                                                    shape:
                                                        RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        30))),
                                                child: Text('Login'),
                                                onPressed: () {},
                                              ))
                                            ],
                                          ),
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(top: 10),
                                          child: Row(
                                            children: [
                                              Expanded(
                                                  child: TextButton(
                                                      onPressed: () {},
                                                      style: TextButton.styleFrom(
                                                          padding:
                                                              EdgeInsets.all(
                                                                  20),
                                                          shape: RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          30))),
                                                      child: Text(
                                                          "Forgot password?")))
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                    padding: EdgeInsets.only(top: 20),
                                  ),
                                  Container(
                                    child: Column(
                                      children: [
                                        TextFormField(
                                          decoration: InputDecoration(
                                            suffixIcon: Icon(Icons.email),
                                            filled: true,
                                            label: Text("Username"),
                                            hintText: 'Enter username',
                                          ),
                                        ),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        TextFormField(
                                          obscureText: _isObscure,
                                          decoration: InputDecoration(
                                            filled: true,
                                            suffixIcon: IconButton(
                                              icon: Icon(
                                                _isObscure
                                                    ? Icons.visibility
                                                    : Icons.visibility_off,
                                              ),
                                              onPressed: () {
                                                setState(() {
                                                  _isObscure = !_isObscure;
                                                });
                                              },
                                            ),
                                            label: Text("Password"),
                                            hintText: 'Enter Password',
                                            // enabledBorder: OutlineInputBorder(
                                            //     borderSide: BorderSide.none,
                                            //     borderRadius:
                                            //         BorderRadius.circular(20))
                                          ),
                                        ),
                                        TextFormField(
                                          decoration: InputDecoration(
                                            suffixIcon: IconButton(
                                              icon: Icon(Icons.calendar_month),
                                              onPressed: () async {
                                                DateTime? pickedDate =
                                                    await showDatePicker(
                                                        context: context,
                                                        locale:
                                                            Locale('th', 'TH'),
                                                        initialDate: DateTime
                                                            .now(), //get today's date
                                                        firstDate: DateTime(
                                                            2000), //DateTime.now() - not to allow to choose before today.
                                                        lastDate:
                                                            DateTime(2101));
                                              },
                                            ),
                                            filled: true,
                                            label: Text("[dd/mm/yyyy]"),
                                            hintText: 'วันเกิด',
                                          ),
                                        ),
                                        SizedBox(
                                          height: 20,
                                        ),
                                        Container(
                                          child: Row(
                                            children: [
                                              Expanded(
                                                  child: ElevatedButton(
                                                style: ElevatedButton.styleFrom(
                                                    padding: EdgeInsets.all(20),
                                                    shape:
                                                        RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        30))),
                                                child: Text('Login'),
                                                onPressed: () {},
                                              ))
                                            ],
                                          ),
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(top: 10),
                                          child: Row(
                                            children: [
                                              Expanded(
                                                  child: TextButton(
                                                      onPressed: () {
                                                        Navigator.pushNamed(
                                                          context,
                                                          AuthenCodePage
                                                              .routeName,
                                                          arguments:
                                                              ScreenArguments(
                                                            'Extract Arguments Screen',
                                                            'This message is extracted in the build method.',
                                                          ),
                                                        );
                                                      },
                                                      style: TextButton.styleFrom(
                                                          padding:
                                                              EdgeInsets.all(
                                                                  20),
                                                          shape: RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          30))),
                                                      child: Text(
                                                          "Forgot password?")))
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                    padding: EdgeInsets.only(top: 20),
                                  ),
                                ]))
                          ])),
                ]),
          ),
        ],
      ),
    );
  }
}

@siteslave
Copy link
Author

siteslave commented Sep 28, 2022

import 'package:dio/dio.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

class Api {

  String imageEndpoint = 'https://apis.moph.go.th/kyc';

  Dio dio = new Dio(new BaseOptions(
      baseUrl: endPoint,
      receiveDataWhenStatusError: true,
      connectTimeout: 60 * 1000,
      receiveTimeout: 60 * 1000));

  Api() {
    dio.interceptors.add(PrettyDioLogger(
      requestHeader: true,
      requestBody: true,
      responseBody: true,
      responseHeader: false,
      compact: false,
    ));
  }

  Future<Response> login(String username, String password) async {
    String path = '/login';

    return await dio.post(path, data: {
      "username": username,
      "password": password,
    });
  }


  Future<Response> getTruckLoads(String closeStatus, String token) async {
    String path = '/info';

    return await dio.get(path,
        options: Options(
            headers: {HttpHeaders.authorizationHeader: 'Bearer $token'}));
  }
}
Future<String> uploadImage(File file) async {
    String fileName = file.path.split('/').last;
    FormData formData = FormData.fromMap({
        "file":
            await MultipartFile.fromFile(file.path, filename:fileName),
    });
    response = await dio.post("/info", data: formData);
    return response.data['id'];
}

@siteslave
Copy link
Author

 <uses-permission android:name="android.permission.CAMERA"/>
getPermissionStatus() async {
  await Permission.camera.request();
  var status = await Permission.camera.status;
  if (status.isGranted) {
    log('Camera Permission: GRANTED');
    setState(() {
      _isCameraPermissionGranted = true;
    });
    // Set and initialize the new camera
  } else {
    log('Camera Permission: DENIED');
  }
}
final ImagePicker _picker = ImagePicker();

final XFile? pickedFile = await _picker.pickImage(
            source: source,
            maxWidth: maxWidth,
            maxHeight: maxHeight,
            imageQuality: quality,
          );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment