Flutter 带颤振的导轨

Flutter 带颤振的导轨,flutter,material-design,flutter-layout,flutter-navigation,Flutter,Material Design,Flutter Layout,Flutter Navigation,材料设计指南包括一个名为的组件 如何创建带有颤振的导轨 最新版本的颤振1.17包括内置导航导轨组件。 什么是导轨 导轨是一个侧面导航组件,显示三到七个应用程序目的地,还可以选择显示一个浮动操作按钮。每个目的地都由一个图标和一个文本标签表示。rail可以在更大的屏幕(如台式机和平板电脑)上独立运行。当用户在屏幕大小和设备之间切换时,导轨还可以补充其他导航组件,例如底部导航 例子 导入“包装:颤振/材料.省道”; void main()=>runApp(MyApp()); ///此小部件是主应用程序

材料设计指南包括一个名为的组件

如何创建带有颤振的导轨

最新版本的颤振1.17包括内置导航导轨组件。 什么是导轨

导轨是一个侧面导航组件,显示三到七个应用程序目的地,还可以选择显示一个浮动操作按钮。每个目的地都由一个图标和一个文本标签表示。rail可以在更大的屏幕(如台式机和平板电脑)上独立运行。当用户在屏幕大小和设备之间切换时,导轨还可以补充其他导航组件,例如底部导航

例子
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
///此小部件是主应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
主页:MyStatefulWidget(),
);
}
}
类MyStatefulWidget扩展了StatefulWidget{
MyStatefulWidget({Key}):超级(Key:Key);
@凌驾
_MyStatefulWidgetState createState()=>\u MyStatefulWidgetState();
}
类_MyStatefulWidgetState扩展状态{
int _selectedIndex=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:世界其他地区(
儿童:[
导航轨道(
selectedIndex:\u selectedIndex,
onDestinationSelected:(int索引){
设置状态(){
_selectedIndex=索引;
});
},
labelType:NavigationRailLabelType.selected,
目的地:[
导航铁路目的地(
图标:图标(图标。收藏夹边框),
选择图标:图标(Icons.favorite),
标签:文本('First'),
),
导航铁路目的地(
图标:图标(图标、书签和边框),
选择图标:图标(Icons.book),
标签:文本('Second'),
),
导航铁路目的地(
图标:图标(图标。星形边框),
选择图标:图标(Icons.star),
标签:文本('Third'),
),
],
),
垂直分隔带(厚度:1,宽度:1),
//这是主要内容。
扩大(
儿童:中心(
子项:文本('selectedIndex:$\u selectedIndex'),
),
)
],
),
);
}
}
找到一个现场演示


是官方文件。

于2020年5月7日发布了颤振1.17。一次快速的“导航轨道抖动”就可以达到目的。

包括现场演示和示例代码

int\u选择的索引=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:世界其他地区(
儿童:[
导航轨道(
selectedIndex:\u selectedIndex,
onDestinationSelected:(int索引){
设置状态(){
_selectedIndex=索引;
});
},
labelType:NavigationRailLabelType.selected,
目的地:[
导航铁路目的地(
图标:图标(图标。收藏夹边框),
选择图标:图标(Icons.favorite),
标签:文本('First'),
),
导航铁路目的地(
图标:图标(图标、书签和边框),
选择图标:图标(Icons.book),
标签:文本('Second'),
),
导航铁路目的地(
图标:图标(图标。星形边框),
选择图标:图标(Icons.star),
标签:文本('Third'),
),
],
),
垂直分隔带(厚度:1,宽度:1),
//这是主要内容。
扩大(
儿童:中心(
子项:文本('selectedIndex:$\u selectedIndex'),
),
)
],
),
);

}
注意:导航栏用于具有宽视口的布局,如桌面web或平板电脑横向布局。对于较小的布局,如移动肖像,应使用
BottomNavigationBar

int _index = 0;

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Row(
      children: <Widget>[
        NavigationRail(
          selectedIndex: _index,
          onDestinationSelected: (index) => setState(() => _index = index),
          extended: true,
          destinations: [
            NavigationRailDestination(
              icon: Icon(Icons.favorite_border),
              label: Text('First'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.bookmark_border),
              label: Text('Second'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.star_border),
              label: Text('Third'),
            ),
          ],
        ),
        // This is the main content.
        Expanded(child: Center(child: Text('Index: $_index')))
      ],
    ),
  );
}
int\u索引=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:世界其他地区(
儿童:[
导航轨道(
已选择索引:\u索引,
onDestinationSelected:(索引)=>setState(()=>_index=index),
是的,
目的地:[
导航铁路目的地(
图标:图标(图标。收藏夹边框),
标签:文本('First'),
),
导航铁路目的地(
图标:图标(图标、书签和边框),
标签:文本('Second'),
),
导航铁路目的地(
图标:图标(图标。星形边框),
标签:文本('Third'),
),
],
),
//这是主要内容。
展开(子对象:中心(子对象:文本($Index:$\u Index)))
],
),
);
}

