I am trying to build simple TabBar but getting an error
missing the concrete implementation of 'State.build' and the declaration 'build isn't referenced'
. I have created the controller and trying to change to each tab when pressed on individuals.
I have created there tab and provided different function to them.
By clicking the Tab it will not change. I donot know where I am getting wrong?
Thank you in advance.
import 'package:flutter/material.dart';
import 'homepage.dart';
import 'secondpage.dart';
import 'thirdpage.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: appTitle,
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late TabController controller;
@override
void initState() {
super.initState();
controller = TabController(length: 3, vsync: this);
controller.addListener(() {
setState(() {});
});
}
@override
void dispose() {
super.initState();
controller.dispose();
super.dispose();
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
leading: Icon(Icons.menu),
title: Text('Tab ${controller.index 1}'),
centerTitle: true,
backgroundColor: Colors.purple,
elevation: 20,
//titleSpacing: 0,
bottom: TabBar(
controller: controller,
tabs: const [
Tab(
text: 'Home',
icon: Icon(Icons.home),
),
Tab(
text: 'Page1',
icon: Icon(Icons.star),
),
Tab(
text: 'Page2',
icon: Icon(Icons.person),
),
],
),
),
body: TabBarView(
controller: controller,
children: const [
HomePage(),
SecondPage(),
ThirdPage(),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add, size: 32),
onPressed: () {
controller.animateTo(0);
}
),
);
}
}
CodePudding user response:
You should declare _MyHomePageState class inside build widget
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
But you did inside void dispose() {}
Method.