I want to use onTap() with GridView and searched in google.
But I did not apply this code.
Therefore could you suggest any solutions?
Recently I started use Flutter and programming.
I think that my question is abstract, so If you have any questions regarding this.
Please feel free to ask me.
Thanks
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title,
style: TextStyle(fontFamily: 'OpenSans'),
),
centerTitle: true,
),
body: Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget> [
GridView.count(
primary: true,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
shrinkWrap: true,
children: <Widget>[
Container(
padding: const EdgeInsets.all(8),
child: const Text("He'd have you all unravel at the"),
color: Colors.teal[100],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Heed not the rabble'),
color: Colors.teal[200],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Sound of screams but the'),
color: Colors.teal[300],
),
],
),
],
),
)
),
);
}
}
you can use inkwell to make anything clickable
InkWell(
onTap: () {
print('1 was clicked');
},
child: Container(
padding: const EdgeInsets.all(8),
child: const Text('Sound of screams but the'),
color: Colors.teal[300],
),
here is all of you need
import 'package:flutter/material.dart';
import 'package:testflow/clickaletexsheet.dart';
import 'package:testflow/paint.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#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> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.title,
style: TextStyle(fontFamily: 'OpenSans'),
),
centerTitle: true,
),
body: Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
GridView.count(
primary: true,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
shrinkWrap: true,
children: <Widget>[
Material(
color: Colors.teal[100],
child: InkWell(
onTap: () {
print('1 was clicked');
},
child: Container(
padding: const EdgeInsets.all(8),
child: const Text("He'd have you all unravel at the"),
),
),
),
Material(
color: Colors.teal[200],
child: InkWell(
onTap: () {
print('2 was clicked');
},
child: Container(
padding: const EdgeInsets.all(8),
child: const Text('Heed not the rabble'),
),
),
),
Material(
color: Colors.teal[300],
child: InkWell(
onTap: () {
print('3 was clicked');
},
child: Container(
padding: const EdgeInsets.all(8),
child: const Text('Sound of screams but the'),
),
),
),
],
),
],
),
)),
);
}
}
i also defined the inkwell as child of a material widget and passed the colorof container to material instead of container itself because in that way when you click on one of those, the splash will work if you don't do that user won't find out when clicked on it
read more about inkwell hereinkwell
Related
I want to show the snack bar or Dialog on any screen in the Flutter app, anyone knows the way for that ?
For example.. let's say, I receive a notification when the user is in-app, on any screen in-app. How can I display the snack bar message in that situation regardless of which screen the user is currently on?
You can do something like that :
pip_flutter: ^0.0.3
Example:
import 'package:flutter/material.dart';
import 'package:pip_flutter/pipflutter_player.dart';
import 'package:pip_flutter/pipflutter_player_configuration.dart';
import 'package:pip_flutter/pipflutter_player_controller.dart';
import 'package:pip_flutter/pipflutter_player_data_source.dart';
import 'package:pip_flutter/pipflutter_player_data_source_type.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Picture in Picture Mode'),
),
body: Center(
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => PictureInPicturePage()));
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Center(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Text(
'Picture in Picture Mode',
style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),
),
),
),
),
),
),
);
}
}
class PictureInPicturePage extends StatefulWidget {
#override
_PictureInPicturePageState createState() => _PictureInPicturePageState();
}
class _PictureInPicturePageState extends State<PictureInPicturePage> {
late PipFlutterPlayerController pipFlutterPlayerController;
final GlobalKey pipFlutterPlayerKey = GlobalKey();
#override
void initState() {
PipFlutterPlayerConfiguration pipFlutterPlayerConfiguration =
const PipFlutterPlayerConfiguration(
aspectRatio: 16 / 9,
fit: BoxFit.contain,
);
PipFlutterPlayerDataSource dataSource = PipFlutterPlayerDataSource(
PipFlutterPlayerDataSourceType.network,
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
);
pipFlutterPlayerController =
PipFlutterPlayerController(pipFlutterPlayerConfiguration);
pipFlutterPlayerController.setupDataSource(dataSource);
pipFlutterPlayerController
.setPipFlutterPlayerGlobalKey(pipFlutterPlayerKey);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Picture in Picture player"),
leading: IconButton(onPressed: (){
Navigator.of(context).pop();
}, icon: const Icon(Icons.arrow_back_ios,color: Colors.white,)),
),
body: Column(
children: [
const SizedBox(height: 20),
Flexible(
flex: 1,
fit: FlexFit.loose,
child: AspectRatio(
aspectRatio: 16 / 9,
child: PipFlutterPlayer(
controller: pipFlutterPlayerController,
key: pipFlutterPlayerKey,
),
),
),
Container(
margin: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Center(child: Text("Show PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () {
pipFlutterPlayerController
.enablePictureInPicture(pipFlutterPlayerKey);
},
),
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: Center(child: const Text("Disable PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () async {
pipFlutterPlayerController.disablePictureInPicture();
},
),
],
),
),
],
),
);
}
}
I want to implement this. I could easily achieve it in CSS using position: absolute, a bit of if-else statement in JavaScript, done.
I'm trying to achieve the same thing in Flutter (see code snippet below), I use the Stack widget, it does give the same visual result. But, I cannot click anything inside the options box.
I've done searching for a solution and found that according to the answer of this question, this behaviour is intentional, and I should refactor my code not to use ClipBehavior (Overflow is deprecated). With that being said, I could do something like just using Column instead of Stack but I need the Stack's behaviour where the Options should not push another widgets when it is being shown, similar to position: absolute in CSS.
I am wondering if there is any other widget that do the same thing as Stack but allow me to interact with the Positioned elements/widgets outside of its bound. If there is any, please let me know!
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const Scaffold(
backgroundColor: Colors.blueGrey,
body: Center(
child: CustomDropdown(),
),
);
}
}
class CustomDropdown extends StatefulWidget {
const CustomDropdown({Key? key}) : super(key: key);
#override
State<CustomDropdown> createState() => _CustomDropdownState();
}
class _CustomDropdownState extends State<CustomDropdown> {
bool showOptions = false;
#override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
_buildPrimaryButton(),
if (showOptions) _buildOptions(),
],
);
}
Widget _buildPrimaryButton() {
return Ink(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey.shade200),
borderRadius: BorderRadius.circular(10),
),
child: InkWell(
onTap: () {
setState(() {
showOptions = !showOptions;
});
},
child: const Padding(
padding: EdgeInsets.all(10),
child: Text('Primary Button'),
),
),
);
}
Widget _buildOptions() {
return Positioned(
right: 0,
bottom: -145,
child: Ink(
padding: const EdgeInsets.symmetric(vertical: 10),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey.shade200),
borderRadius: BorderRadius.circular(10),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
InkWell(
onTap: () => print('TAPPED Option 1'),
child: const Padding(
padding: EdgeInsets.fromLTRB(20, 10, 150, 10),
child: Text('Option 1'),
),
),
InkWell(
onTap: () => print('TAPPED Option 2'),
child: const Padding(
padding: EdgeInsets.fromLTRB(20, 10, 150, 10),
child: Text('Option 2'),
),
),
InkWell(
onTap: () => print('TAPPED Option 3'),
child: const Padding(
padding: EdgeInsets.fromLTRB(20, 10, 150, 10),
child: Text('Option 3'),
),
),
],
),
),
);
}
}
I have the following widget tree:
The ListView has scrollDirection: Axis.horizontal and 2 children. The Column has crossAxisAlignment: CrossAxisAlignment.center with 3 children. The 2nd child is significantly wider than the 1st and 3rd hence it determines the width of the column. How can I make the 1st widget in the column left aligned? Currently all three widgets are centered horizontally as per the crossAxisAlignment of the Column.
Here's the layout of the column:
As expected all three children of the Column get positioned in the center of it. I need to make the first one left aligned.
My code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 2,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(
left: 10,
right: 10,
),
child: _composeCard(),
);
}),
),
),
);
}
Widget _composeCard() {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
boxShadow: [
_composeShadow(dy: 2, blur: 8),
_composeShadow(dy: 0, blur: 1),
],
),
child: Material(
clipBehavior: Clip.antiAlias,
child: InkWell(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text("Header"), // I want to push this widget to the left
SizedBox(height: 20),
Text("Body: Lorem ipsum dolor sit amet"),
SizedBox(height: 20),
Text("Footer"),
],
),
),
),
),
)),
);
}
BoxShadow _composeShadow({
required double dy,
required double blur,
double spread = 0,
}) {
return BoxShadow(
color: const Color(0xFFDBDBDB),
offset: Offset(0, dy),
spreadRadius: spread,
blurRadius: blur,
);
}
}
Thank you!
I modified your code to work.
You have to set a width for the item extend in your list, then you can use a row.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 2,
itemExtent: 300,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(
left: 10,
right: 10,
),
child: _composeCard(),
);
}),
),
),
);
}
Widget _composeCard() {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
boxShadow: [
_composeShadow(dy: 2, blur: 8),
_composeShadow(dy: 0, blur: 1),
],
),
child: Material(
clipBehavior: Clip.antiAlias,
child: InkWell(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: const [
Expanded(
child: Text("Header"),
),
],
),
const SizedBox(height: 20),
const Text("Body: Lorem ipsum dolor sit amet"),
const SizedBox(height: 20),
const Text("Footer"),
],
),
),
),
),
)),
],
),
);
}
BoxShadow _composeShadow({
required double dy,
required double blur,
double spread = 0,
}) {
return BoxShadow(
color: const Color(0xFFDBDBDB),
offset: Offset(0, dy),
spreadRadius: spread,
blurRadius: blur,
);
}
}
Edit:
I have an alternative solution if you can't use the fixed itemExtend, you could use Stack instead of Column:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(kPaddingPage),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 2,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(
left: kPaddingItemH,
right: kPaddingItemH,
),
child: _composeCard(MediaQuery.of(context).size.width),
);
}),
),
),
);
}
Widget _composeCard(double maxWidth) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
boxShadow: [
_composeShadow(dy: 2, blur: 8),
_composeShadow(dy: 0, blur: 1),
],
),
child: Material(
clipBehavior: Clip.antiAlias,
child: InkWell(
child: Container(
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text("Header"),
//other elements
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text(""),
SizedBox(height: 20),
Text("Body: Lorem ipsum dolor sit amet"),
SizedBox(height: 20),
Text("Footer"),
],
),
],
),
),
),
),
)),
],
),
);
}
BoxShadow _composeShadow({
required double dy,
required double blur,
double spread = 0,
}) {
return BoxShadow(
color: const Color(0xFFDBDBDB),
offset: Offset(0, dy),
spreadRadius: spread,
blurRadius: blur,
);
}
}
When press the Add button, it will add a TextField. When I click the minus button, I want the TextField get removed. But it keeps deleting the wrong TextField.
import 'package:flutter/material.dart';
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: '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> {
var commentList = List();
TextEditingController _commentsController = TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text("Comments", style: TextStyle(color: Colors.grey)),
SizedBox(width: 20),
Container(
height: 35,
padding: const EdgeInsets.all(10.0),
child: InkWell(
onTap: () {
setState(() {
commentList.insert(
0, _commentsController.text);
});
},
child: Row(children: [
Icon(
Icons.add,
color: Colors.white,
size: 15,
),
SizedBox(width: 5),
Text(
"Add",
style: TextStyle(color: Colors.white),
)
])),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: const Color(0xff0000ff),
)),
]),
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(10),
child: _showListViewComments()),
],
))));
}
Widget _showListViewComments() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: commentList.length + 1,
itemBuilder: (BuildContext ctxt, int index) {
if (index < commentList.length) {
return Padding(
padding: EdgeInsets.only(bottom: 10),
child: Row(children: [
Expanded(
child: TextField(
maxLines: 3,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
))),
SizedBox(width: 10),
Container(
width: 30,
height: 30,
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
setState(() {
print(index);
commentList.removeAt(index);
});
},
child: Icon(
Icons.remove,
color: Colors.white,
),
))
]));
}
});
}
}
Eg: I have text 1 and text 2. I want delete text 1 instead of text 2.
Every TextField should pair with defined TextEditingController. I edited your code to
import 'package:flutter/material.dart';
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: '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> {
List<TextEditingController> commentList = List();
//TextEditingController _commentsController = TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text("Comments", style: TextStyle(color: Colors.grey)),
SizedBox(width: 20),
Container(
height: 35,
padding: const EdgeInsets.all(10.0),
child: InkWell(
onTap: () {
setState(() {
commentList.add(TextEditingController());
// commentList.insert(
// 0, _commentsController.text);
});
},
child: Row(children: [
Icon(
Icons.add,
color: Colors.white,
size: 15,
),
SizedBox(width: 5),
Text(
"Add",
style: TextStyle(color: Colors.white),
)
])),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: const Color(0xff0000ff),
)),
]),
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(10),
child: _showListViewComments()),
],
))));
}
Widget _showListViewComments() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: commentList.length + 1,
itemBuilder: (BuildContext ctxt, int index) {
if (index < commentList.length) {
return Padding(
padding: EdgeInsets.only(bottom: 10),
child: Row(children: [
Expanded(
child: TextField(
maxLines: 3,
controller: commentList[index],
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
))),
SizedBox(width: 10),
Container(
width: 30,
height: 30,
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
setState(() {
print(index);
commentList.removeAt(index);
});
},
child: Icon(
Icons.remove,
color: Colors.white,
),
))
]));
}
});
}
}
I have tried to implement a layout like this using flutter.
I have used the Cupertino Picker Class for that.
But it just looks not as good as I want it to look.
I have tried to fix it by using the properties, that are listed on the flutter website, but I did not find any better solution. How can I fix this issue or is there even another widget that is better in this case?
My code looks like this:
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
backgroundColor: Colors.red,
),
child: Container(
child: Center(
child: CupertinoPicker(
itemExtent: 40,
diameterRatio: 5,
offAxisFraction: 10,
backgroundColor: Colors.black54,
onSelectedItemChanged: (int index) {
print(index);
},
children: <Widget>[
Text('Tap', style: TextStyle(color:Colors.white)),
Text('Colorbattle',style: TextStyle(color:Colors.white)),
Text('Patience',style: TextStyle(color:Colors.white)),
],
))));
}
}
Playing around your code, I've tried to implement the same behavior. I've observed that changing the values of diameterRatio and offAxisFraction could change the way your UI look like. In my sample, the values of diameterRatio and offAxisFraction is 10 and -1 respectively. Just imagine this kind of roulette:
diameterRatio is the diameter of the cylinder and offAxisFraction is the axis where the side of the cylinder is facing. To mimic the example in the picture, set the value of the offAxisFraction to a positive value, in the implementation shown below, I've set it to 10.
Below is an example of copy the behavior of the UI you're trying to replicate:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
backgroundColor: Colors.red,
),
child: Center(
child: Container(
// color: Colors.black54,
decoration: BoxDecoration(
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
width: 200.0,
height: 150.0,
child: Center(
child: CupertinoPicker(
itemExtent: 40,
diameterRatio: 10,
offAxisFraction: -1,
backgroundColor: Colors.black54,
onSelectedItemChanged: (int index) {
print(index);
},
children: <Widget>[
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Text(
'Estimate',
style: TextStyle(color: Colors.white),
),
),
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Text(
'Random',
style: TextStyle(color: Colors.white),
),
),
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Text(
'Colorbattle',
style: TextStyle(color: Colors.white),
),
),
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Text(
'Patience',
style: TextStyle(color: Colors.white),
),
),
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 40.0),
child: Text(
'Tap',
style: TextStyle(color: Colors.white),
),
),
),
],
),
),
),
),
);
}
}
Output: