为什么在Flutter中剖面总览不起作用?

2fjabf4q  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(97)

有人能给予我一个解决方案,为什么这个应用程序代码不运行?我试图创建一个学校的摩托车社区应用程序,但我有麻烦弄清楚和实施必要的步骤这样做。但是,我明白,我应该试着弄清楚我自己,以便更好地学习。

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

pgpifvop1#

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

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

相关问题