简体   繁体   中英

Why doesn't the profile overview work in flutter?

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,
      ),
    );
  }
}

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 ?? "";
  },
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM