flutter 获取“无方向性”小部件时发现错误,无法修复

gg58donl  于 2022-12-30  发布在  Flutter
关注(0)|答案(1)|浏览(177)

我遇到了一个问题,它显示了一个错误,我不太明白。我试图输入一个背景图像在我的应用程序,但它不工作。
这是我的屏幕
这是我的代码。(我的代码是相当长的,所以我削减它。如果你想看到我的整个代码,我会张贴在这里)

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return MainScreen();
  }
}

class MainScreen extends State<MyApp> {
  final currentTime = DateTime.now();
  final busStopController = TextEditingController();

  //To customise the greeting according to the time
  String greeting() {
    var hour = DateTime.now().hour;
    if (hour < 12) {
      return 'Morning';
    }
    if (hour < 17) {
      return 'Afternoon';
    }
    return 'Evening';
  }

  //Strings for user input and busStop
  String name = '';
  String busStop = '';

  //Strings for different bus timings
  String sb1timing = '';
  String sb2timing = '';
  String sb3timing = '';
  String sb4timing = '';

  //Different icon colors for bus capacity
  Color _iconColorDefault1 = Colors.white;
  Color _iconColorDefault2 = Colors.white;
  Color _iconColorDefault3 = Colors.white;
  Color _iconColorDefault4 = Colors.white;

  @override
  Widget build(BuildContext context) {
    return Builder(builder: (context) {
      return Stack(children: [
        const Backgroundimage(),
        Scaffold(
          backgroundColor: Colors.transparent,
          drawer: Drawer(
            child: ListView(
              padding: EdgeInsets.zero,
              children: [
                const DrawerHeader(
                  decoration: BoxDecoration(
                    color: Colors.black45,
                  ),
mzsu5hc0

mzsu5hc01#

Directionality小部件用于在Flutter小部件树内的Text小部件中设置文本方向的小部件。
默认情况下,它是在flutter项目的MaterialApp中设置的,而在您的代码中,没有MaterialApp,这将导致错误。
因此,将MainScreen() Package 为Directionality将修复您的错误,但我建议始终使用MaterialApp,因为此类错误将与其他必要的小部件(如NavigatorThemeMediaQuery ...)一起抛出。
下面是您应该做,替换它:

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return MainScreen();
  }

用这个

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return MaterialApp( // this should be present
    home: MainScreen(),
    );
  }
 }
}

相关问题