gpt4 book ai didi

flutter - DefaultTabBarController flutter

转载 作者:IT王子 更新时间:2023-10-29 07:12:02 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com