How i can do this in flutter? - flutter

so in my assignment i have to make this screen in flutter i did this so far but we havent learned much they said search for answers and i cant find everything
import 'package:flutter/material.dart';
import 'package:cupertino_icons/cupertino_icons.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chat App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(color: Color.fromRGBO(0, 0, 0, 1.0)),
),
home: const MyHomePage(title: 'Person'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => 0,
),
title: Text(widget.title),
),
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(image: AssetImage('images/background.png'))),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'',
),
Text(
'',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: const Color.fromRGBO(0, 0, 0, 1.0),
onPressed: () => 0,
tooltip: 'Record',
child: const Icon(Icons.mic),
),
);
}
}
I did try to do it but I cannot get to know how to add the icons in the appbar and the texts and text field so if anyone could help that would be amazing

answering your question quickly!
in AppBar use ListTile as a Widget in the title property and add leading and title inside ListTile.
To achieve action buttons, need to use action property in appbar then you can add IconButton.
Also take a look at widget catelogue

You can use Row on title and actions for right buttons.
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => 0,
),
title: Row(
children: [
CircleAvatar(),
Text(widget.title),
],
),
actions: [
IconButton(
onPressed: () {},
icon: Icon(Icons.more_vert),
),
],
),
Find more about AppBar

Related

Flutter: Using a List to display iconButtons in the fab does not work

Trying to use FabCircularMenu package and I am trying to display icons inside the FAB using a List. However, the button is showing nothing.
List _fabBarIcons = [
FaIcon(FontAwesomeIcons.search),
FaIcon(FontAwesomeIcons.briefcase),
FaIcon(FontAwesomeIcons.users),
FaIcon(FontAwesomeIcons.calendar),
FaIcon(FontAwesomeIcons.cog),
];
...List.generate(
_fabBarIcons.length,
(index) {
Ink(
decoration: const ShapeDecoration(
color: Colors.cyan,
shape: CircleBorder(),
),
child: IconButton(
icon: _fabBarIcons[index],
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
);
return Container(
height: 0,
);
},
),
I've tried to add colors to make it appear or other stuffs. I get zero error on the debug console. I have no clue why these IconButtons are not showing up here.
You cannot see anything because you are returning a Container from List.generate. Instead you should be returning the Ink widget. Please see the code below :
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Test(),
),
),
);
}
}
class Test extends StatefulWidget {
#override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
bool changeColor = false;
List _fabBarIcons = [
Icon(Icons.ac_unit),
Icon(Icons.access_time),
Icon(Icons.accessible),
Icon(Icons.ad_units),
Icon(Icons.search),
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
_fabBarIcons.length,
(index) {
return Ink(
decoration: const ShapeDecoration(
color: Colors.cyan,
shape: CircleBorder(),
),
child: IconButton(
onPressed: () {},
icon: _fabBarIcons[index],
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
);
},
),
),
);
}
}

ExpansionTile within an IconButton in AppBar flutter

I'm trying to build a collapse view after clicking an IconButton on my AppBar widget, I used ExpansionTile but nothing happens after I clicked the IconButton.
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
ExpansionTile(
title: Text(
"Settings",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold
),
),
);
},
icon: Icon(Icons.settings),
),
],
),
Did I write the code right, or should I consider refactoring it. Thanks in advance!
Yes, you need to refactor your code because you cannot insert a widget into the widget tree this way directly from a function. I'm not sure of what you are trying to do, so I made two assumptions (1) You are trying to show a Expansion Tile in the app bar or (2) You want to show a Popup menu. Please see my code below to understand how you can achieve both.
import 'package:flutter/material.dart';
final Color darkBlue = const Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
title: 'Welcome to Flutter',
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Widget myWidget = Container(
child: const Text("Flutter"),
);
String myText = 'Hello, World!';
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: myWidget, actions: [
IconButton(
onPressed: () async {
print(context);
showMenu(
context: context,
position: const RelativeRect.fromLTRB(110.0, 80.0, 0.0, 0.0),
items: ["One", "Two", "Three", "Four"]
.map(
(value) => PopupMenuItem<String>(
child: Text(value),
value: value,
),
)
.toList(),
elevation: 8.0,
);
},
icon: Icon(Icons.settings),
),
PopupMenuButton<int>(
onSelected: (selected) {
setState(
() {
myText = "You selected the menu number $selected";
},
);
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<int>>[
const PopupMenuItem<int>(
value: 1,
child: Text('First Item'),
),
const PopupMenuItem<int>(
value: 2,
child: Text('Second Item'),
),
const PopupMenuItem<int>(
value: 3,
child: Text('Thrid Item'),
),
const PopupMenuItem<int>(
value: 4,
child: Text('Fourth Item'),
),
],
)
]),
body: Center(
child: Text(myText, style: Theme.of(context).textTheme.headline4),
),
);
}
}

