Maintain font size during navigation in flutter - flutter

I want the second screen to get only the text size from the first screen and apply it to its text. The font size increases or decreases as a user pinch the screen. So I want to get the desire font size and apply it to the text in the second screen so that the user wouldn't have to pinch the screen anytime he navigate to another screen. How do I do it?
class One extends StatefulWidget {
#override
_OneState createState() => _OneState();
}
class _OneState extends State<One> {
double _prevScale;
double _scale;
#override
void initState() {
super.initState();
_prevScale = _scale = 1.0;
}
#override
Widget build(BuildContext context) {
return GestureDetector(
onScaleUpdate: (ScaleUpdateDetails details) {
setState(() {
_scale = (_prevScale * (details.scale));
});
},
onScaleEnd: (ScaleEndDetails details) {
setState(() {
_prevScale = _scale;
});
},
child: Scaffold(
body: ListView(
children: [
Container(
height: MediaQuery.of(context).size.height/15,
width:MediaQuery.of(context).size.width,
child: Card (
elevation: 0.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton( icon:Icon(Icons.arrow_back), iconSize: 20.0,
padding: EdgeInsets.only(left: 15.0), onPressed: (){
Navigator.of(context).pop();
},
),
IconButton( icon:Icon(Icons.share), iconSize: 20.0,
padding: EdgeInsets.only(right: 10.0), onPressed: (){
},
),
],
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton( icon:Icon(Icons.arrow_back_ios), iconSize: 20.0,
padding: EdgeInsets.only(left: 10.0),
),
Text("Selfless", style: TextStyle(fontSize: 20.0, fontFamily:"CrimsonText" ), ),
IconButton( icon:Icon(Icons.arrow_forward_ios), iconSize: 20.0,
padding: EdgeInsets.only(right: 10.0), onPressed: (){
Navigator.of(context).push(MaterialPageRoute(builder: (_)=>Two()));
},
),
],
),
Card(
// shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
margin: EdgeInsets.all(10.0),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
Text("-1-", style: TextStyle(fontSize: (25.0*_scale).clamp(20.0, 70.0), fontFamily:"CrimsonText" )),
Row(
children: [
Expanded(
child: Text("At Wandsworth Prison the Master, ‘Abdu’l-Baha, wrote in the visitors’ book: "
"‘The greatest prison is the prison of self.’",
style: TextStyle(fontSize: (25.0*_scale).clamp(20.0, 70.0), fontFamily:"CrimsonText", height: 1.4 ), ),
),
],
)
],
),
)
)
],
),
),
);
}
}

You can pass the font size (or the scale) from the first page to the second like this.
Then, you can simply use it on the second screen.

In a new stateful widget where you need the textSize, do something like this
class TwoState extends StatefulWidget {
double size;
TwoState(this.size);
#override
_TwoStateState createState() => _TwoStateState(size);
}
class _TwoStateState extends State<TwoState> {
double size;
_TwoStateState(this.size);
#override
Widget build(BuildContext context) {
return Container(
child: Text("",style:TextStyle(fontSize: 25*size)),
);
}
}
And push it like
Navigator.push(context, MaterialPageRoute(builder: (context)=>TwoState(size)))
But this only works from parent to child. You can try to do something like this
Size.dart
class Sizing{
double size;
Sizing(this.size);
}
Then in the home page, create a new variable like
Sizing size = Sizing(25);
And then instead of size variable use size.size and pass it around like the previous example. This way, it will work from parent to child and from child to parent

auto_size_text: ^2.1.0.
AutoSizeText() instead of Text()
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: SizedBox(
width: 200.0,
height: 140.0,
child: AutoSizeText(
'This string will be automatically resized to fit in two lines.',
style: TextStyle(fontSize: 30.0),
maxLines: 2,
),
),
),
),
);
}
}

Related

