class _BottomNavigationBar extends StatelessWidget {
const _BottomNavigationBar({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return SafeArea(
top: false,
bottom: true,
left: true,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_BottomNavigationBar(),
_BottomNavigationBar(),
_BottomNavigationBar(),
_BottomNavigationBar(),
],
),
);
}
}
class _NavigationBarItem extends StatelessWidget {
const _NavigationBarItem({super.key});
#override
Widget build(BuildContext context) {
return SafeArea(child: Text('item'));
}
i am meeting this mistake in this window
i did a lot ways but they didnot work they didnt work
Related
I have come across a strange issue. While implementing a functionality, I noticed that all of my widgets functions are called continuously, resulting in high CPU consumption. To prevent this, I changed all my widgets functions to stateless widgets but unfortunately, the problem still persists. Can anyone guide me as to why this is happening or where I am going wrong?
class BearbeitungWidget extends StatelessWidget {
const BearbeitungWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Consumer<EAProvider>(
builder: (context, v, child) => Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Visibility(
visible: v.expandBearWidget,
child: SizedBox(
child: Row(
children: [
SizedBox(
child: Column(
children: const [
SizedBox(child: Text('Vorgang')),
SizedBox(child: Text('Maßnahme')),
],
),
),
// this is called multiple times
_BearbeitungItemWidget(
itemText: 'Technische Prüfung (E)',
gridLength: 11,
backWidgetColor: const Color.fromRGBO(250, 80, 80, 1),
frontWidgetSize: 0.01,
includeIcons: true),
],
),
))
],
),
),
);
}
}
class _BearbeitungItemWidget extends StatelessWidget {
String itemText;
int gridLength;
Color backWidgetColor;
double frontWidgetSize;
bool includeIcons = false;
_BearbeitungItemWidget(
{Key? key,
required this.itemText,
required this.gridLength,
required this.backWidgetColor,
required this.frontWidgetSize,
this.includeIcons = false})
: super(key: key);
#override
Widget build(BuildContext context) {
print('_BearbeitungItemWidget CALLED');
return SizedBox(
child: Column(
children: [
Container(
child: const Text('Maßnahme'),
),
SizedBox(
// this is called multiple times
child: _StackedItemWidget(
backWidgetColor: backWidgetColor,
frontWidgetSize: frontWidgetSize,
includeIcons: includeIcons),
),
// this is called multiple times
_NumberGridWidget(length: gridLength),
],
),
);
}
}
class _NumberGridWidget extends StatelessWidget {
int length;
_NumberGridWidget({Key? key, required this.length}) : super(key: key);
#override
Widget build(BuildContext context) {
print('_NumberGridWidget CALLED');
return Container(
child: Wrap(
children: [
for (var i = 0; i < length; i++)
// this is called multiple times
_GridItemWidget(
text: i,
)
],
),
);
}
}
class _GridItemWidget extends StatelessWidget {
int text;
_GridItemWidget({Key? key, required this.text}) : super(key: key);
#override
Widget build(BuildContext context) {
print('_GridItemWidget CALLED');
return Container(
child: const Text('Maßnahme'),
);
}
}
class _StackedItemWidget extends StatelessWidget {
Color backWidgetColor;
double frontWidgetSize;
bool includeIcons = false;
_StackedItemWidget(
{Key? key,
required this.backWidgetColor,
required this.frontWidgetSize,
this.includeIcons = false})
: super(key: key);
#override
Widget build(BuildContext context) {
print('_StackedItemWidget CALLED');
return Stack(
children: [
// this is called multiple times
_StackedChildItemWidget(
textColor: backWidgetColor,
width: MediaQuery.of(context).size.width * 0.10,
includeIcons: includeIcons),
],
);
}
}
class _StackedChildItemWidget extends StatelessWidget {
Color? textColor;
double? width;
bool includeIcons = false;
_StackedChildItemWidget(
{Key? key, this.textColor, this.width, this.includeIcons = false})
: super(key: key);
#override
Widget build(BuildContext context) {
print('_StackedChildItemWidget CALLED');
return Container(
child: Center(
child: const Text('Maßnahme'),
),
);
}
}
I have a parent widget and a child widget which does not hold any state.
From my understanding, initState function will be called every time before the page is rendered (Correct me if i am wrong).
So in my case, when i render my parent widget, the initState function in my child widget is not working.
Can someone please explain why this is the case, and what is the right way to do it?
My code:
class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
#override
_HomePageState createState() =>
_HomePageState();
}
class _HomePageState extends State<HomePage>
with AutomaticKeepAliveClientMixin {
int _balanceIndex = 0;
List<HomeBalanceCardModel> _balanceCardInfos =
HomeBalanceCardInfos.getMockData();
_onBalancePageChange(index, _) {
setState(() {
_balanceIndex = index;
});
}
#override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
backgroundColor: Colors.white,
centerTitle: false,
pinned: true,
floating: false,
snap: false,
primary: true,
expandedHeight: 250.0,
title: HomeAppBar(title: "Banking"),
flexibleSpace: FlexibleSpaceBar(
background: HomeBalanceCarousel(
balanceCardInfos: _balanceCardInfos,
onPageChanged: _onBalancePageChange,
)),
),
SliverToBoxAdapter(
child:
Padding(padding: EdgeInsets.only(top: 20), child: HomeAdBanner()),
),
SliverToBoxAdapter(
child:LoanList(),
)
],
));
}
My child Widget:
class LoanList extends StatefulWidget {
const LoanList({Key? key,}) : super(key: key);
#override
_LoanListState createState() => _LoanListState();
}
class _LoanListState extends State<LoanList> {
#override
void initState() {
super.initState();
print('running init state');
}
#override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
_buildTab(),
_buildList(),
],
),
);
}
}
How to align/move a DefaultTabController to be side by side in Flutter? Like this. When I try to do this, the widget deploys in the center
as an option:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(TabBarDemo());
}
class TabBarDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Home'),
bottom: TabBarWrapper(
tabBar: TabBar(
tabs: [
Tab(child: Text('Programs')),
Tab(child: Text('Bookings')),
],
),
child: RightSideWidget(),
),
),
),
),
);
}
}
class TabBarWrapper extends StatelessWidget implements PreferredSizeWidget {
final PreferredSizeWidget tabBar;
final Widget child;
const TabBarWrapper({required this.tabBar, required this.child, Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
children: [
IntrinsicWidth(child: tabBar),
Expanded(child: child),
],
);
}
#override
Size get preferredSize => tabBar.preferredSize;
}
class RightSideWidget extends StatelessWidget {
const RightSideWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Icon(Icons.qr_code, color: Colors.white, size: 20),
Padding(
padding: const EdgeInsets.only(left: 8, right: 16),
child: Text(('Scan'), style: TextStyle(color: Colors.white)),
),
],
);
}
}
In continuation with question
The solution provided above is good. But hard for me to implement in my project.
Expected results:
I've created two tabs.
In each tab I have SingleChildScrollView wrapped with Scrollbar.
I can not have the primary scrollcontroller in both the tabs, because that throws me exception: "ScrollController attached to multiple scroll views."
For Tab ONE I use primary scrollcontroller, for Tab TWO I created Scrollcontroller and attached it.
Widgets in both the tabs should be scrollabale using keyboard and mouse.
Actual results:
For Tab ONE with primary scrollcontroller I can scroll both by keyboard and dragging scrollbar.
But for Tab TWO with non primary scrollcontroller, I have to scroll only by dragging scrollbar. This tab doesn't respond to keyboard page up /down keys.
When keyboard keys are used in Tab TWO actually contents of tab ONE are getting scrolled.
Check code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: TabExample(),
);
}
}
class TabExample extends StatefulWidget {
const TabExample({Key key}) : super(key: key);
#override
_TabExampleState createState() => _TabExampleState();
}
class _TabExampleState extends State<TabExample> {
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Text('Tab ONE')),
Tab(icon: Text('Tab TWO')),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
WidgetC(),
WidgetD(),
],
),
),
);
}
}
class WidgetC extends StatefulWidget {
const WidgetC({Key key}) : super(key: key);
#override
_WidgetCState createState() => _WidgetCState();
}
class _WidgetCState extends State<WidgetC>
with AutomaticKeepAliveClientMixin<WidgetC> {
List<Widget> children;
#override
void initState() {
children = [];
for (int i = 0; i < 20; i++) {
children.add(
Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Container(
height: 100,
width: double.infinity,
color: Colors.blue,
child: Center(child: Text('$i')),
),
),
);
}
super.initState();
}
#override
Widget build(BuildContext context) {
super.build(context);
return Scrollbar(
key: PageStorageKey('WidgetC'),
isAlwaysShown: true,
showTrackOnHover: true,
child: SingleChildScrollView(
child: Column(
children: children,
),
),
);
}
#override
bool get wantKeepAlive => true;
}
class WidgetD extends StatefulWidget {
const WidgetD({Key key}) : super(key: key);
#override
_WidgetDState createState() => _WidgetDState();
}
class _WidgetDState extends State<WidgetD>
with AutomaticKeepAliveClientMixin<WidgetD> {
List<Widget> children;
ScrollController _scrollController;
#override
void initState() {
_scrollController = ScrollController();
children = [];
for (int i = 0; i < 20; i++) {
children.add(
Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Container(
height: 100,
width: double.infinity,
color: Colors.green,
child: Center(child: Text('$i')),
),
),
);
}
super.initState();
}
#override
void dispose() {
_scrollController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
super.build(context);
return Scrollbar(
key: PageStorageKey('WidgetD'),
isAlwaysShown: true,
showTrackOnHover: true,
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
child: Column(
children: children,
),
),
);
}
#override
bool get wantKeepAlive => true;
}
This has been accepted as a bug in flutter.
Pl follow for progress here: https://github.com/flutter/flutter/issues/83711
Note for other developers facing same issue.
To overcome the mentioned problem, I changed my design layout. Instead of tabbar view I used Navigationrail widget. This solved my problem.
NavigationRail widget allowed me to attach primary scroll controller to multiple widgets without giving me exception: "ScrollController attached to multiple scroll views."
Sample code.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key key}) : super(key: key);
#override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
WidgetC _widgetC = WidgetC();
WidgetD _widgetD = WidgetD();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('NavigationRail Demo'), centerTitle: true),
body: Row(
children: <Widget>[
NavigationRail(
elevation: 8.0,
selectedIndex: _selectedIndex,
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.all,
groupAlignment: 0.0,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.favorite_border),
selectedIcon: Icon(Icons.favorite),
label: Text('Tab ONE'),
),
NavigationRailDestination(
icon: Icon(Icons.bookmark_border),
selectedIcon: Icon(Icons.book),
label: Text('Tab TWO'),
),
],
),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: _getPageAtIndex(_selectedIndex),
)
],
),
);
}
Widget _getPageAtIndex(int index) {
switch (index) {
case 0:
return _widgetC;
case 1:
return _widgetD;
}
return Container();
}
}
class WidgetC extends StatefulWidget {
const WidgetC({Key key}) : super(key: key);
#override
_WidgetCState createState() => _WidgetCState();
}
class _WidgetCState extends State<WidgetC>
with AutomaticKeepAliveClientMixin<WidgetC> {
List<Widget> children;
#override
void initState() {
children = [];
for (int i = 0; i < 20; i++) {
children.add(
Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Container(
height: 100,
width: double.infinity,
color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Center(child: Text('$i')),
),
),
);
}
super.initState();
}
#override
Widget build(BuildContext context) {
super.build(context);
return Scrollbar(
key: PageStorageKey('WidgetC'),
isAlwaysShown: true,
showTrackOnHover: true,
child: SingleChildScrollView(
child: Column(
children: children,
),
),
);
}
#override
bool get wantKeepAlive => true;
}
class WidgetD extends StatefulWidget {
const WidgetD({Key key}) : super(key: key);
#override
_WidgetDState createState() => _WidgetDState();
}
class _WidgetDState extends State<WidgetD>
with AutomaticKeepAliveClientMixin<WidgetD> {
List<Widget> children;
// ScrollController _scrollController;
#override
void initState() {
// _scrollController = ScrollController();
children = [];
for (int i = 0; i < 20; i++) {
children.add(
Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Container(
height: 100,
width: double.infinity,
color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Center(child: Text('$i')),
),
),
);
}
super.initState();
}
#override
void dispose() {
// _scrollController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
super.build(context);
return Scrollbar(
key: PageStorageKey('WidgetD'),
isAlwaysShown: true,
showTrackOnHover: true,
// controller: _scrollController,
child: SingleChildScrollView(
// controller: _scrollController,
child: Column(
children: children,
),
),
);
}
#override
bool get wantKeepAlive => true;
}
I have this class, taken and changed from one of the examples
class SignUpView extends StatelessWidget {
const SignUpView({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: 400,
child: Card(
child: SignUpForm(),
),
),
),
);
}
}
But if I want to put children , instead of child, like this
class SignUpView extends StatelessWidget {
const SignUpView({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
children: [
SizedBox(
width: 400,
child: Card(
child: SignUpForm(),
),
),
],
),
);
}
}
It says The named parameter children isn't defined.
What if I want to put more than one child in the Center container?
Center can only have a single child. Use something like Column, Row, or a ListView to use more than 1 widget for Center.
import 'package:flutter/material.dart';
class SignUpView extends StatelessWidget {
const SignUpView({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView(
children: [
SizedBox(
width: 400,
child: Card(
child: SignUpForm(),
),
),
],
),
),
);
}
}