当我在flutter模拟器上运行应用程序时出现白色?

vptzau2j  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(131)

当我通过android模拟器在flutter上运行我的应用程序时,我遇到了一个问题。它只是一个白色的屏幕,我根本无法与我的应用程序交互。我分享了main.dart,homepage.dart供您参考。谢谢
编辑:现在是黑屏编辑2:stuck like this for the past 10 mins

这是主.dart

import 'package:flutter/material.dart';
import 'package:flutter_application_1/widgets/stock_management.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class _HomePageState extends State<HomePage> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pressed the button this many times:',),
            Text('$counter', style: Theme.of(context).textTheme.headline4),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  counter++;
                });
              },
              child: const Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

这是主页.dart

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My Home Page'),
      ),
      body: const Center(
        child: Text('Welcome to my home page!'),
      ),
    );
  }
}
7gcisfzg

7gcisfzg1#

我认为使用计数器函数时,应该将HomePage定义为有状态的小部件,因为每次按下递增按钮时,都在更新所传递的计数器值的状态。
尝试获取主.dart文件:

import 'package:flutter/material.dart';
import 'home_page.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

对于您的home_page.dart文件,请尝试:

import 'package:flutter/material.dart';

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pressed the button this many times:',),
            Text('$counter', style: Theme.of(context).textTheme.headline4),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  counter++;
                });
              },
              child: const Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

不太确定你从模拟器中得到的空白的白色屏幕是怎么回事。你可以试着用更多关于你的开发环境的细节来截图,应该有人能帮助你。

相关问题