检查 Flutter 应用程序是否在 Web 上运行

您可以使用基础库中的kIsWeb常量检查您的 Flutter 应用程序是否在 Web 浏览器上运行
在这里插入图片描述

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,
            )),
      ),
    );
  }
}

猜你喜欢

转载自blog.csdn.net/qq_39132095/article/details/123254496