How to create an admin UI left menu with Flutter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to flutter but I have a curiosity. Considering the typical bootstrap admin UI that you can find online and the typical left menu, how would you recreate that with flutter? I'm particularly interested on a left menu that can be resized clicking on a button and on a sub-menu that can appear and disappear.
An example can be found here
Edit:
I want to be be clear about the effect I'm trying to reproduce as well. If you click the link relative to the example on the left you see a number of menu. For instance, clicking on Base you are going to see a vertical menu appearing and disappearing. I would like to know how to reproduce it as well.
Thanks
Thanks
I have tried to re-create the same design with some minor changes in Flutter. I have to enable flutter web support by following the instructions here:
Flutter Web
Regarding the left menu, I have used AnimatedSize widget to give the sliding drawer feel & placed it inside Row.
Please find 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: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget>
with SingleTickerProviderStateMixin {
final colors = <Color>[Colors.indigo, Colors.blue, Colors.orange, Colors.red];
double _size = 250.0;
bool _large = true;
void _updateSize() {
setState(() {
_size = _large ? 250.0 : 0.0;
_large = !_large;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
AnimatedSize(
curve: Curves.easeIn,
vsync: this,
duration: Duration(seconds: 1),
child: LeftDrawer(size: _size)),
Expanded(
flex: 4,
child: Container(
child: Column(
children: [
Container(
color: Colors.white,
padding: const EdgeInsets.all(8),
child: Row(
children: [
IconButton(
icon: Icon(Icons.menu, color: Colors.black87),
onPressed: () {
_updateSize();
},
),
FlatButton(
child: Text(
'Dashboard',
style: const TextStyle(color: Colors.black87),
),
onPressed: () {},
),
FlatButton(
child: Text(
'User',
style: const TextStyle(color: Colors.black87),
),
onPressed: () {},
),
FlatButton(
child: Text(
'Settings',
style: const TextStyle(color: Colors.black87),
),
onPressed: () {},
),
const Spacer(),
IconButton(
icon: Icon(Icons.brightness_3, color: Colors.black87),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.notification_important,
color: Colors.black87),
onPressed: () {},
),
CircleAvatar(),
],
),
),
Container(
height: 1,
color: Colors.black12,
),
Card(
margin: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0),
),
child: Container(
color: Colors.white,
padding: const EdgeInsets.all(20),
child: Row(
children: [
Text(
'Home / Admin / Dashboard',
style: const TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: ListView(
children: [
Row(
children: [
_container(0),
_container(1),
_container(2),
_container(3),
],
),
Container(
height: 400,
color: Color(0xFFE7E7E7),
padding: const EdgeInsets.all(16),
child: Card(
color: Colors.white,
child: Container(
padding: const EdgeInsets.all(16),
child: Text(
'Traffic',
style: const TextStyle(color: Colors.black87),
),
),
),
),
],
),
),
],
),
),
),
],
),
);
}
Widget _container(int index) {
return Expanded(
child: Container(
padding: const EdgeInsets.all(20),
color: Color(0xFFE7E7E7),
child: Card(
color: Color(0xFFE7E7E7),
child: Container(
color: colors[index],
width: 250,
height: 140,
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
'9.823',
style: TextStyle(fontSize: 24),
)),
Icon(Icons.more_vert),
],
),
Text('Members online')
],
),
),
),
),
);
}
}
class LeftDrawer extends StatelessWidget {
const LeftDrawer({
Key key,
this.size,
}) : super(key: key);
final double size;
#override
Widget build(BuildContext context) {
return Expanded(
flex: 1,
child: Container(
width: size,
color: const Color(0xFF2C3C56),
child: ListView(
children: [
Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(16),
color: Color(0xFF223047),
child: Text('CORE UI'),
),
_tile('Dashboard'),
Container(
padding: const EdgeInsets.only(left: 10),
margin: const EdgeInsets.only(top: 30),
child: Text('THEME',
style: TextStyle(
color: Colors.white54,
))),
_tile('Colors'),
_tile('Typography'),
_tile('Base'),
_tile('Buttons'),
],
),
),
);
}
Widget _tile(String label) {
return ListTile(
title: Text(label),
onTap: () {},
);
}
}
You can use the Drawer widget inside a Scaffold. If you want the navigation drawer to be able to resize according to the browser height and width you can use the responsive_scaffold package.

