I am trying to create a circular button, but there is an error when I try to use ElevatedButton:
This is the code:
import 'package:flutter/material.dart';
void main() => runApp(Menu());
class Menu extends StatefulWidget {
const Menu({super.key});
#override
State<Menu> createState() => _MenuState();
}
class _MenuState extends State<Menu> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Container(
ElevatedButton(
backgroundColor: Colors.amberAccent,
onPressed: () {},
child: Icon(
Icons.train,
size: 35,
color: Colors.black,
),
),
),
)
);
}
}
I have also tried to create a FloatingActionButton, but it didn't worked.
Container provide child and you need to use style for decoration.
class _MenuState extends State<Menu> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.amberAccent,
),
onPressed: () {},
child: Icon(
Icons.train,
size: 35,
color: Colors.black,
),
),
),
));
}
}
Related
I have a simple app that has 4 main pages as mentioned on the bottom navigation bar, and also a subpage (a page you can access from the main page but its not a page you can access from the bottom navigation bar.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class Page extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text('Home'),
),
body: Container(
child: const Center(
child: Text("Random Page"),
),
),
);
}
}
class PageDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
endDrawer: const Drawer(),
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text('drawer'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("I want the drawer to cover the nav bar here"),
Center(
child: TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => SubPage()),
);
},
child: const Text('Go to sub page'),
),
),
],
),
),
);
}
}
class SubPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('subpage'),
),
body: const Padding(
padding: EdgeInsets.all(15.0),
child: Center(
child: Text(
"The navbar is active here and has a different appbar from the previous page"),
),
));
}
}
class CustomTab extends StatelessWidget {
CustomTab({required this.child});
final Widget child;
late BuildContext tabContext;
#override
Widget build(BuildContext context) {
return CupertinoTabView(
builder: (BuildContext context) {
tabContext = context;
return child;
},
);
}
}
class Tabbed extends StatefulWidget {
#override
_TabbedState createState() => _TabbedState();
}
class _TabbedState extends State<Tabbed> {
int _currentTab = 0;
final List<CustomTab> tabs = <CustomTab>[
CustomTab(
child: Page(),
),
CustomTab(
child: PageDrawer(),
),
CustomTab(
child: Page(),
),
CustomTab(
child: Page(),
),
];
Future<void> _setTab(int index) async {
if (_currentTab == index) {
if (Navigator.of(tabs[index].tabContext).canPop()) {
Navigator.of(tabs[index].tabContext)
.popUntil((Route<dynamic> r) => r.isFirst);
}
return;
}
setState(() {
_currentTab = index;
});
}
#override
Widget build(BuildContext context) {
return Material(
child: Column(
children: <Widget>[
_buildStack(),
_buildTabs(),
],
),
);
}
Widget _buildStack() {
return Expanded(
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
),
child: IndexedStack(
sizing: StackFit.expand,
index: _currentTab,
children: tabs,
),
),
);
}
Widget _buildTabs() {
return Container(
color: Colors.blue,
child: SafeArea(
top: false,
child: SizedBox(
height: 55.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
iconSize: 30.0,
color: _currentTab == 0
? const Color.fromRGBO(58, 66, 86, 1.0)
: const Color.fromRGBO(58, 66, 86, 0.3),
icon: const Icon(Icons.home),
onPressed: () {
_setTab(0);
},
),
IconButton(
iconSize: 30.0,
color: _currentTab == 1
? const Color.fromRGBO(58, 66, 86, 1.0)
: const Color.fromRGBO(58, 66, 86, 0.3),
icon: const Icon(Icons.search),
onPressed: () {
_setTab(1);
},
),
IconButton(
iconSize: 30.0,
color: _currentTab == 2
? const Color.fromRGBO(58, 66, 86, 1.0)
: const Color.fromRGBO(58, 66, 86, 0.3),
icon: const Icon(Icons.notifications),
onPressed: () {
_setTab(2);
},
),
IconButton(
iconSize: 30.0,
color: _currentTab == 3
? const Color.fromRGBO(58, 66, 86, 1.0)
: const Color.fromRGBO(58, 66, 86, 0.3),
icon: const Icon(Icons.settings),
onPressed: () {
_setTab(3);
},
)
],
),
),
),
);
}
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Tabbed(),
);
}
}
void main() => runApp(App());
The issue I have with it currently is that I want the drawer (on the second page) to open over the navigational bar. I understand that to do so I must have the drawer on the same level or above the navbar, but the only way I can think of doing that is to have a predefined appbar in the scaffold. The reason this doesnt work for me is that I would like the sub page to have a different appbar style but I have no known way of differentiating between them since the page index for the subpage and the main page is the same.
So this would be an invalid solution:
appbar: (_index == 1) ? Appbar() : null,
At this point im out of ideas and would appreciate any suggestions :)
I have a dart file as a 'MyDrawer' and I want to add this drawer menu to my dashboard page. Here is my code. I don't know why it doesn't work.
import 'package:flutter/material.dart';
class MyDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Menu drawer'),
decoration: BoxDecoration(
color: Colors.red,
),
),
ListTile(
leading: Icon(Icons.home, size: 40,),
title: Text('First item'),
subtitle: Text("This is the 1st item"),
trailing: Icon(Icons.more_vert),
onTap: () {
},
),
ListTile(
title: Text('Second item'),
onTap: () {
},
),
],
),
);}}
And I'm calling this drawer menu in my dashboard page. It's like that;
class Dash extends StatefulWidget { ...
class _DashState extends State<Dash> { ...
#override
void initState(){ ...
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
body: Container(
... ),
Your drawer MyDrawer should be a StatelessWidget that returns a Drawer.
This is how you should do it:
class DashboardPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
);
}
}
class MyDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Menu drawer'),
decoration: BoxDecoration(
color: Colors.red,
),
),
ListTile(
leading: Icon(Icons.home, size: 40,),
title: Text('First item'),
subtitle: Text("This is the 1st item"),
trailing: Icon(Icons.more_vert),
onTap: () {
},
),
ListTile(
title: Text('Second item'),
onTap: () {
},
),
],
),
);
}
}
All I needed was to add an appbar. I definitely missed these details. Here is the updated version.
class Dash extends StatefulWidget { ...
class _DashState extends State<Dash> { ...
#override
void initState(){ ...
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
drawer: MyDrawer(),
body: Container(
... ),
I'm trying to figure out how to have the green fill up the entire drawer space under the yellow header. Right now I have my ListTiles wrapped in a Column, in a Container, with the Container color set to green. All help is appreciated.
What I have so far
One of the ways could be wrapping the ListView that you are probably using with a Container that has color green. Please see the code below :
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Flutter Demo"),
),
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.white,
),
drawer: Container(
color: Colors.green,
child: ListView(
children: <Widget>[
DrawerHeader(
padding: const EdgeInsets.all(0),
child: Container(
child: const Center(child: Text("Profile")),
color: Colors.yellow,
),
),
ListTile(
title: const Text('Item 1'),
tileColor: Colors.green,
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: const Text('Item 2'),
tileColor: Colors.green,
onTap: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}
I want to size a Widget inside a Row/Column with the minimum size of another widget of the same Row/Column
example:
class Example extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: FittedBox(
child: Column(
children: [
LongWidget(),
FlatButton(child: Text("flat button"),
color: Colors.red,
onPressed: () {},
),
],
),
),
);
}
}
it look currently like this:
i want to size the button to the size of the long widget. I tried Expanded and CrossAxisAlignment.Stretch but both break the column
You can copy paste run full code below
You can use IntrinsicWidth and set FlatButton's minWidth: double.infinity
code snippet
return Align(
alignment: Alignment.topCenter,
child: IntrinsicWidth(
child: Column(
children: [
Text("long" * 12),
FlatButton(
minWidth: double.infinity,
child: Text("flat button"),
color: Colors.red,
onPressed: () {},
),
],
),
),
);
working demo
full code
import 'package:flutter/material.dart';
class Example extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: IntrinsicWidth(
child: Column(
children: [
Text("long" * 12),
FlatButton(
minWidth: double.infinity,
child: Text("flat button"),
color: Colors.red,
onPressed: () {},
),
],
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: "test",),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Example(),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
int _weight =60;
class RoundIconData extends StatefulWidget {
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: Icon(icon),
onPressed: (){
setState(() {
if(icon == FontAwesomeIcons.minus){
_weight--;
}
else{
_weight++
}
});
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
i am getting error while creating this.
What i Want
Custom widget with RawmaterialButton through which i can add icons.
if i add icon.minus then my given private weight wants to be decremented
else
given private weights to be incremented
You can copy paste run full code below
You have to move the following code to RoundIconData
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
and pass callback for refresh
working demo
full code
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
int weight = 60;
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
refresh() {
setState(() {});
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RoundIconData(
icon: Icon(FontAwesomeIcons.minus),
notifyParent: refresh,
),
RoundIconData(
icon: Icon(Icons.add),
notifyParent: refresh,
),
Text(
'${weight}',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class RoundIconData extends StatefulWidget {
final Icon icon;
final int pressme;
final Function() notifyParent;
RoundIconData(
{#required this.icon,
#required this.pressme,
#required this.notifyParent});
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: widget.icon,
onPressed: () {
print(widget.icon.toString());
print(Icon(FontAwesomeIcons.minus).toString());
if (widget.icon.toString() == Icon(FontAwesomeIcons.minus).toString()) {
weight--;
widget.notifyParent();
print(weight);
} else {
weight++;
widget.notifyParent();
print(weight);
}
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
Creating a Custom widget in Flutter
import 'package:flutter/material.dart';
class WelcomePage extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Auth',
theme: ThemeData(
primaryColor: Colors.purple,
scaffoldBackgroundColor: Colors.white,
),
home:Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"WELCOME TO XYZ",
style: TextStyle(fontWeight: FontWeight.bold,color: Colors.purple,fontSize: 25),
),
Padding(
padding: const EdgeInsets.only(right: 40),
child: Image.asset(
"assets/images/food_order.png",
height: 200,
),
),
SizedBox(height: 10 ),
loginMethod(),
signUpMethod(),
],
),
),
),
),
);
}
// Login Button Method Widget
Widget loginMethod(){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
),
),
);
}
// Signup button method widget
Widget signUpMethod (){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Sign up',
style: TextStyle(color: Colors.white),
),
),
),
);
}
}