导航轨道

  • 显示在应用程序左侧或右侧的材质小部件,用于在少量视图之间导航,通常在三到五个视图之间
示例代码
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
debugShowCheckedModeBanner:false,
主页:HomeWidget(),
);
}
}
类HomeWidget扩展了StatefulWidget{
HomeWidget({Key}):超级(Key:Key);
@凌驾
_HomeWidgetState创建状态
int _index = 0;

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Row(
      children: <Widget>[
        NavigationRail(
          selectedIndex: _index,
          onDestinationSelected: (index) => setState(() => _index = index),
          extended: true,
          destinations: [
            NavigationRailDestination(
              icon: Icon(Icons.favorite_border),
              label: Text('First'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.bookmark_border),
              label: Text('Second'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.star_border),
              label: Text('Third'),
            ),
          ],
        ),
        // This is the main content.
        Expanded(child: Center(child: Text('Index: $_index')))
      ],
    ),
  );
}
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

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

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

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

class _HomeWidgetState extends State<HomeWidget> {
  int _selectedIndex = 0;
  bool showNavigationBar = false;

  var list = [
    HomePage(),
    WalkPage(),
    LocationPage(),
    NotificationPage(),
    SettingsPage(),
    SearchPage()
  ];

  var title = [
    "HomePage",
    'WalkPage',
    'LocationPage',
    'NotificationPage',
    'SettingsPage',
    'SearchPage'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title[_selectedIndex]),
        centerTitle: false,
        leading: IconButton(
            icon: Icon(
              Icons.menu,
              color: Colors.white,
            ),
            onPressed: () {
              setState(() {
                showNavigationBar = !showNavigationBar;
              });
            }),
      ),
      body: Container(
        child: SafeArea(
            child: Stack(
          children: <Widget>[
            list[_selectedIndex],
            Positioned(
              top: 0,
              left: 0,
              child: Visibility(
                visible: showNavigationBar,
                child: Container(
                  height: MediaQuery.of(context).size.height,
                  child: NavigationRail(
                    selectedIndex: _selectedIndex,
                    elevation: 10,
                    backgroundColor: Colors.white,
                    leading: Container(
                      child: Center(child: Text('leading')),
                    ),
                    trailing: Container(
                      child: Center(child: Text('trailing')),
                    ),
                    selectedIconTheme: IconThemeData(color: Colors.purple, size: 30),
                    unselectedIconTheme: IconThemeData(color: Colors.grey, size: 20),
                    selectedLabelTextStyle:
                        TextStyle(color: Colors.purple, fontWeight: FontWeight.bold),
                    unselectedLabelTextStyle:
                        TextStyle(color: Colors.grey, fontWeight: FontWeight.normal),
                    onDestinationSelected: (int index) {
                      setState(() {
                        _selectedIndex = index;
                        showNavigationBar = !showNavigationBar;
                      });
                    },
                    labelType: NavigationRailLabelType.none,
                    destinations: [
                      NavigationRailDestination(
                        icon: Icon(Icons.home),
                        selectedIcon: Icon(Icons.home),
                        label: Text('Home'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.directions_walk),
                        selectedIcon: Icon(Icons.directions_walk),
                        label: Text('Walk'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.location_on),
                        selectedIcon: Icon(Icons.location_on),
                        label: Text('Location'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.notifications),
                        selectedIcon: Icon(Icons.notifications),
                        label: Text('Notifications'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.settings),
                        selectedIcon: Icon(Icons.settings),
                        label: Text('Settings'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.search),
                        selectedIcon: Icon(Icons.search),
                        label: Text('Search'),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        )),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Center(
          child: Text('Home Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}

class WalkPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      child: Center(
          child: Text('Walk Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}

class LocationPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.orange,
      child: Center(
          child: Text('Location Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}

class NotificationPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      child: Center(
          child: Text('Notification Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}

class SettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.amber,
      child: Center(
          child: Text('Settings Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}

class SearchPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.teal,
      child: Center(
          child: Text('Search Page',
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0))),
    );
  }
}