【发布时间】:2021-07-23 02:12:28
【问题描述】:
此程序中有 2 个错误已在上图中显示
Main.dart 文件
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Personal Expenses ',
theme: ThemeData(
primarySwatch: Colors.green,
accentColor: Colors.amber,
//errorColor: Colors.red[700],
fontFamily: 'Quicksand',
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'OpenSans',
fontWeight: FontWeight.bold,
fontSize: 18,
),
button: TextStyle(color: Colors.amber)),
appBarTheme: AppBarTheme(
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<Transaction> _userTransactions = [
];
bool _showChart = false;
List<Transaction> get _recentTransactions {
return _userTransactions.where((tx) {
return tx.date.isAfter(
DateTime.now().subtract(
Duration(days: 7),
),
);
}).toList();
}
void _addNewTransaction(
String txTitle, double txAmount, DateTime chosenDate) {
final newTx = Transaction(
title: txTitle,
amount: txAmount,
date: chosenDate,
id: DateTime.now().toString(),
);
setState(() {
_userTransactions.add(newTx);
});
}
void _startAddNewTransaction(BuildContext ctx) {
showModalBottomSheet(
context: ctx,
builder: (_) {
return GestureDetector(
onTap: () {},
child: NewTransaction(_addNewTransaction),
behavior: HitTestBehavior.opaque,
);
},
);
}
void _deleteTransaction(String id) {
setState(() {
_userTransactions.removeWhere((tx) => tx.id == id);
});
}
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
final isLandscape = mediaQuery.orientation == Orientation.landscape;
final PreferredSizeWidget appbar = Platform.isIOS
“Widget”类型的值不能分配给“PreferredSizeWidget”类型的变量。
? CupertinoNavigationBar(
middle: Text(
'Personal Expenses ',
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
child: Icon(CupertinoIcons.add),
onTap: () => _startAddNewTransaction(context),
)
],
),
)
: AppBar(
title: Text(
'Personal Expenses ',
style: TextStyle(fontFamily: 'OpenSans'),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () => _startAddNewTransaction(context),
),
],
);
final txListWidget = Container(
height: (mediaQuery.size.height -
appbar.preferredSize.height -
mediaQuery.padding.top) *
0.7,
child: TransactionList(
_userTransactions,
_deleteTransaction,
),
);
final pageBody = SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
if (isLandscape)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Show Chart'),
Switch.adaptive(
activeColor: Theme.of(context).accentColor,
value: _showChart,
onChanged: (val) {
setState(() {
_showChart = val;
});
},
),
],
),
],
),
);
返回 Platform.isIOS ? CupertinoPageScaffold( 孩子:pageBody, 导航栏:应用栏,
参数类型“PreferredSizeWidget”不能分配给参数类型“ObstructingPreferredSizeWidget?”。
)
: Scaffold(
.........,
),
);
}
}
【问题讨论】:
-
在提供代码示例时,请将整个文件包含在一个代码块中,以便更容易重现您的问题。您始终可以在代码中将错误添加为 cmets。