簡體   English   中英

如何使用 flutter 中的容器創建垂直固定自定義標簽欄

[英]How to create a vertical fixed custom tab bar using container in flutter

我必須創建一個垂直左側固定垂直條,像這樣在此處輸入圖像描述

我已經使用 TabBar 創建了,但是還有很多額外的工作需要完成。 就像我必須刪除它上面的模糊 hover 效果,然后點擊 tabBar 項目應該在中心顯示其相應的內容,如果內容更多,它應該是可滾動的。

您可以在下面復制粘貼運行完整代碼
您可以使用NavigationRail
您可以更改NavigationRailDestinationiconselectedIcon
iconselectedIcon接受widget ,因此您可以根據您的請求使用Column wrap iconText
代碼片段

    NavigationRail(
        selectedIndex: _selectedIndex,
        onDestinationSelected: (int index) {
          setState(() {
            _selectedIndex = index;
          });
        },
        labelType: NavigationRailLabelType.selected,
        destinations: [
          NavigationRailDestination(
            icon: Column(
              children: [Icon(Icons.favorite_border), Text('Button 1')],
            ),
            selectedIcon: Container(
              color: Colors.green,
              child: Column(
                children: [Icon(Icons.favorite_border), Text('Button 1')],
              ),
            ),
            label: Text(""),
          ),
          NavigationRailDestination(
            icon: Column(
              children: [Icon(Icons.bookmark_border), Text('Button 2')],
            ),
            selectedIcon: Column(
              children: [Icon(Icons.book), Text('2 clicked')],
            ),
            label: Text(''),
          ),
          NavigationRailDestination(
            icon: Icon(Icons.star_border),
            selectedIcon: Icon(Icons.star),
            label: Text('Third'),
          ),
        ],
      ),

工作演示

在此處輸入圖像描述

完整代碼

import 'package:flutter/material.dart';

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

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

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

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

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: <Widget>[
          NavigationRail(
            selectedIndex: _selectedIndex,
            onDestinationSelected: (int index) {
              setState(() {
                _selectedIndex = index;
              });
            },
            labelType: NavigationRailLabelType.selected,
            destinations: [
              NavigationRailDestination(
                icon: Column(
                  children: [Icon(Icons.favorite_border), Text('Button 1')],
                ),
                selectedIcon: Container(
                  color: Colors.green,
                  child: Column(
                    children: [Icon(Icons.favorite_border), Text('Button 1')],
                  ),
                ),
                label: Text(""),
              ),
              NavigationRailDestination(
                icon: Column(
                  children: [Icon(Icons.bookmark_border), Text('Button 2')],
                ),
                selectedIcon: Column(
                  children: [Icon(Icons.book), Text('2 clicked')],
                ),
                label: Text(''),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.star_border),
                selectedIcon: Icon(Icons.star),
                label: Text('Third'),
              ),
            ],
          ),
          VerticalDivider(thickness: 1, width: 1),
          // This is the main content.
          Expanded(
            child: Center(
              child: Text('selectedIndex: $_selectedIndex'),
            ),
          )
        ],
      ),
    );
  }
}

暫無
暫無

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

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