簡體   English   中英

為什么配置文件概覽在 flutter 中不起作用?

[英]Why doesn't the profile overview work in flutter?

誰能給我一個解決方案,為什么這個應用程序代碼沒有運行? 我正在嘗試為學校創建一個摩托車社區應用程序,但我無法弄清楚並實施必要的步驟來這樣做。 但是,我明白我應該自己去想辦法,這樣才能學得更好。

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 function 中遵循此模式
TextFormField(
  onSaved: (String? value) {
  _motorcycleMake = value ?? "";
  },
),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM