Check if a Flutter app is running on the web

You can check if your Flutter app is running on a web browser using the kIsWeb constant in the base library
insert image description here

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: '大前端之旅',
        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('大前端之旅')),
      body: const Center(
        child: Text(kIsWeb ? 'Web' : 'Not Web',
            style: TextStyle(
              fontSize: 40,
            )),
      ),
    );
  }
}

Guess you like

Origin blog.csdn.net/qq_39132095/article/details/123254496