Create a new card when FAB is clicked

I need create a card, when the FAB is clicked, like a list or just create a card into Column, like this image:
enter image description here
Here's one way:
import 'package:flutter/material.dart';
void main() async {
runApp(MaterialApp(debugShowCheckedModeBanner: false, home: MyApp()));
}
class MyApp extends StatefulWidget {
#override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Widget> cardWidget = [];
buildCard() {
setState(() {
cardWidget.add(
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(20.0),
child: TextFormField(),
),
Padding(
padding: EdgeInsets.all(20.0),
child: TextFormField(),
),
RaisedButton(
onPressed: () {},
padding: EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0, bottom: 10.0),
color: Colors.green,
child: Text(
'Criar',
style: TextStyle(
color: Colors.white
),
),
)
],
),
)
);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[900],
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.blue[900]
),
floatingActionButton: FloatingActionButton(
onPressed: buildCard,
backgroundColor: Colors.green,
child: Icon(
Icons.add,
color: Colors.white,
),
),
body: Column(
children: cardWidget.map((data) {
return data;
}).toList()
),
);
}
}
This method may not be the best way for you so I encourage you to try something new on your own!

Flutter: is there any possibility to send the button value to the text field?

I am Writing a small quiz game, in which I am pressing the button and these buttons are going to the empty text fields, I don't know how to send the text of the button to the text fields.
here is my code :
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: NinjaCard()));
class NinjaCard extends StatefulWidget {
#override
_NinjaCardState createState() => _NinjaCardState();
}
class _NinjaCardState extends State<NinjaCard> {
String result = "";
String shaka = "";
var text;
String str;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(title: Text('Animals'), backgroundColor: Colors.green),
body: Padding(
padding: EdgeInsets.all(15),
child: Column(
children: <Widget>[
Center(
child: Image.asset('lib/photo-1495594059084-33752639b9c3.jpg'),
),
SizedBox(width: 10, height: 10),
Row(children: <Widget>[
Container(
color: Colors.grey,
width: 40.0,
child: Text('$result', style: TextStyle(fontSize: 10.0, height: 2.0, color: Colors.black)),
),
SizedBox(width: 10),
Container(
color: Colors.grey,
width: 40.0,
child: Text('$shaka', style: TextStyle(fontSize: 10.0, height: 2.0, color: Colors.black)),
),
SizedBox(width: 15),
Row(
children: <Widget>[
SizedBox(
width: 50,
child: RaisedButton(
onPressed: () {},
color: Colors.green,
splashColor: Colors.red,
child: Text('S', style: TextStyle(backgroundColor: Colors.green, fontSize: 20, color: Colors.white)),
),
),
SizedBox(width: 15),
SizedBox(
width: 50,
child: RaisedButton(
onPressed: () {},
color: Colors.green,
splashColor: Colors.red,
child: Text('T', style: TextStyle(backgroundColor: Colors.green, fontSize: 20, color: Colors.white)),
),
),
SizedBox(width: 15),
],
),
]),
],
),
),
);
}
}
In a simple case, I would go with a stateful widget and array of letters. Of course, it could be created and sized dynamically, below I only explain the basic idea with some simplifications (no duplicate checks, no shuffling):
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
home: GuessTheWordWidget(),
);
}
}
class GuessTheWordWidget extends StatefulWidget {
#override
_GuessTheWordWidgetState createState() => _GuessTheWordWidgetState();
}
class _GuessTheWordWidgetState extends State<GuessTheWordWidget> {
String _word = 'Goldfish';
List<String> _input = List.generate(8, (_) => '');
int _position = 0;
void _press(int rune) {
setState(() {
if (_position < _input.length) {
print('Position ${_position}, rune: ${String.fromCharCode(rune)}');
_input[_position++] = String.fromCharCode(rune);
}
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('App'),
),
body: Center(
child: Column(children: <Widget>[
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _input
.map((letter) => Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent)),
child: Text(letter),
))
.toList())),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _word.runes
.map((rune) => RaisedButton(
onPressed: () => _press(rune),
child: Text(String.fromCharCode(rune),
style: TextStyle(fontSize: 20)),
))
.toList())),
])),
);
}
}
Go play with this code at DartPad: https://dartpad.dev/69bae58772305c74f1688193076ecaef!

