~garritfra/FlutterFlux

472636296c2e663af8da339fd8078f01c52053a4 — Garrit Franke 3 years ago fedeb91
feat: config screen
3 files changed, 28 insertions(+), 10 deletions(-)

M lib/config.dart
M lib/main.dart
M lib/views/user_input_view.dart
M lib/config.dart => lib/config.dart +1 -0
@@ 1,4 1,5 @@
class Config {
  static final IDENTIFIER_LOCALSTORAGE = "flutter_flux";
  static final IDENTIFIER_API_KEY = "api_key";
  static final IDENTIFIER_SERVER_URL = "server_url";
}

M lib/main.dart => lib/main.dart +13 -9
@@ 38,17 38,21 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
  final LocalStorage storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE);

  @override
  Widget build(BuildContext context) {
    /// Check if the user token is set.
    /// If that's not the case, redirect to the corresponding page
    void checkToken() {
      String token = storage.getItem(Config.IDENTIFIER_API_KEY);
      if (token == null || token.isEmpty) {
  /// Check if the user token is set.
  /// If that's not the case, redirect to the config page
  void checkConfig() async {
    await storage.ready;
    dynamic token = await storage.getItem(Config.IDENTIFIER_API_KEY);
    if (token == null || token.isEmpty) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        Navigator.pushNamed(context, "/config");
      }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    checkConfig();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),


@@ 56,7 60,7 @@ class _MyHomePageState extends State<MyHomePage> {
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[MaterialButton(onPressed: checkToken)],
          children: <Widget>[],
        ),
      ),
    );

M lib/views/user_input_view.dart => lib/views/user_input_view.dart +14 -1
@@ 1,4 1,7 @@
import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';

import '../config.dart';

class UserInputView extends StatefulWidget {
  @override


@@ 6,7 9,15 @@ class UserInputView extends StatefulWidget {
}

class _UserInputViewState extends State<UserInputView> {
  void _onSubmit() {}
  final _serverTextController = TextEditingController();
  final _apiKeyTextController = TextEditingController();
  final storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE);

  void _onSubmit() {
    storage.setItem(Config.IDENTIFIER_API_KEY, _apiKeyTextController.text);
    storage.setItem(Config.IDENTIFIER_SERVER_URL, _serverTextController.text);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(


@@ 25,6 36,7 @@ class _UserInputViewState extends State<UserInputView> {
                    child: Container(
                      padding: EdgeInsets.all(5.0),
                      child: TextField(
                        controller: _serverTextController,
                        decoration: InputDecoration(
                            border: OutlineInputBorder(), labelText: "Server"),
                      ),


@@ 34,6 46,7 @@ class _UserInputViewState extends State<UserInputView> {
                    child: Container(
                      padding: EdgeInsets.all(5.0),
                      child: TextField(
                        controller: _apiKeyTextController,
                        decoration: InputDecoration(
                            border: OutlineInputBorder(), labelText: "API Key"),
                      ),