我遇到了一个问题,它显示了一个错误,我不太明白。我试图输入一个背景图像在我的应用程序,但它不工作。
这是我的屏幕
这是我的代码。(我的代码是相当长的,所以我削减它。如果你想看到我的整个代码,我会张贴在这里)
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,
),
1条答案
按热度按时间mzsu5hc01#
Directionality
小部件用于在Flutter小部件树内的Text
小部件中设置文本方向的小部件。默认情况下,它是在flutter项目的
MaterialApp
中设置的,而在您的代码中,没有MaterialApp
,这将导致错误。因此,将
MainScreen()
Package 为Directionality
将修复您的错误,但我建议始终使用MaterialApp
,因为此类错误将与其他必要的小部件(如Navigator
、Theme
、MediaQuery
...)一起抛出。下面是您应该做,替换它:
用这个