How can I add navigation code to flutter list view?

I am having trouble inserting navigation links within my code below. I would like to learn how to insert navigation links within the second
onPressed: () {
//TODO(implement).
}
The ListView Widgets: Approved, Pending etc are the items that should contain links to other pages. The pages will be accessed by clicking on the FontAwesome.chevronRight
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class AppTab2 extends StatefulWidget {
#override
_AppTab2State createState() => _AppTab2State();
}
class _AppTab2State extends State<AppTab2> {
PageController _pageController = PageController(initialPage: 2);
#override
build(BuildContext context) {
final Map<String, Widget> pages = <String, Widget>{
'My Music': Center(
child: Text('My Music not implemented'),
),
'Shared': Center(
child: Text('Shared not implemented'),
),
'Feed': Feed(),
};
TextTheme textTheme = Theme.of(context).textTheme;
return Stack(
children: [
Container(
decoration: BoxDecoration(
// Gradient color the background
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
const Color.fromARGB(255, 253, 72, 72),
const Color.fromARGB(255, 87, 97, 249),
],
stops: [0.0, 1.0],
)),
// child: Align(
// alignment: FractionalOffset.bottomCenter,
// child: Container(
// padding: const EdgeInsets.all(10.0),
// child: Text(
// 'Demo',
// style: textTheme.headline.copyWith(
// color: Colors.grey.shade800.withOpacity(0.8),
// fontWeight: FontWeight.bold,
// ),
// ),
// ),
// ),
),
Scaffold(
backgroundColor: Colors.white,
// appBar: AppBar(
// backgroundColor: const Color(0x00000000),
// elevation: 0.0,
// leading: Center(
// child: ClipOval(
// child: Image.network(
// 'http://i.imgur.com/TtNPTe0.jpg',
// ),
// ),
// ),
// actions: [
// IconButton(
// icon: Icon(Icons.add),
// onPressed: () {
// // TODO: implement
// },
// ),
// ],
//// title: const Text('tofu\'s songs'),
// bottom: CustomTabBar(
// pageController: _pageController,
// pageNames: pages.keys.toList(),
// ),
// ),
body: PageView(
controller: _pageController,
children: pages.values.toList(),
),
),
],
);
}
}
class CustomTabBar extends AnimatedWidget implements PreferredSizeWidget {
CustomTabBar({this.pageController, this.pageNames})
: super(listenable: pageController);
final PageController pageController;
final List<String> pageNames;
#override
final Size preferredSize = Size(0.0, 40.0);
#override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
height: 40.0,
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.grey.shade800.withOpacity(0.5),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: List.generate(pageNames.length, (int index) {
return InkWell(
child: Text(pageNames[index],
style: textTheme.subhead.copyWith(
fontSize: 30.0,
fontFamily: 'OpenSans',
color: Colors.white.withOpacity(
index == pageController.page ? 1.0 : 0.2,
),
)),
onTap: () {
pageController.animateToPage(
index,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
});
}),
),
);
}
}
class Feed extends StatefulWidget {
#override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
#override
Widget build(BuildContext context) {
return ListView(
children: [
Content(
title: 'Approved',
),
Content(
title: 'Pending',
),
Content(
title: 'Published',
),
Content(
title: 'Withdrawn',
),
Content(
title: 'Declined',
),
Content(
title: 'Content Rights',
),
],
);
}
}
class Content extends StatefulWidget {
const Content({
this.title,
});
final String title;
#override
_ContentState createState() => _ContentState();
}
class _ContentState extends State<Content> {
#override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
decoration: BoxDecoration(
color: Colors.grey.shade300.withOpacity(0.5),
borderRadius: BorderRadius.circular(5.0),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
// Container(
// margin:
// const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 10.0),
// child: CircleAvatar(
// backgroundImage: NetworkImage(
// 'http://thecatapi.com/api/images/get?format=src'
// '&size=small&type=jpg#${title.hashCode}'),
// radius: 20.0,
// ),
// ),
SizedBox(
height: 15.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.title, style: textTheme.subhead),
],
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Icon(
FontAwesomeIcons.chevronRight,
size: 25.0,
color: Colors.deepPurpleAccent,
),
onTap: () {
// TODO(implement)
},
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Icon(Icons.favorite, size: 25.0),
// Text('${likes ?? ''}'),
],
),
onTap: () {
// TODO(implement)
},
),
),
],
),
));
}
}
There are some bug in your code. for demo purpose, I modify some of your code.
you can see full demo code and picture below
code snippet
child: ListView(
children: [
Content(
title: 'Approved',
screenPage: Approved(),
),
Content(
title: 'Pending',
screenPage: Pending(),
),
...
class Content extends StatefulWidget {
const Content({
this.title,
this.screenPage,
});
final String title;
final Widget screenPage;
...
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => widget.screenPage),
);
},
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(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
#override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Feed(),
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),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class AppTab2 extends StatefulWidget {
#override
_AppTab2State createState() => _AppTab2State();
}
class _AppTab2State extends State<AppTab2> {
PageController _pageController = PageController(initialPage: 2);
#override
build(BuildContext context) {
final Map<String, Widget> pages = <String, Widget>{
'My Music': Center(
child: Text('My Music not implemented'),
),
'Shared': Center(
child: Text('Shared not implemented'),
),
'Feed': Feed(),
};
TextTheme textTheme = Theme.of(context).textTheme;
return Stack(
children: [
Container(
decoration: BoxDecoration(
// Gradient color the background
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
const Color.fromARGB(255, 253, 72, 72),
const Color.fromARGB(255, 87, 97, 249),
],
stops: [0.0, 1.0],
)),
// child: Align(
// alignment: FractionalOffset.bottomCenter,
// child: Container(
// padding: const EdgeInsets.all(10.0),
// child: Text(
// 'Demo',
// style: textTheme.headline.copyWith(
// color: Colors.grey.shade800.withOpacity(0.8),
// fontWeight: FontWeight.bold,
// ),
// ),
// ),
// ),
),
Scaffold(
backgroundColor: Colors.white,
// appBar: AppBar(
// backgroundColor: const Color(0x00000000),
// elevation: 0.0,
// leading: Center(
// child: ClipOval(
// child: Image.network(
// 'http://i.imgur.com/TtNPTe0.jpg',
// ),
// ),
// ),
// actions: [
// IconButton(
// icon: Icon(Icons.add),
// onPressed: () {
// // TODO: implement
// },
// ),
// ],
//// title: const Text('tofu\'s songs'),
// bottom: CustomTabBar(
// pageController: _pageController,
// pageNames: pages.keys.toList(),
// ),
// ),
body: PageView(
controller: _pageController,
children: pages.values.toList(),
),
),
],
);
}
}
class CustomTabBar extends AnimatedWidget implements PreferredSizeWidget {
CustomTabBar({this.pageController, this.pageNames})
: super(listenable: pageController);
final PageController pageController;
final List<String> pageNames;
#override
final Size preferredSize = Size(0.0, 40.0);
#override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
height: 40.0,
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.grey.shade800.withOpacity(0.5),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: List.generate(pageNames.length, (int index) {
return InkWell(
child: Text(pageNames[index],
style: textTheme.subhead.copyWith(
fontSize: 30.0,
fontFamily: 'OpenSans',
color: Colors.white.withOpacity(
index == pageController.page ? 1.0 : 0.2,
),
)),
onTap: () {
pageController.animateToPage(
index,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
});
}),
),
);
}
}
class Approved extends StatefulWidget {
#override
_ApprovedState createState() => _ApprovedState();
}
class _ApprovedState extends State<Approved> {
#override
Widget build(BuildContext context) {
return Text('Approved');
}
}
class Pending extends StatefulWidget {
#override
_PendingState createState() => _PendingState();
}
class _PendingState extends State<Pending> {
#override
Widget build(BuildContext context) {
return Text('Pending');
}
}
class Feed extends StatefulWidget {
#override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
#override
Widget build(BuildContext context) {
return Container(
height: 300,
child: ListView(
children: [
Content(
title: 'Approved',
screenPage: Approved(),
),
Content(
title: 'Pending',
screenPage: Pending(),
),
Content(
title: 'Published',
),
Content(
title: 'Withdrawn',
),
Content(
title: 'Declined',
),
Content(
title: 'Content Rights',
),
],
),
);
}
}
class Content extends StatefulWidget {
const Content({
this.title,
this.screenPage,
});
final String title;
final Widget screenPage;
#override
_ContentState createState() => _ContentState();
}
class _ContentState extends State<Content> {
#override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
decoration: BoxDecoration(
color: Colors.grey.shade300.withOpacity(0.5),
borderRadius: BorderRadius.circular(5.0),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
Text("this is Content"),
// Container(
// margin:
// const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 10.0),
// child: CircleAvatar(
// backgroundImage: NetworkImage(
// 'http://thecatapi.com/api/images/get?format=src'
// '&size=small&type=jpg#${title.hashCode}'),
// radius: 20.0,
// ),
// ),
SizedBox(
height: 15.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.title, style: textTheme.subhead),
],
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Icon(
FontAwesomeIcons.chevronRight,
size: 25.0,
color: Colors.deepPurpleAccent,
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => widget.screenPage),
);
},
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Icon(Icons.favorite, size: 25.0),
// Text('${likes ?? ''}'),
],
),
onTap: () {
// TODO(implement)
},
),
),
],
),
));
}
}

