mirror of
https://github.com/timokz/flutter-vienna-hackathon-25.git
synced 2025-11-08 21:24:20 +01:00
146 lines
4.5 KiB
Dart
146 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:serverpod_flutter/serverpod_flutter.dart';
|
|
import 'package:wien_talks_client/wien_talks_client.dart';
|
|
import 'package:wien_talks_flutter/go_router.dart';
|
|
|
|
/// Sets up a global client object that can be used to talk to the server from
|
|
/// anywhere in our app. The client is generated from your server code
|
|
/// and is set up to connect to a Serverpod running on a local server on
|
|
/// the default port. You will need to modify this to connect to staging or
|
|
/// production servers.
|
|
/// In a larger app, you may want to use the dependency injection of your choice
|
|
/// instead of using a global client object. This is just a simple example.
|
|
late final Client client;
|
|
|
|
late String serverUrl;
|
|
|
|
void main() {
|
|
// When you are running the app on a physical device, you need to set the
|
|
// server URL to the IP address of your computer. You can find the IP
|
|
// address by running `ipconfig` on Windows or `ifconfig` on Mac/Linux.
|
|
// You can set the variable when running or building your app like this:
|
|
// E.g. `flutter run --dart-define=SERVER_URL=https://api.example.com/`
|
|
const serverUrlFromEnv = String.fromEnvironment('SERVER_URL');
|
|
final serverUrl = serverUrlFromEnv.isEmpty ? 'http://$localhost:8080/' : serverUrlFromEnv;
|
|
|
|
client = Client(serverUrl)..connectivityMonitor = FlutterConnectivityMonitor();
|
|
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp.router(
|
|
title: 'Serverpod Demo',
|
|
theme: ThemeData(primarySwatch: Colors.blue),
|
|
routerConfig: router,
|
|
//home: NewsScreen(),
|
|
//home: const MyHomePage(title: 'Serverpod Example'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({super.key, required this.title});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
MyHomePageState createState() => MyHomePageState();
|
|
}
|
|
|
|
class MyHomePageState extends State<MyHomePage> {
|
|
/// Holds the last result or null if no result exists yet.
|
|
String? _resultMessage;
|
|
|
|
/// Holds the last error message that we've received from the server or null
|
|
/// if no error exists yet.
|
|
String? _errorMessage;
|
|
|
|
final _textEditingController = TextEditingController();
|
|
|
|
/// Calls the `hello` method of the `greeting` endpoint. Will set either the
|
|
/// `_resultMessage` or `_errorMessage` field, depending on if the call
|
|
/// is successful.
|
|
void _callHello() async {
|
|
try {
|
|
final result = await client.greeting.hello(_textEditingController.text);
|
|
setState(() {
|
|
_errorMessage = null;
|
|
_resultMessage = result.message;
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_errorMessage = '$e';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(widget.title)),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 16.0),
|
|
child: TextField(
|
|
controller: _textEditingController,
|
|
decoration: const InputDecoration(hintText: 'Enter your name'),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 16.0),
|
|
child: ElevatedButton(
|
|
onPressed: _callHello,
|
|
child: const Text('Send to Server'),
|
|
),
|
|
),
|
|
ResultDisplay(
|
|
resultMessage: _resultMessage,
|
|
errorMessage: _errorMessage,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// ResultDisplays shows the result of the call. Either the returned result
|
|
/// from the `example.greeting` endpoint method or an error message.
|
|
class ResultDisplay extends StatelessWidget {
|
|
final String? resultMessage;
|
|
final String? errorMessage;
|
|
|
|
const ResultDisplay({super.key, this.resultMessage, this.errorMessage});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
String text;
|
|
Color backgroundColor;
|
|
if (errorMessage != null) {
|
|
backgroundColor = Colors.red[300]!;
|
|
text = errorMessage!;
|
|
} else if (resultMessage != null) {
|
|
backgroundColor = Colors.green[300]!;
|
|
text = resultMessage!;
|
|
} else {
|
|
backgroundColor = Colors.grey[300]!;
|
|
text = 'No server response yet.';
|
|
}
|
|
|
|
return ConstrainedBox(
|
|
constraints: const BoxConstraints(minHeight: 50),
|
|
child: Container(
|
|
color: backgroundColor,
|
|
child: Center(child: Text(text)),
|
|
),
|
|
);
|
|
}
|
|
}
|