From 472636296c2e663af8da339fd8078f01c52053a4 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Thu, 11 Feb 2021 11:24:51 +0100 Subject: [PATCH] feat: config screen --- lib/config.dart | 1 + lib/main.dart | 22 +++++++++++++--------- lib/views/user_input_view.dart | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/config.dart b/lib/config.dart index 775cfde..cac8caa 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -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"; } diff --git a/lib/main.dart b/lib/main.dart index 686c177..8459a58 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -38,17 +38,21 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { 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 { body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, - children: [MaterialButton(onPressed: checkToken)], + children: [], ), ), ); diff --git a/lib/views/user_input_view.dart b/lib/views/user_input_view.dart index 3eb4df7..f661a8e 100644 --- a/lib/views/user_input_view.dart +++ b/lib/views/user_input_view.dart @@ -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 { - 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 { 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 { child: Container( padding: EdgeInsets.all(5.0), child: TextField( + controller: _apiKeyTextController, decoration: InputDecoration( border: OutlineInputBorder(), labelText: "API Key"), ), -- 2.45.2