Как я могу добавить еще одну кнопку в Scaffold?

#flutter #dart

#flutter #dart

Вопрос:

Я новичок в flutter и пытаюсь перестроить демонстрационное приложение. Как я могу добавить FloatingActioButton в приложение кажется, что я не могу добавить другое тело или что-то в этом роде. Как я могу это сделать?

 import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  int _counter = 0;

  void increase_Counter() {
    setState(() {
      _counter  = 1;
    });
  }


  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(
      appBar: AppBar(
        title: Text("Demo App"),
      ),
      body: Center(
        child: Text("Your press button $_counter times"),)
      body:(FloatingActionButton(onPressed: increase_Counter,)
    ),)
    ,
    );
  }
}

  

Ответ №1:

Вы можете проверить приведенный ниже код. Плавающая кнопка действия всегда находится внутри scaffold.

  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(
      appBar: AppBar(
        title: Text("Demo App"),
      ),
      body: Center(
        child: Text("Your press button $_counter times"),),
      floatingActionButton: FloatingActionButton(
    onPressed: () {
      // Add your onPressed code here!
    },
    child: Icon(Icons.navigation),
    backgroundColor: Colors.green,
    ),
    
 
    );
  }
  

И вы также можете следить за FloatingActionButton, чтобы лучше понять.