24.7 C
New York
Friday, October 7, 2022

Check if a Flutter app is running on the web

You can check whether your Flutter app is running on a web browser by using the kIsWeb constant from the foundation library.

Example

Screenshot:Advertisements

The code:

// main.dart
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'KindaCode.com',
        theme: ThemeData(
          primarySwatch: Colors.indigo,
        ),
        home: const HomePage());
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('KindaCode.com')),
      body: const Center(
        child: Text(kIsWeb ? 'Web' : 'Not Web',
            style: TextStyle(
              fontSize: 40,
            )),
      ),
    );
  }
}

You can find more information about the foundation library in Flutter’s official documentation.

Further reading:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Advertisements

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles