为什么配置文件概述在颤振中不起作用?



谁能给我一个解决方案,为什么这个应用程序代码不运行?我正在尝试为学校创建一个摩托车社区应用程序,但我很难想出并实施必要的步骤。然而,我明白为了更好地学习,我应该试着自己弄清楚。

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Container(
color: Colors.grey[700],
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 0;
final _formKey = GlobalKey<FormState>();
String _motorcycleMake = '';
String _motorcycleModel = '';
int _motorcycleYear = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
Widget _buildLocationPage() {
return Center(
child: Text('Location Page'),
);
}
Widget _buildCommunityPage() {
return Center(
child: Text('Community Page'),
);
}
Widget _buildProfilePage() {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Make'),
onSaved: (String value) {
_motorcycleMake = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Model'),
onSaved: (String value) {
_motorcycleModel = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Year'),
keyboardType: TextInputType.number,
onSaved: (String value) {
_motorcycleYear = int.parse(value);
},
),
RaisedButton(
child: Text('Submit'),
onPressed: () {
final form = _formKey.currentState;
form.save();
print('Make: $_motorcycleMake');
print('Model: $_motorcycleModel');
print('Year: $_motorcycleYear');
}
)]
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Navigation Bar Example'),
),
body: Center(
child: _selectedIndex == 0
? Text('Location content')
: _selectedIndex == 1
? Text('Community content')
: _selectedIndex == 2
? Text('Profile content')
: Text('Friends content'),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.location_on),
label: 'Location',
),
BottomNavigationBarItem(
icon: Icon(Icons.group),
label: 'Community'
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
label: 'Friends',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.blue,
onTap: _onItemTapped,
),
);
}
}

我在你的代码中找到了一些解决方案:

  • 在家里调用MyHomePage,像这样:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
  • Raised按钮替换为ElevatedButton
  • 尝试在
  • onSaved函数中遵循此模式
TextFormField(
onSaved: (String? value) {
_motorcycleMake = value ?? "";
},
),

最新更新