flutter: is there an easy way to layout my app bar?

I'm new to flutter so please bear with me. When I implement an app bar, sometimes it only has a title, some other time it has a title and returns button on the left of the bar, also, sometimes it adds another button on the right of the bar. I have to layout differently to suit different situations which are quite troublesome. Is there a convenient widget that provides three optional properties to allow me to set my title, left button, and right button or any good layout strategy? What I have done is below.
AppBar(
backgroundColor: Colors.gray,
elevation: 0.0,
title: Container(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.only(bottom: ScreenUtil.dp(11)),
height: ScreenUtil.dp(22),
width: ScreenUtil.dp(160),
child: Text(
'title',
style: TextStyle(
fontSize: ScreenUtil.sp(17), fontFamily: FontFamily.family, color: Colors.black, fontWeight: FontWeight.w600
)
),
alignment: Alignment.center,
),
),
),
),
```
You should learn more about the Appbar with other properties to assist with the things you need like leading, trailing, ...
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: _title,
home: MyStatelessWidget(),
);
}
}
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final SnackBar snackBar = const SnackBar(content: Text('Showing Snackbar'));
void openPage(BuildContext context) {
Navigator.push(context, MaterialPageRoute(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Next page'),
),
body: const Center(
child: Text(
'This is the next page',
style: TextStyle(fontSize: 24),
),
),
);
},
));
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
leading: Icon(Icons.navigate_next),
title: const Text('AppBar Demo'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.add_alert),
tooltip: 'Show Snackbar',
onPressed: () {
scaffoldKey.currentState.showSnackBar(snackBar);
},
),
IconButton(
icon: const Icon(Icons.navigate_next),
tooltip: 'Next page',
onPressed: () {
openPage(context);
},
),
],
),
body: const Center(
child: Text(
'This is the home page',
style: TextStyle(fontSize: 24),
),
),
);
}
}

Flutter - How to give a color to an IconButton?

By reading documentation, i'm sure this is well-declared, but the add icon is still gray.
class _TaskState extends State<Task> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Tasks'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
color: Colors.white,
iconSize: 32.0,
),
],
),
drawer: TheDrawer()
);
}
}
Pay attention to linter warnings. You are not passing an onPressed parameter which is required for the IconButton constructor.
Adding it should solve your issue.
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(Task());
}
class Task extends StatefulWidget {
#override
_TaskState createState() => _TaskState();
}
class _TaskState extends State {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Tasks'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
color: Colors.white,
iconSize: 32.0,
onPressed: () {
}
),
],
),
);
}
}
When the onPressed callback is null, the IconButton automatically greys itself out to indicate that the button is disabled. See the documentation for more information.

Flutter - Checkbox animation doesn't show

The value is effectively changing when clicking but the animation doesn't show :
Here's my code :
var editGender = Padding(
padding: const EdgeInsets.only(top: 12.0),
child: Column(
children: <Widget>[
CheckboxListTile(
value: _male,
onChanged: _maleChanged,
title: Text("Male"),
activeColor: Theme.of(context).primaryColor,
),
CheckboxListTile(
value: _female,
onChanged: _femaleChanged,
title: Text("Female"),
activeColor: Theme.of(context).primaryColor,
)
],
),
);
When tapping the edit button :
FlatButton(
onPressed: (){
buildShowRoundedModalBottomSheet(context, title, editGender, option);
},
child: Text('Edit'),
it shows the bottom sheet :
Future buildShowRoundedModalBottomSheet(BuildContext context, String title, Widget content,[String date]) {
return showRoundedModalBottomSheet(
context: context,
radius: 20.0,
builder: (context){
return Padding(
padding: const EdgeInsets.only(top: 20.0, bottom: 20.0, left: 20.0, right: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
title,
style: TextStyle(
fontFamily: 'SamsungSans',
fontSize: 20.0,
),
),
content,
...
I am passing the same context to the widget :/
setState would change the value but it wouldn't rebuild your bottom sheet as it is being called on a onPressed of a FlatButton. You are certainly not invoking that onPressed again but you wouldn't want to do it either.
As I mentioned in the comments a StatefulBuilder would do the job.
A working example
import 'package:flutter/material.dart';
import 'package:rounded_modal/rounded_modal.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'),
);
}
}
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;
bool value = false;
void _incrementCounter() {
showRoundedModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(builder: (context, setState) {
return Container(
height: 200.0,
child: Checkbox(value: value, onChanged: (val) {
setState(() {
value = val;
});
}),
);
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
As commented by #10101010, you'll have to use a Stateful widget. And In _femaleChanged and _maleChanged, you'll have to use setState(). Example :
void _femaleChanged(bool value) => setState(() => _female = value);