Flutter null 安全构造函数
Flutter null safety constructor
我正在使用 flutter 的最新(空安全)版本并创建了一个构造函数。
此代码不会在屏幕上显示任何内容。为什么我看不到图标和文字?
import 'package:flutter/material.dart';
import 'input_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.lightBlue[300],//app bar ın rengi
scaffoldBackgroundColor: Colors.lightBlue[300],//scfold un body si
),
home: InputPage(),
);
}
}
以下代码input_page.dart
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class InputPage extends StatefulWidget {
@override
_InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'YAŞAM BEKLENTİSİ',
style: TextStyle(color: Colors.black54),
),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: Row(
children: [
Expanded(
child: MyContainer(),
),
Expanded(
child: MyContainer(),
),
],
),
),
Expanded(
child: MyContainer(),
),
Expanded(
child: MyContainer(),
),
Expanded(
child: Row(
children: [
Expanded(
child: MyContainer(
child: Column(
children: <Widget>[
Icon(
(FontAwesomeIcons.venus),
size: 50,
color: Colors.black54,
),
Text("KADIN"),
],
),
),
),
Expanded(
child: MyContainer(),
),
],
),
),
],
),
);
}
}
class MyContainer extends StatelessWidget {
final Color color;
final Widget? child;
MyContainer({this.color = Colors.white, this.child});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(12),
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(10), color: color),
);
}
那是因为您没有向 Container
提供 child
。
class MyContainer extends StatelessWidget {
final Color color;
final Widget? child;
MyContainer({this.color = Colors.white, this.child});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(12),
decoration: BoxDecoration(...),
child: child, // <-- Missing
);
}
}
我正在使用 flutter 的最新(空安全)版本并创建了一个构造函数。
此代码不会在屏幕上显示任何内容。为什么我看不到图标和文字?
import 'package:flutter/material.dart';
import 'input_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.lightBlue[300],//app bar ın rengi
scaffoldBackgroundColor: Colors.lightBlue[300],//scfold un body si
),
home: InputPage(),
);
}
}
以下代码input_page.dart
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class InputPage extends StatefulWidget {
@override
_InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'YAŞAM BEKLENTİSİ',
style: TextStyle(color: Colors.black54),
),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: Row(
children: [
Expanded(
child: MyContainer(),
),
Expanded(
child: MyContainer(),
),
],
),
),
Expanded(
child: MyContainer(),
),
Expanded(
child: MyContainer(),
),
Expanded(
child: Row(
children: [
Expanded(
child: MyContainer(
child: Column(
children: <Widget>[
Icon(
(FontAwesomeIcons.venus),
size: 50,
color: Colors.black54,
),
Text("KADIN"),
],
),
),
),
Expanded(
child: MyContainer(),
),
],
),
),
],
),
);
}
}
class MyContainer extends StatelessWidget {
final Color color;
final Widget? child;
MyContainer({this.color = Colors.white, this.child});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(12),
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(10), color: color),
);
}
那是因为您没有向 Container
提供 child
。
class MyContainer extends StatelessWidget {
final Color color;
final Widget? child;
MyContainer({this.color = Colors.white, this.child});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(12),
decoration: BoxDecoration(...),
child: child, // <-- Missing
);
}
}