Flutter 中的 TabBarView 小部件:全面指南
warning:
这篇文章距离上次修改已过257天,其中的内容可能已经有所变动。
在Flutter中,TabBarView
是与TabBar
小部件一起使用的,以创建带有多个标签的界面,每个标签都有自己的滚动视图(即TabBarView
)。以下是一个简单的例子,展示如何使用TabBarView
:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: Text('TabBar View Example'),
bottom: TabBar(
tabs: [
Tab(text: 'Tab One'),
Tab(text: 'Tab Two'),
Tab(text: 'Tab Three'),
],
),
),
body: TabBarView(
children: [
Center(child: Text('Content of Tab One')),
Center(child: Text('Content of Tab Two')),
Center(child: Text('Content of Tab Three')),
],
),
),
),
);
}
}
在这个例子中,我们创建了一个包含3个标签的TabBar
,每个标签的内容通过TabBarView
的children
属性来指定。当用户点击不同的标签时,TabBarView
会显示对应的内容。DefaultTabController
是用来管理标签状态的,确保TabBar
和TabBarView
能够正确通信。
评论已关闭