使用bottomNavigationBar时如何为每个子屏幕都有一个自定义AppBar?

问题描述 投票:0回答:1

我有一个带有AppBar的主屏幕,也有bottomNavigationBar

由于在主屏幕的AppBar中设置了Scaffold,所以我无法为每个子屏幕都具有自定义标题。

您可能会看到,由于所有子屏幕都具有相同的AppBar,因为它是在主屏幕级别设置的。

Sub Screen 1Sub Screen 2

是否有解决方法?

flutter dart flutter-layout
1个回答
0
投票

您可以在下面复制粘贴运行完整代码您可以创建一个List<AppBar>并显示为appbarList[_selectedIndex]

代码段

List<AppBar> appbarList = [
    AppBar(
      title: const Text('Home'),
    ),
    AppBar(
      title: const Text('Basic AppBar'),
      actions: <Widget>[
        // action button
        IconButton(
          icon: Icon(Icons.stop),
          onPressed: () {},
        ),
        // action button
        IconButton(
          icon: Icon(Icons.event),
          onPressed: () {},
        ), // overflow menu
      ],
    ),
    AppBar(
      title: const Text('School'),
    ),
  ];
... 
Scaffold(
  appBar: appbarList[_selectedIndex],

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  List<AppBar> appbarList = [
    AppBar(
      title: const Text('Home'),
    ),
    AppBar(
      title: const Text('Basic AppBar'),
      actions: <Widget>[
        // action button
        IconButton(
          icon: Icon(Icons.stop),
          onPressed: () {},
        ),
        // action button
        IconButton(
          icon: Icon(Icons.event),
          onPressed: () {},
        ), // overflow menu
      ],
    ),
    AppBar(
      title: const Text('School'),
    ),
  ];

  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',
      style: optionStyle,
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
    Text(
      'Index 2: School',
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appbarList[_selectedIndex],
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            title: Text('School'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.