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/helper/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, connectionTimeout: const Duration(seconds: 5)) ..connectivityMonitor = FlutterConnectivityMonitor(); client.openStreamingConnection(); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp.router( title: 'Wien Talks', theme: ThemeData(primarySwatch: Colors.blue), routerConfig: router, //home: NewsScreen(), //home: const MyHomePage(title: 'Serverpod Example'), ); } } /// 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)), ), ); } }