作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用 defaulttabbarcontroller
这是代码
tab.dart
void main() => runApp(TabBar1());
final key = new GlobalKey<TabBar1State>();
class TabBar1 extends StatefulWidget {
TabBar1({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => TabBar1State();
}
class TabBar1State extends State<TabBar1> with SingleTickerProviderStateMixin {
TabController tabController;
@override
void initState() {
super.initState();
tabController = TabController(vsync: this, length: 2);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: PreferredSize(
child: AppBar(
backgroundColor: Colors.greenAccent,
bottom: TabBar(
controller: tabController,
tabs: [
Tab(
child: Text("Login"),
),
Tab(
child: Text("Sign Up"),
),
],
indicatorColor: Colors.black,
),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.orange,
],
),
),
),
),
preferredSize: Size.fromHeight(200.0),
),
body: TabBarView(
controller: tabController,
children: [
LoginApp(),
SignUpApp(),
],
),
),
),
);
}
}
这是我想从登录页面调用我的 SignUp() 选项卡的代码片段
LoginApp() 页面代码片段
Padding(
padding: EdgeInsets.only(top: 30, bottom: 30),
child: new RichText(
text: TextSpan(
text: "Don't have an account? ",
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
new TextSpan(
text: 'Sign Up',
style: new TextStyle(
fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
key.currentState.tabController.animateTo(
(key.currentState.tabController
.index +
1) %
2);
}),
]),
))
但是我得到以下错误
处理手势时抛出以下 NoSuchMethodError:I/flutter (17770): getter 'tabController' 被调用为 null。I/flutter (17770): 接收器: nullI/flutter (17770):尝试调用:tabController我/flutter (17770):
如何做同样的事情以及我做错了什么?请帮忙
最佳答案
显然 tabcontroller 没有得到任何东西。以下代码可能是您要找的代码
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
static final _myTabbedPageKey = new GlobalKey<_MyTabbedPageState>();
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyTabbedPage(
key: MyApp._myTabbedPageKey,
),
);
}
}
class MyTabbedPage extends StatefulWidget {
const MyTabbedPage({Key key}) : super(key: key);
@override
_MyTabbedPageState createState() => new _MyTabbedPageState();
}
class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin {
final List<Tab> myTabs = <Tab>[
new Tab(text: 'LEFT'),
new Tab(text: 'RIGHT'),
];
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: myTabs.length);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Tab demo"),
bottom: new TabBar(
controller: _tabController,
tabs: myTabs,
),
),
body: new TabBarView(
controller: _tabController,
children: [
LoginApp(),
SignUpApp(),
],
),
);
}
}
class LoginApp extends StatefulWidget {
@override
_LoginAppState createState() => _LoginAppState();
}
class _LoginAppState extends State<LoginApp> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 30, bottom: 30),
child: new RichText(
text: TextSpan(
text: "Don't have an account? ",
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
new TextSpan(
text: 'Sign Up',
style: new TextStyle(
fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () =>
MyApp._myTabbedPageKey.currentState._tabController.animateTo((MyApp._myTabbedPageKey.currentState._tabController.index + 1) % 2),
),
]),
));
}
}
class SignUpApp extends StatefulWidget {
@override
_SignUpAppState createState() => _SignUpAppState();
}
class _SignUpAppState extends State<SignUpApp> {
@override
Widget build(BuildContext context) {
return Text('godbye');
}
}
关于flutter - DefaultTabBarController flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546584/
我正在使用 defaulttabbarcontroller 这是代码 tab.dart void main() => runApp(TabBar1()); final key = new Global
我是一名优秀的程序员,十分优秀!