Can anyone give my a soloution why this app code isn't running? I am trying to create a motorcycle community app for school, but I am having trouble figuring out and implementing the necessary steps to do so. However, I understand that I should try to figure it out on my own in order to learn better.
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,
),
);
}
}
CodePudding user response:
I found some solutions in your code:
- Call MyHomePage in the home, like this: class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } }
- Replaced Raised button with ElevatedButton
- Try to follow this pattern in onSaved function in TextFormField onSaved: (String? value) { _motorcycleMake = value ?? ""; },
CodePudding user response:
The problem with your code is that you are not calling the MyHomePage widget anywhere in your MyApp widget. As a result, the MyHomePage widget is not being built and displayed, and the app is just showing a grey container.
To fix this issue, you can call the MyHomePage widget in the build method of the MyApp widget, like this:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
This will make sure that the MyHomePage widget is built and displayed when the app is run.