How to center AppBar and how to reduce leading icon size in PrefrerredSize?

How to center whole AppBar and how to reduce leading icon?
I tried with Center widget and Column with mainAxisAlignment.center not working.
And I tried add width and height to leading icon container.but nothing is working
appBar: PreferredSize(
child: AppBar(
leading: Container(
decoration: BoxDecoration(..),
child: Icon(..),
),
title: TextFormField(
...
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
...
),
CupertinoSwitch(
...
)
],
)
],
),
preferredSize: Size.fromHeight(80.0)),
As shown here.
as an option
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AwesomeAppBar(height: 80),
),
);
}
}
class AwesomeAppBar extends PreferredSize {
final double height;
const AwesomeAppBar({Key key, #required this.height});
#override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, snapshot) {
return Container(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
height: height,
color: Theme.of(context).primaryColor,
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.all(16),
child: Icon(
Icons.arrow_back,
color: Colors.white,
),
),
Expanded(
child: Container(
height: 32,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(horizontal: 16),
margin: EdgeInsets.only(right: 16),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
color: Colors.white,
),
child: Text('Search'),
),
),
SwitchWithText(),
SizedBox(width: 16),
],
),
);
});
}
#override
Size get preferredSize => Size.fromHeight(height);
}
class SwitchWithText extends StatefulWidget {
#override
_SwitchWithTextState createState() => _SwitchWithTextState();
}
class _SwitchWithTextState extends State<SwitchWithText> {
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Text('Online', style: TextStyle(color: Colors.white)),
CupertinoSwitch(
value: true,
onChanged: (b) {},
activeColor: Colors.lightBlueAccent,
),
],
);
}
}